如何检查是否在 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
}
如果发生轮班或类似情况,您需要通过以下其中一项:https://msdn.microsoft.com/de-de/library/windows/desktop/dd375731(v=vs.85).aspx
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>
相关文章