使用按键切换布尔值
我正在编写一个 Windows C++ 应用程序,我希望能够检测按键(在本例中,使用字母“S”)。当按下该键时,程序应打开或关闭布尔值(取决于其当前状态)。
我知道在控制台应用程序中您可以使用 cin.get,但我不熟悉 Win32 API。我还想确保当按下按键时,事件仅注册一次,即如果用户按下“S”但按住按键一段时间,程序应该只检测到“S”;不是“SSSSSS”。
I am writing a Windows C++ app which I would like to have detect a keypress (for this example, using the letter 'S'). When the key is pressed, the program should switch a bool value either on or off (depending on its current state).
I know that in console apps you can use cin.get
, but I'm unfamiliar with the Win32 API. I also would like to be sure that when the key is pressed, the event is only registered once, i.e. if the user presses 'S' but holds the key down for a while, the program should detect only 'S'; not 'SSSSSSS'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以你有一个 Windows 消息循环,对吗?捕获 WM_KEYDOWN 并检查其是否打开自动重复。
但我猜你实际上正在使用一个框架,无论是 MFC、QT 还是其他框架。该框架将包装您的 Windows 消息循环并允许您捕获关键事件,但如果您希望我们告诉您如何操作,您必须说明您正在使用什么框架。
So you have a windows message loop going, right? Capture WM_KEYDOWN and check whether it's on autorepeat.
But I'd guess you are actually using a framework, be it MFC, QT or something else. The framework will wrap your windows message loop and allow you to capture key events, but if you want us to tell you how, you'll have to say what framework you're using.