【问题标题】:Numpad key events result in stuck keys from GetKeyboardStateNumpad 键事件导致 GetKeyboardState 中的键卡住
【发布时间】:2013-08-28 23:20:41
【问题描述】:

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

这是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 的笨蛋)

GetKeyboardState 现在将报告VK_UP 被按下。

发生的事件是(对应于上面的动作)。

  1. &lt;None&gt;
  2. WM_KEYDOWN, VK_SHIFT
  3. WM_KEYUP, VK_SHIFT
    WM_KEYDOWN, VK_UP
    WM_KEYDOWN, VK_SHIFT
  4. WM_KEYUP, VK_SHIFT
  5. WM_KEYUP, VK_NUMPAD8

因此,Windows 无法识别出现了向上键,现在 GetKeyboardState 已损坏。

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

【问题讨论】:

  • 您是否尝试过使用其他键盘?
  • 是的,它发生在我们尝试过的所有键盘上。
  • 由于这是一个 MFC 应用程序,请务必检查您的 PreTranslateMessage 实现。这就是随着时间的推移,各种坏事会累积的地方。另请注意GetKeyboardState 文档中的注释:“状态会随着线程从其消息队列中删除键盘消息而发生变化。” 换句话说:如果您的应用程序做了一些可疑的事情,那么@987654344 很可能@ 感到困惑。
  • 事实证明,这种行为在一个小型独立测试应用程序中是可重现的。好主意。

标签: c++ windows mfc keyboard-events


【解决方案1】:

解决了。

我在 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;
}

【讨论】:

    猜你喜欢
    • 2018-08-07
    • 2012-03-05
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2022-12-18
    • 2013-06-02
    相关资源
    最近更新 更多