检查是否在Windows控制台中按下了键 [英] Check whether key is pressed down in Windows console

查看:60
本文介绍了检查是否在Windows控制台中按下了键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
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控制台中按下了键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆