我有一个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; }
但是,有一些关键的组合卡住了:
打开NUmlock
按SHIFT
按NumPad8
发布SHIFT
发布NumPad8
(这是一个例子,还有一些其他的,包括一个用CTRL – ALT – DEL来做的 )
GetKeyboardState现在将报告按下了VK_UP 。
从Linuxinput设备访问密钥
用linux c shell上的向上箭头显示最后的命令
C ++控制台键盘事件
发送键盘到应用程序在c#(sendkeys,postmessage,sendmessage都不工作)
如何使用keybd_event函数打印/按“@”?
发生的事件是(对应于上面的动作)。
<None>
WM_KEYDOWN , VK_SHIFT
WM_KEYUP , VK_SHIFT
WM_KEYDOWN , VK_UP
WM_KEYDOWN , VK_SHIFT
WM_KEYUP , VK_SHIFT
WM_KEYUP , VK_NUMPAD8
所以,Windows无法识别Up键,现在GetKeyboardState被破坏了。
有没有什么好方法来检查密钥的真实状态? GetAsyncKeyState和GetKeyState都报告关键是closures。
如何用XLib创build一个映射但不可见的窗口?
当subprocess运行时更好的方法来中断我的Python代码?
Linuxinput设备读取ioctl(EVIocgKEY())与read(input_event)
用linux下的ioctl重新映射键盘
在Linux上用Xlib重播“主动”键盘抓取
解决了它。
我连接到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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。