用HOOK<br><br>unit HookUnit;<br><br>interface<br><br>uses<br> Windows, Messages;<br>var<br> hNextHookProc: HHook;//保存SetWindowsHookEx的返回值<br> procSaveExit: Pointer;<br> function KeyboardHookHandler(nCode: Integer; w_Param: WPARAM;<br> l_Param: LPARAM): LRESULT; stdcall;export;<br> function EnableHotKeyHook: BOOL; export;//安装钩子<br> function DisableHotKeyHook: BOOL; export;//卸载钩子<br> procedure HotKeyHookExit; far;<br><br>implementation<br><br>type<br> // Declaration from VC++ WINUSER.H<br> //<br> PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;<br> KBDLLHOOKSTRUCT = record<br> vkCode: DWORD;<br> scanCode: DWORD;<br> flags: DWORD;<br> time: DWORD;<br> dwExtraInfo: DWORD;<br> end;<br><br>const<br> LLKHF_ALTDOWN = KF_ALTDOWN shr 8;<br> WH_KEYBOARD_LL = 13;<br><br>function KeyboardHookHandler(nCode: Integer; w_Param: WPARAM;<br> l_Param: LPARAM): LRESULT; stdcall;<br>var<br> blEatKeystroke: Boolean;<br> p: PKBDLLHOOKSTRUCT;<br>begin<br> blEatKeystroke := False;<br> if nCode = HC_ACTION then<br> begin<br> case w_Param of<br> WM_KEYDOWN,<br> WM_SYSKEYDOWN,<br> WM_KEYUP,<br> WM_SYSKEYUP:<br> begin<br> p := PKBDLLHOOKSTRUCT(l_Param);<br> // 进行按键过滤<br> //在这里添加你要屏蔽的键值<br> blEatKeystroke :=<br> (((p^.vkCode = VK_TAB) and<br> ((p^.flags and LLKHF_ALTDOWN) <> 0)) or<br> ((p^.vkCode = VK_ESCAPE) and<br> ((p^.flags and LLKHF_ALTDOWN) <> 0)) or<br> ((p^.vkCode = VK_ESCAPE) and<br> ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or<br> ((p^.vkCode = VK_LWIN) or (p^.vkCode = VK_RWIN)) or<br> ((p^.vkCode = VK_DELETE) and<br> ((p^.flags and LLKHF_ALTDOWN) <> 0) and<br> ((GetKeyState(VK_CONTROL) and $8000) <> 0)));<br> end;<br> end; // End of Case<br> end;<br> if blEatKeystroke then<br> Result := 1 // 捕获这些组合键,按键消息由自己处理,必须返回 1<br> else<br> // 其他的按键,交由别的线程处理(过滤)<br> Result := CallNextHookEx(0, nCode, w_Param, l_Param);<br>end;<br><br>function EnableHotKeyHook: BOOL; export;<br>begin<br> Result := False;//初始化返回值<br> if hNextHookProc<>0 then//如果已经注册,直接退出<br> Exit;<br> hNextHookProc:=SetWindowsHookEx(WH_KEYBOARD_LL,KeyboardHookHandler,HInstance,0);<br><br>//注册hook<br> Result:=hNextHookProc <> 0;//通过返回值确定是否注册成功<br>end;<br><br>function DisableHotKeyHook: BOOL; export;<br>begin<br> if hNextHookProc<>0 then<br> begin<br> UnhookWindowshookEx(hNextHookProc);//解除Keyboard Hook<br> hNextHookProc:=0;//恢复标志<br> end;<br> Result:=hNextHookProc=0;//返回是否注销成功<br>end;<br><br>procedure HotKeyHookExit;<br>begin<br> if hNextHookProc<>0 then DisableHotKeyHook;//如果忘了解除HOOK,自动代理解除动作<br> ExitProc:=procSaveExit;<br>end;<br><br>end.<br><br>