unit hook11;
interface
uses
Windows,
SysUtils,
Classes,
Messages;
type
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
KBDLLHOOKSTRUCT = record
vkCode: DWORD;
ScanCode: DWORD;
Flags: DWORD;
Time: DWORD;
dwExtraInfo: DWORD;
end;
function InstKeyBoard: Boolean; stdcall;
function UnInstKeyBoard: Boolean; stdcall;
var
hhk: HHOOK;
//{$R *.res}
implementation
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
p: PKBDLLHOOKSTRUCT;
y: Integer;
begin
y:= 0;
if nCode = HC_ACTION then
begin
case WParam of
WM_KEYDOWN, WM_SYSKEYDOWN:
begin
p:= PKBDLLHOOKSTRUCT(Lparam);
//測試當 160(左 shift 鍵) 和 162(左 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 160 then if GetKeyState(162) < 0 then y:= 1;
//測試當 161(右 shift 鍵) 和 162(左 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 161 then if GetKeyState(162) < 0 then y:= 1;
//測試當 32(空格鍵) 和 162(左 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 32 then if GetKeyState(162) < 0 then y:= 1;
//測試當 160(左 shift 鍵) 和 163(右 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 160 then if GetKeyState(163) < 0 then y:= 1;
//測試當 161(右 shift 鍵) 和 163(右 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 161 then if GetKeyState(163) < 0 then y:= 1;
//測試當 32(空格?) 和 163(右 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 32 then if GetKeyState(163) < 0 then y:= 1;
end;
WM_KEYUP, WM_SYSKEYUP:
begin
p:= PKBDLLHOOKSTRUCT(Lparam);
//測試當 160(左 shift 鍵) 和 162(左 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 160 then if GetKeyState(162) < 0 then y:= 1;
//測試當 161(右 shift 鍵) 和 162(左 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 161 then if GetKeyState(162) < 0 then y:= 1;
//測試當 32(空格?) 和 162(左 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 32 then if GetKeyState(162) < 0 then y:= 1;
//測試當 160(左 shift 鍵) 和 163(右 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 160 then if GetKeyState(163) < 0 then y:= 1;
//測試當 161(右 shift 鍵) 和 163(右 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 161 then if GetKeyState(163) < 0 then y:= 1;
//測試當 32(空格鍵) 和 163(右 ctrl 鍵) 也按下的同時 觸發事件
if p^.vkCode = 32 then if GetKeyState(163) < 0 then y:= 1;
end;
end;
end;
if y = 1 then Result:= 1 else Result:= CallNextHookEx(hHk,nCode,WParam,LParam);
end;
function InstKeyBoard: Boolean; stdcall; export;
begin
if hhk <> 0 then
begin
Result:= False;
Exit;
end;
hhk:= SetWindowsHookEx(13, @HookProc, HINSTANCE, 0);//此處13換成其他信息類型,未發
//現可用的,需注意。
Result:= hhk <> 0;
end;
function UnInstKeyBoard: Boolean; stdcall; export;
begin
if hhk <> 0 then
begin
UnhookWindowshookEx(hhk);
hhk:= 0;
end;
Result:= hhk = 0;
end;
exports
InstKeyBoard,
UnInstKeyBoard;
begin
end.
勾子用法實例
最新推荐文章于 2023-10-24 21:02:29 发布