如何检查是否在C ++上按下了键 [英] How do I check if a Key is pressed on C++
本文介绍了如何检查是否在C ++上按下了键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查Windows上是否按下了按键?
How could I possibly check if a Key is pressed on Windows?
推荐答案
正如其他人所提到的,没有跨平台的方法可以做到这一点,但是在Windows上您可以这样做:
As mentioned by others there's no cross platform way to do this, but on Windows you can do it like this:
下面的代码检查键"A"是否按下.
The Code below checks if the key 'A' is down.
if(GetKeyState('A') & 0x8000/*Check if high-order bit is set (1 << 15)*/)
{
// Do stuff
}
In case of shift or similar you will need to pass one of these: https://msdn.microsoft.com/de-de/library/windows/desktop/dd375731(v=vs.85).aspx
if(GetKeyState(VK_SHIFT) & 0x8000)
{
// Shift down
}
低位指示是否切换了键.
The low-order bit indicates if key is toggled.
SHORT keyState = GetKeyState(VK_CAPITAL/*(caps lock)*/);
bool isToggled = keyState & 1;
bool isDown = keyState & 0x8000;
哦,也不要忘记
#include <Windows.h>
这篇关于如何检查是否在C ++上按下了键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文