Windows 控制台中的 C++ 键输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2246502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ key input in Windows console
提问by lamas
I'm currently developing various consolegames in Windows that won't really work using regular input via cin
.
我目前正在Windows 中开发各种控制台游戏,这些游戏实际上无法通过cin
.
How can I (In a simple way using only standard windows libraries available in MSVC):
我如何(以一种简单的方式仅使用 MSVC 中可用的标准 Windows 库):
Make the program wait for a (specific?) key press and return the key ID (It would have to work for all keys including the arrow keys)
During a real-time game check for the last pressed key of the user and if there was any key pressed since the last check.
让程序等待(特定的?)按键并返回键 ID(它必须适用于包括箭头键在内的所有键)
在实时游戏期间检查用户最后按下的键以及自上次检查以来是否按下了任何键。
It would really help if you could include a short example program for your solution
如果您可以为您的解决方案包含一个简短的示例程序,那真的很有帮助
采纳答案by Goz
AFAIK you can't do it using the standard C runtime. You will need to use something such as the Win32 function GetAsyncKeyState.
AFAIK 你不能使用标准的 C 运行时来做到这一点。您将需要使用诸如 Win32 函数GetAsyncKeyState 之类的东西。
回答by Goz
You want the Windows Console API, for example PeekConsoleInput.
您需要 Windows 控制台 API,例如PeekConsoleInput。
回答by jacopo
I've got just what you need.
我有你需要的东西。
Here enjoy pal: C++ source
这里欣赏一下朋友: C++源码
It's pretty much self-explanatory but if you have any doubts my email is [email protected]
这几乎是不言自明的,但如果您有任何疑问,我的电子邮件是 [email protected]