Numpad键事件导致GetKeyboardState中的键被卡住

时间:2013-08-28 23:20:41

标签: c++ windows mfc keyboard-events

我有一个C ++(MFC)应用程序需要检查计时器上的keystate。如果用户按住某个键,我们会延迟处理某些代码。

以下是对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;
}

但是,有一些关键的组合被卡住了:

  1. 开启 NUMLOCK
  2. SHIFT
  3. NumPad8
  4. 发布 SHIFT
  5. 发布 NumPad8
    (这是一个例子,还有其他一些例子,其中包括 CTRL - ALT - DEL
  6. GetKeyboardState现在会报告VK_UP被按下了。

    发生的事件是(对应于上述行为)。

    1. <None>
    2. WM_KEYDOWNVK_SHIFT
    3. WM_KEYUPVK_SHIFT
      WM_KEYDOWNVK_UP
      WM_KEYDOWNVK_SHIFT
    4. WM_KEYUPVK_SHIFT
    5. WM_KEYUPVK_NUMPAD8
    6. 因此,Windows无法识别出Up键,现在GetKeyboardState已损坏。

      有没有什么好方法可以检查密钥的真实状态? GetAsyncKeyStateGetKeyState都报告密钥已关闭。

1 个答案:

答案 0 :(得分:2)

解决了它。

我迷上了InitInstance中的键盘事件,并通过扫描代码(扫描代码为键,虚拟键为值的地图)跟踪起伏。

m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD, &KeyboardHook, NULL, GetCurrentThreadId());

static LRESULT CALLBACK KeyboardHook(
    __in int nCode,
    __in WPARAM wParam,
    __in LPARAM lParam
    )
{
    // According to the docs, we're not allowed to do any "further processing" if nCode is < 0.
    // According to the docs, we should process messages if we get code HC_ACTION. 
    // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644984(v=vs.85).aspx
    // It doesn't specify what to do if another code comes in, so we will just ignore any other codes.
    if (nCode == HC_ACTION)
    {
        uint8 scanCode = (uint8)((lParam & 0x00FF0000) >> 16);
        uint8 virtKey = (uint8)wParam;
        if (lParam & 0x80000000) // key up
            KeyState::SetKeyUp(scanCode);
        else
            KeyState::SetKeyDown(scanCode, virtKey);
    }

    // We must call the next hook in the chain, according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms644975%28v=vs.85%29.aspx
    // First param is ignored, according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms644974%28v=vs.85%29.aspx )
    return CallNextHookEx(0, nCode, wParam, lParam);
}

所以,我推迟检查:

// Similarly, don't process deferred events if there are keys or mouse buttons which are currently down.
s_executeDeferredStuckKeys.clear();
if (KeyState::AnyKeysDown(s_excludeKeys, arrsize(s_excludeKeys)))
{
    s_executeDeferredResult = e_executeDeferredButtonsActive;
    KeyState::GetDownKeys(s_executeDeferredStuckKeys);
    return;
}