提示: 如果要设置系统级(全局)钩子, 钩子函数必须封装在 DLL 中.
HHOOK hmyHook = {返回钩子的句柄; 0 表示失败}
SetWindowsHookEx( int idHook, {钩子类型} HOOKPROC lpfn, {函数指针} HINSTANCE hMod, {包含钩子函数的模块(EXE、DLL)句柄; 一般是 HInstance; 如果是当前线程这里可以是 0} DWORD dwThreadId {关联的线程; 可用 GetCurrentThreadId 获取当前线程; 0 表示是系统级钩子} )//钩子类型 idHook 选项: WH_MSGFILTER = -1; {线程级; 截获用户与控件交互的消息} WH_JOURNALRECORD = 0; {系统级; 记录所有消息队列从消息队列送出的输入消息, 在消息从队列中清除时发生; 可用于宏记录} WH_JOURNALPLAYBACK = 1; {系统级; 回放由 WH_JOURNALRECORD 记录的消息, 也就是将这些消息重新送入消息队列} WH_KEYBOARD = 2; {系统级或线程级; 截获键盘消息} WH_GETMESSAGE = 3; {系统级或线程级; 截获从消息队列送出的消息} WH_CALLWNDPROC = 4; {系统级或线程级; 截获发送到目标窗口的消息, 在 SendMessage 调用时发生} WH_CBT = 5; {系统级或线程级; 截获系统基本消息, 譬如: 窗口的创建、激活、关闭、最大最小化、移动等等} WH_SYSMSGFILTER = 6; {系统级; 截获系统范围内用户与控件交互的消息} WH_MOUSE = 7; {系统级或线程级; 截获鼠标消息} WH_HARDWARE = 8; {系统级或线程级; 截获非标准硬件(非鼠标、键盘)的消息} WH_DEBUG = 9; {系统级或线程级; 在其他钩子调用前调用, 用于调试钩子} WH_SHELL = 10; {系统级或线程级; 截获发向外壳应用程序的消息} WH_FOREGROUNDIDLE = 11; {系统级或线程级; 在程序前台线程空闲时调用} WH_CALLWNDPROCRET = 12; {系统级或线程级; 截获目标窗口处理完毕的消息, 在 SendMessage 调用后发生}在网上得到了下面的资料:There are 14 types of hooks:
- WH_CALLWNDPROC called when SendMessage is called
- WH_CALLWNDPROCRET called when SendMessage returns
- WH_GETMESSAGE called when GetMessage or PeekMessage is called
- WH_KEYBOARD called when GetMessage or PeekMessage retrieves WM_KEYUP or WM_KEYDOWN from the message queue
- WH_MOUSE called when GetMessage or PeekMessage retrieves a mouse message from the message queue
- WH_HARDWARE called when GetMessage or PeekMessage retrieves some hardware message that is not related to keyboard or mouse.
- WH_MSGFILTER called when a dialog box, menu or scrollbar is about to process a message. This hook is local. It's specifically for those objects which have their own internal message loops.
- WH_SYSMSGFILTER same as WH_MSGFILTER but system-wide
- WH_JOURNALRECORD called when Windows retrieves message from the hardware input queue
- WH_JOURNALPLAYBACK called when an event is requested from the system's hardware input queue.
- WH_SHELL called when something interesting about the shell occurs such as when the task bar needs to redraw its button.
- WH_CBT used specifically for computer-based training (CBT).
- WH_FOREGROUNDIDLE used internally by Windows. Little use for general applications
- WH_DEBUG used to debug the hooking procedure
参考:
http://old.blog.edu.cn/user4/pafone/archives/2007/1660551.shtml