什么是 SetWindowsHook 的 HOOKPROC 中的 LPARAM?
What is LPARAM in HOOKPROC for SetWindowsHook?
我想实现击键 (CTRL + KEY
),因此使用 SetWindowsHook (SetWindowsHook(WH_KEYBOARD, hookProcTest)
) 来“安装”一个钩子。
正在查看 HOOKPROC
LRESULT CALLBACK hookProcTest(int code, WPARAM wParam, LPARAM lParam)
code
参数似乎总是0,wParam
匹配虚拟键码,lParam
可能是指向包含它是什么键事件的结构的指针。
当我按下一个键、释放一个键或按下另一个键时,lParam
显示出巨大的变化。
我试图将其转换为 LPCWPRETSTRUCT
,但这给了我一个垃圾指针(访问成员时程序崩溃)。
那么用的是什么结构呢?
编辑:这是在这里:link
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
{
WORD vkCode = LOWORD(wParam); // virtual-key code
BYTE scanCode = LOBYTE(HIWORD(lParam)); // scan code
BOOL scanCodeE0 = (HIWORD(lParam) & KF_EXTENDED) == KF_EXTENDED; // extended-key flag, 1 if scancode has 0xE0 prefix
BOOL upFlag = (HIWORD(lParam) & KF_UP) == KF_UP; // transition-state flag, 1 on keyup
BOOL repeatFlag = (HIWORD(lParam) & KF_REPEAT) == KF_REPEAT; // previous key-state flag, 1 on autorepeat
WORD repeatCount = LOWORD(lParam); // repeat count, > 0 if several keydown messages was combined into one message
BOOL altDownFlag = (HIWORD(lParam) & KF_ALTDOWN) == KF_ALTDOWN; // ALT key was pressed
BOOL dlgModeFlag = (HIWORD(lParam) & KF_DLGMODE) == KF_DLGMODE; // dialog box is active
BOOL menuModeFlag = (HIWORD(lParam) & KF_MENUMODE) == KF_MENUMODE; // menu is active
// ...
}
break;
lParam
不是结构 - 而是一堆位字段 - 正如 Remy Lebeau 在评论中所述。
lParam
在 KeyboardProc
文档中描述 here。 lParam
在其位中编码重复计数、扫描码、扩展键标志、上下文代码、先前键状态标志和转换状态标志。
Bits
Description
0-15
The repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key.
16-23
The scan code. The value depends on the OEM.
24
Indicates whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0.
25-28
Reserved.
29
The context code. The value is 1 if the ALT key is down; otherwise, it is 0.
30
The previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
31
The transition state. The value is 0 if the key is being pressed and 1 if it is being released.
我想实现击键 (CTRL + KEY
),因此使用 SetWindowsHook (SetWindowsHook(WH_KEYBOARD, hookProcTest)
) 来“安装”一个钩子。
正在查看 HOOKPROC
LRESULT CALLBACK hookProcTest(int code, WPARAM wParam, LPARAM lParam)
code
参数似乎总是0,wParam
匹配虚拟键码,lParam
可能是指向包含它是什么键事件的结构的指针。
当我按下一个键、释放一个键或按下另一个键时,lParam
显示出巨大的变化。
我试图将其转换为 LPCWPRETSTRUCT
,但这给了我一个垃圾指针(访问成员时程序崩溃)。
那么用的是什么结构呢?
编辑:这是在这里:link
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
{
WORD vkCode = LOWORD(wParam); // virtual-key code
BYTE scanCode = LOBYTE(HIWORD(lParam)); // scan code
BOOL scanCodeE0 = (HIWORD(lParam) & KF_EXTENDED) == KF_EXTENDED; // extended-key flag, 1 if scancode has 0xE0 prefix
BOOL upFlag = (HIWORD(lParam) & KF_UP) == KF_UP; // transition-state flag, 1 on keyup
BOOL repeatFlag = (HIWORD(lParam) & KF_REPEAT) == KF_REPEAT; // previous key-state flag, 1 on autorepeat
WORD repeatCount = LOWORD(lParam); // repeat count, > 0 if several keydown messages was combined into one message
BOOL altDownFlag = (HIWORD(lParam) & KF_ALTDOWN) == KF_ALTDOWN; // ALT key was pressed
BOOL dlgModeFlag = (HIWORD(lParam) & KF_DLGMODE) == KF_DLGMODE; // dialog box is active
BOOL menuModeFlag = (HIWORD(lParam) & KF_MENUMODE) == KF_MENUMODE; // menu is active
// ...
}
break;
lParam
不是结构 - 而是一堆位字段 - 正如 Remy Lebeau 在评论中所述。
lParam
在 KeyboardProc
文档中描述 here。 lParam
在其位中编码重复计数、扫描码、扩展键标志、上下文代码、先前键状态标志和转换状态标志。
Bits Description 0-15 The repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key. 16-23 The scan code. The value depends on the OEM. 24 Indicates whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0. 25-28 Reserved. 29 The context code. The value is 1 if the ALT key is down; otherwise, it is 0. 30 The previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up. 31 The transition state. The value is 0 if the key is being pressed and 1 if it is being released.