可能重复: C ++控制台键盘事件
如果要按下某个键,我希望Windows控制台程序执行某些操作,像
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) .... }但我不知道要在 if 语句中添加什么.如何检查键"o"是否按下?
but I don't know what to put in the if statement. How do I check if the key 'o' is pressed down?
我正在使用Windows 7 64位和Visual Studio Professional 2008.
I'm using Windows 7 64-bit and Visual Studio Professional 2008.
推荐答案您应该在应用程序中注册按键事件(假设这是Windows GUI应用程序),而不是忙于查询按键,并检查按键您感兴趣.
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.
如果您实际上是在制作控制台应用程序,请参见此处: C ++控制台键盘事件
If you are actually making a console app, see here: C++ console keyboard events
更多推荐
检查是否在Windows控制台中按下了键
发布评论