钩子教程 - 原理(十六) : KeyboardProc
Posted 勿在浮沙筑高台
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了钩子教程 - 原理(十六) : KeyboardProc相关的知识,希望对你有一定的参考价值。
原文地址:http://www.zdexe.com/program/201004/590.html
方法11 : KeyboardProc Function
The KeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed.
KeyboardProc钩子子程是同SetWindowsHookEx方法一起使用的、用户定义的或者库定义的回调函数。无论什么时候,当应用程序调用GetMessage 或者PeekMessage方法时,系统都调用该方法,将有一个键盘消息(WM_KEYUP或者WM_KEYDOWN)被处理。
The HOOKPROC type defines a pointer to this callback function.KeyboardProc is a placeholder for the application-defined or library-defined function name.
HOOKPROC类型定义了指向该回调函数的指针。KeyboardProc是程序定义的或者库定义的方法名字。
Syntax语法
LRESULT CALLBACK KeyboardProc(
int code,
WPARAM wParam,
LPARAM lParam
);
Parameters参数
code : [in] Specifies a code the hook procedure uses to determine how to process the message. If code is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. This parameter can be one of the following values.
指定钩子子程使用的代码,来决定如何处理该消息。如果code小于0,钩子子程必须将该消息传递给CallNextHookEx方法,自己不进行任何进一步的处理,并且返回由CallNextHookEx方法返回的返回值。该参数可以是以下值之一:
1.HC_ACTION :The wParam and lParam parameters contain information about a keystroke message.
参数 wParam 和 lParam 包含有键盘敲击消息的信息。
2.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和lParam包含有键盘敲击消息的信息,键盘敲击消息还没有被从消息队列中移除。(应用程序调用PeekMessage方法,同时指定PM_NOREMOVE标志。)
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. 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.
指定扫描代码。该值依赖于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.
指定该值是否是一个扩展键,例如功能键或者数字键盘上的键。如果是扩展键,该值为1,否则为0。
25-28 : Reserved.
保留
29 :Specifies the context code. The value is 1 if the ALT key is down; otherwise, it is 0.
指定上下文代码。如果ALT键被按下,该值为1,否则为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.
指定前面的键状态。如果在消息发出之前该键被按下,值为1;如果键弹起,值为0。
31 : Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
指定转换状态。如果键正在被按下,该值为0,如果正在被释放,则为1。
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.
如果code小于0,钩子子程必须返回由CallNextHookEx返回的返回值。如果code大于等于0,表示钩子子程没有处理该消息,强烈要求调用CallNextHookEx方法,并返回由它返回的返回值;否则,其它已经安装有WH_KEYBOARD钩子的应用程序将收不到钩子通知,可能导致行为的错误。如果钩子子程处理了该消息,可能返回一个非0值,用来阻止系统将该消息传递给钩子链表中的其它钩子或者目的窗体程序。
Remarks 备注
An application installs the hook procedure by specifying theWH_KEYBOARD hook type and a pointer to the hook procedure in a call to the SetWindowsHookEx function.
应用程序通过下面的方法安装该钩子子程: 指定WH_KEYBOARD钩子类型;指定在调用SetWindowsHookEx的方法中的一个指向钩子子程的指针。
以上是关于钩子教程 - 原理(十六) : KeyboardProc的主要内容,如果未能解决你的问题,请参考以下文章
钩子教程 - 原理(十五) : JournalRecordProc