c++ - Numpad 键事件导致 GetKeyboardState 卡住键

标签 c++ windows mfc keyboard-events

我有一个 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被按下。

发生的事件是(对应上面的 Action )。

  1. <None>
  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坏了。

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

最佳答案

解决了。

我挂接到 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;
}

关于c++ - Numpad 键事件导致 GetKeyboardState 卡住键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499859/

相关文章:

c++ - 在opencv中对有界矩形进行排序

.net - 可以捕获环回流量的 Windows 数据包嗅探器?

c - 在C中读取进程内存

c++ - 删除指向创建数组的类的指针

c++ - 如何禁用流缓冲?

c++ - 带有 vsprintf (C++) 的查询助手的 MySQL 动态字符串大小

windows - 在 Windows 中删除大文件夹的最快方法是什么?

c++ - 有没有办法在编辑公共(public)控件中添加我自己的按钮?

c++ - 在mfc中删除以前绘制的窗口

c++ - 类成员不知何故不情愿地改变