C++ key input in Windows console

12,102

Solution 1

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

Solution 2

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 [email protected]

Share:
12,102
lamas
Author by

lamas

I'm a web developer primarily focusing on PHP, MySQL, HTML, CSS and a bit of Flash too - However I have some experience in Python and C++ too. Always there to try out new things!

Updated on June 11, 2022

Comments

  • lamas
    lamas almost 2 years

    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

  • beppe9000
    beppe9000 about 6 years
    will this trigger even if the console window has no focus?
  • c z
    c z over 2 years
    GetAsyncKeyState reads globally, even if the app isn't in focus. It is blocked my AV for making the app look like a keylogger.