C++ key input in Windows console

2019-04-09 18:34发布

I'm currently developing various console games in Windows that won't really work using regular input via cin.

How can I (In a simple way using only standard windows libraries available in MSVC):

  • 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.

It would really help if you could include a short example program for your solution

3条回答
贪生不怕死
2楼-- · 2019-04-09 18:42

AFAIK you can't do it using the standard C runtime. You will need to use something such as the Win32 function GetAsyncKeyState.

查看更多
叛逆
3楼-- · 2019-04-09 18:44

You want the Windows Console API, for example PeekConsoleInput.

查看更多
Root(大扎)
4楼-- · 2019-04-09 18:56

I've got just what you need.

Here enjoy pal: C++ source

It's pretty much self-explanatory but if you have any doubts my email is jacobossm@gmail.com

查看更多
登录 后发表回答