对于 USB 键盘配置工具,我需要拦截所有键盘输入并检测同时按下了哪些修改键和普通键。因此,我使用了一个 Windows 低级挂钩(WH_KEYBOARD_LL),它工作正常,除了我无法确定是否按下了 WIN-Key(VK_LWIN / VK_RWIN)(控制/移位和 alt 正在工作)。
我做了一个小命令行工具来显示问题:
#include <Windows.h>
#include <iostream>
using namespace std;
HHOOK hKeyboardHook;
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
{
if (nCode < 0 || nCode != HC_ACTION )
return CallNextHookEx( hKeyboardHook, nCode, wParam, lParam);
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*)lParam;
if(wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
{
// working
if(GetAsyncKeyState(VK_CONTROL) & 0x8000)
cout << "CONTROL" << endl;
if(GetAsyncKeyState(VK_SHIFT) & 0x8000)
cout << "SHIFT" << endl;
if(GetAsyncKeyState(VK_MENU) & 0x8000) // ALT
cout << "ALT" << endl;
// VK_xWIN not working at all
if((GetAsyncKeyState(VK_LWIN) & 0x8000) || (GetAsyncKeyState(VK_RWIN) & 0x8000))
cout << "WIN" << endl;
// working for ALTGR/right-handed ALT
if((GetAsyncKeyState(VK_LCONTROL) & 0x8000) || (GetAsyncKeyState(VK_RCONTROL) & 0x8000))
cout << "LRCONTROL" << endl;
// not working at all
if((GetAsyncKeyState(VK_LSHIFT) & 0x8000) || (GetAsyncKeyState(VK_RSHIFT) & 0x8000))
cout << "LRSHIFT" << endl;
if((GetAsyncKeyState(VK_LMENU) & 0x8000) || (GetAsyncKeyState(VK_RMENU) & 0x8000))
cout << "LRMENU" << endl;
}
//return CallNextHookEx( hKeyboardHook, nCode, wParam, lParam);
return 1;
}
int main(int argc, char* argv[])
{
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0 );
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
UnhookWindowsHookEx(hKeyboardHook);
return 0;
}
如果我从 LowLevelKeyboardProc 返回“1”,则每次按键都会被“吞下”(CTRL+ALT+DEL 和 WIN+L 除外)。如果我在回调函数的末尾调用下一个钩子,则行为会发生变化(并且键显然不再被吞下)。然后,如果 WIN 键与另一个键一起按下,我会得到按下 WIN 键的信息。
我该怎么做才能拦截所有键盘输入并检测 WIN 键按下(使用 GetAsyncKeyState)?还是有另一种方法可以让所有(inkl.WIN)按下修饰键?