简体   繁体   English

控制台获取没有Windows消息的按键C ++

[英]Console get key press w/o windows messages c++

Is there any way to get the last key press in a console without using Windows messages or the std::cin stream? 有什么方法可以在不使用Windows消息或std :: cin流的情况下在控制台中获得最后一次按键? I've heard that there is a function in the standard library. 我听说标准库中有一个函数。 Solutions should preferably be as portable as possible. 解决方案最好应尽可能地便携。 Thanks for your help in advance. 感谢您的帮助。

There's conio.h but it's not technically standard. conio.h,但是在技术上不是标准的。 On Linux, my first Google hit suggests termios.h . 在Linux上,我的第一个Google命中建议termios.h

Not really portable but you can access the current key state using GetAsyncKeyState even from console app under windows. 并非真正可移植,但是您甚至可以从Windows下的控制台应用程序使用GetAsyncKeyState访问当前键状态。 More technical, and equally windows specific, would be to hook the keyboard using SetWindowsHookEx into a call back in your system that simply stores the last key pressed. 更技术性的(同样是特定于Windows的)将是使用SetWindowsHookEx将键盘挂接到系统中的回叫中,该回叫仅存储最后按下的键。

But your basic problem: Console + Portability - seems to imply cin is your best bet - what do you need that cin doesn't provide? 但是您的基本问题是:控制台+可移植性-似乎暗示cin是您最好的选择-cin不提供您需要什么?

Have you considered using a curses library like pdcurses? 您是否考虑过使用像pdcurses这样的curses库? That's about the only cross-platform library that will do console management that I know of. 那是我所知道的唯一可以进行控制台管理的跨平台库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM