本文整理自网络,侵删。
{*******************************************************************************
XOtecExpress Visual Component Library [陆岛工作室]
Copyright (c) 2008 XOtec Studio. [PengJunli]
By: PengJunLi Build: 2008-06
E-mail: iinsnian@126.com XOtec@vip.QQ.com QQ:442801172
*******************************************************************************}
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes;
type
tagKBDLLHOOKSTRUCT = packed record
vkCode: DWORD;
ScanCode: DWORD;
Flags: DWORD;
Time: DWORD;
dwExtraInfo: DWORD;
end;
KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
const
WH_KEYBOARD_LL = 13;
LLKHF_ALTDOWN = $20;
//采用钩子的方法屏蔽系统按键
function DisableTaskKeys(Disable: Boolean): Boolean;
implementation
var
hhkLowLevelKybd: HHOOK;
{ LowLevelKeyboardProc }
function LowLevelKeyboardProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
fEatKeystroke: BOOL;
p: PKBDLLHOOKSTRUCT;
begin
Result := 0;
fEatKeystroke := FALSE;
p := PKBDLLHOOKSTRUCT (lParam);
if (nCode = HC_ACTION) then //nCode值为HC_ACTION时表示WParam和LParam参数包涵了按键消息
begin
//拦截按键消息并测试是否是Ctrl+Esc、Alt+Tab、和Alt+Esc功能键。
case wParam of
WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP:
fEatKeystroke :=
((p.vkCode = VK_TAB) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or // Alt+Tab
((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0))or //
(p.vkCode = VK_Lwin) or (p.vkCode = VK_Rwin) or (p.vkCode = VK_apps) or //屏蔽WIN按键
((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or
((p.vkCode = VK_F4) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or
((p.vkCode = VK_SPACE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) OR
(((p.vkCode = VK_CONTROL) and (P.vkCode = LLKHF_ALTDOWN and p.flags) and (P.vkCode = VK_Delete)));
end;
end;
if fEatKeystroke = True then
Result := 1;
if nCode <> 0 then
Result := CallNextHookEx(0, nCode, wParam, lParam);
end;
{ DisableTaskKeys }
function DisableTaskKeys(Disable: Boolean): Boolean;
begin
Result := False;
if (hhkLowLevelKybd = 0) and Disable then
begin
hhkLowLevelKybd :=SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc, Hinstance, 0); //设置钩子
Result := hhkLowLevelKybd<>0; // 返回设置成功
end else if not Disable and (hhkLowLevelKybd<>0) then
begin
if UnhookWindowsHookEx(hhkLowLevelKybd) then //卸载键盘钩子
begin
Result := True;
hhkLowLevelKybd := 0;
end;
end;
end;
end.
相关阅读 >>
Delphi如何获取系统服务(即service服务程序)列表
Delphi 手机app打开一个安卓系统支持的文件,比如 pdf
Delphi types of actual and formal var parameters must be identical
更多相关阅读请进入《Delphi》频道 >>