HC_ACTION
The wParam and lParam parameters contain information about a keystroke message.
HC_NOREMOVE
The wParam and lParam parameters contain information about a keystroke message, and the keystroke message has not been removed from the message queue. (An application called thePeekMessage function, specifying the PM_NOREMOVE flag.)
wParam
[in] Specifies the virtual-key code of the key that generated the keystroke message.
lParam
[in] Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. For more information about the lParam parameter, see Keystroke Message Flags. This parameter can be one or more of the following values.
0-15
Specifies 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
Specifies the scan code. The value depends on the OEM.
24
Specifies 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
Specifies the context code. The value is 1 if the ALT key is down; otherwise, it is 0.
30
Specifies 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
Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
Return Value
If code is less than zero, the hook procedure must return the value returned by CallNextHookEx.
If code is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.
Remarks
An application installs the hook procedure by specifying the WH_KEYBOARD hook type and a pointer to the hook procedure in a call to the SetWindowsHookEx function.
keyboardProc钩子过程是定义在应用程序中或是库文件中的回调函数,它有SetWindowsHookEx来使用。当应用程序中调用了GetMessage或是PeekMessage方法时并且处理的是键盘消息(WM_KEYUP或WM_KEYDOWN)HOOKPROC类型定义了一个指向回调函数的指针。从原型定义中我们可以看出:typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
它其实就是在WinUser.h中定义的一个函数指针
一个简单的回调函数的例子如下(键盘钩子为例)
[cpp]
// 钩子回调函数(钩子过程)
LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0 || nCode == HC_NOREMOVE)
//向下传递消息使得hook链上的其他函数可以处理该消息
return ::CallNextHookEx(g_hHook, nCode, wParam, lParam);
if (lParam & 0x40000000) // Check the previous key state
{
return ::CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
//将自定义消息发送到主窗口
//wParam 定义了虚拟键代码
//lParam 定义了按键数据
::PostMessage(g_hWnd, WM_KEYSTROKE, wParam, lParam);
return ::CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
// 钩子回调函数(钩子过程)
LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0 || nCode == HC_NOREMOVE)
//向下传递消息使得hook链上的其他函数可以处理该消息
return :