I have a C ++ application (MFC) for which I need to check the keystat on the timer. If the user holds the key, we delay the processing of some code.
Here's the check for keydown :
if (!GetKeyboardState(keyState)) { s_executeDeferredResult = e_executeDeferredButtonCheckFailed; return; } s_executeDeferredStuckKeys.clear(); for (int index=0; index<arrsize(keyState); ++index) { if (keyState[index] & 0x80) { s_executeDeferredStuckKeys.insert(index); } } if (!s_executeDeferredStuckKeys.empty()) { s_executeDeferredResult = e_executeDeferredButtonsActive; return; }
But there are some key combos that get stuck:
- Enable NUMLOCK
- Press SHIFT
- Click NumPad8
- Release SHIFT
- NumPad8 Release
(this is one example, there are others, including doozy with CTRL - ALT - DEL )
GetKeyboardState will now report that VK_UP pressed.
Events that occur (correspond to the actions above).
<None>WM_KEYDOWN , VK_SHIFTWM_KEYUP , VK_SHIFT
WM_KEYDOWN , VK_UP
WM_KEYDOWN , VK_SHIFTWM_KEYUP , VK_SHIFTWM_KEYUP , VK_NUMPAD8
So, Windows does not recognize that the Up key has risen, and now GetKeyboardState broken.
Is there a good way to check the real state of a key? GetAsyncKeyState and GetKeyState report that the key is also turned off.
source share