|
{注意:这个结构在Windows NT 4 sp3以上系统中才能使用}<br/> type<br/> tagKBDLLHOOKSTRUCT = packed record<br/> vkCode: DWORD;<br/> scanCode: DWORD;<br/> flags: DWORD;<br/> time: DWORD;<br/> dwExtraInfo: DWORD;<br/> end;<br/> KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;<br/> PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;<br/> const WH_KEYBOARD_LL = 13;<br/> const LLKHF_ALTDOWN = $20;</p><p> var<br/> hhkLowLevelKybd: HHOOK;</p><p> function LowLevelKeyboardProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM)RESULT; stdcall;<br/> procedure hookstar; //设置钩子<br/> procedure hookend; //注消钩子</p><p> <br/> var<br/> hhkLowLevelKybd: HHOOK;</p><p> implementation</p><p> <br/> function LowLevelKeyboardProc(nCode: Integer;<br/> WParam: WPARAM;LParam: LPARAM)RESULT; stdcall;<br/> var<br/> fEatKeystroke: BOOL;<br/> p: PKBDLLHOOKSTRUCT;<br/> begin<br/> Result := 0;<br/> fEatKeystroke := FALSE;<br/> p := PKBDLLHOOKSTRUCT (lParam);<br/> if (nCode = HC_ACTION) then<br/> begin<br/> //拦截按键消息并测试是否是Ctrl+Esc、Alt+Tab、和Alt+Esc功能键。<br/> case wParam of<br/> WM_KEYDOWN,<br/> WM_SYSKEYDOWN,<br/> WM_KEYUP,<br/> WM_SYSKEYUP:<br/> fEatKeystroke :=<br/> ((p.vkCode = VK_TAB) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or // Alt+Tab<br/> ((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0))or //<br/> (p.vkCode = VK_Lwin) or (p.vkCode = VK_Rwin)or (p.vkCode = VK_apps) or //上面是屏蔽WIN按键<br/> //((p.vkCode = VK_CONTROL) and (P.vkCode = LLKHF_ALTDOWN) and (P.vkCode = VK_Delete)) or<br/> ((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or<br/> ((p.vkCode = VK_F4) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or<br/> ((p.vkCode = VK_SPACE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) OR<br/> (((p.vkCode = VK_CONTROL) and (P.vkCode = LLKHF_ALTDOWN and p.flags) and (P.vkCode = VK_Delete))) //AND (p.flags = true) ;<br/> end;<br/> end;</p><p> if fEatKeystroke = True then<br/> Result := 1;<br/> if nCode <> 0 then<br/> Result := CallNextHookEx(0, nCode, wParam, lParam);<br/> end;<br/> //----------------------回调函数结束-----------------------------------------</p><p> procedure hookstar; //设置钩子<br/> begin<br/> //设置键盘钩子<br/> if hhkLowLevelKybd = 0 then<br/> begin<br/> hhkLowLevelKybd := SetWindowsHookExW(WH_KEYBOARD_LL,<br/> LowLevelKeyboardProc,<br/> Hinstance,<br/> 0);<br/> if hhkLowLevelKybd <> 0 then<br/> //MessageBox(0, '键盘钩子设置成功!', '提示', MB_OK)<br/> else<br/> MessageBox(0, '键盘钩子设置失败!', '提示', MB_OK);<br/> end<br/> else<br/> //MessageBox(Handle, '键盘钩子已设置!', '提示', MB_OK);<br/> end;<br/> //---------------------设置钩子完成-----------------</p><p> procedure hookend;//卸载键盘钩子<br/> begin<br/> if hhkLowLevelKybd <> 0 then<br/> if UnhookWindowsHookEx(hhkLowLevelKybd) <> False then<br/> begin<br/> //MessageBox(0, '系统热键恢复使用!', '提示', MB_OK);<br/> hhkLowLevelKybd := 0;<br/> end<br/> else<br/> //MessageBox(Handle, '键盘钩子卸载失败!', '提示', MB_OK)<br/> else<br/> //MessageBox(Handle, '没有发现键盘钩子!', '提示', MB_OK);<br/> end;</p><p> |
|