Check whether key is pressed down in Windows console

Timothy picture Timothy · Jun 25, 2011 · Viewed 14.6k times · Source

Possible Duplicate:
C++ console keyboard events

I want a Windows console program to do something if a certain key is pressed down, something like

while(1)
{
    ....
    if(the key 'o' is pressed down)
      ....
}

but I don't know what to put in the if statement. How do I check if the key 'o' is pressed down?

I'm using Windows 7 64-bit and Visual Studio Professional 2008.

Answer

John Zwinck picture John Zwinck · Jun 25, 2011

Rather than busy-polling for a keypress, you should register for key events in your application (assuming this is a Windows GUI app), and check for the keys you're interested in.

If you are actually making a console app, see here: C++ console keyboard events