开发者

Check whether key is pressed down in Windows console [duplicate]

This question already has answers here: Closed 11 years ago.

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 th开发者_如何学Ce 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.


You can use the std::cin.get() or you can use the windows.h GetAsyncKeyState, depending on what exactly you want to do.

If you want lower level stuff, look into hooks and events from the WinAPI.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜