This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.lpr
116 lines (105 loc) · 2.41 KB
/
hook.lpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{$mode delphi}
library hook;
uses
Windows,
Messages;
const
WH_KEYBOARD_LL = 13;
ALTKEY = 32;
CTRLKEY = 0;
WINKEYHELPER = 1;
type
KBDLLHOOKSTRUCT = record
vkCode: dword;
scanCode: dword;
flags: dword;
time: dword;
dwExtraInfo: int64;
end;
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
var
HookHandle: Cardinal = 0;
WindowHandle: Cardinal = 0;
strict: boolean = false;
enabled: boolean = false;
function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
var
p: PKBDLLHOOKSTRUCT;
begin
p:=PKBDLLHOOKSTRUCT(lParam);
//Strict will block all keys, non-strict will block some shortcuts
if not(strict) then begin
if (wParam = WM_KEYDOWN)
or (wParam = WM_KEYUP)
or (wParam = WM_SYSKEYDOWN)
or (wParam = WM_SYSKEYUP) then begin
if (p.vkCode = VK_F4) and (p.flags = ALTKEY) then begin //Alt+F4
result:=1;
end
else if (p.vkCode = VK_ESCAPE) and (p.flags = ALTKEY) then begin //Alt+Esc
result:=1;
end
else if (p.vkCode = VK_ESCAPE) and (p.flags = CTRLKEY) then begin //Ctrl+Esc
result:=1;
end
else if (p.vkCode = VK_LWIN) and (p.flags = WINKEYHELPER) then begin //WinL
result:=1;
end
else if (p.vkCode = VK_RWIN) and (p.flags = WINKEYHELPER) then begin //WinR
result:=1;
end
else if (p.flags = ALTKEY) then begin //Alt
result:=1;
end
else begin
result:=CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;
end;
end
else begin
if (enabled) then begin
result:=1;
end
else begin
result:=CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;
end;
end;
function InstallHook(Hwnd: Cardinal; strictParam: boolean): Boolean; stdcall;
begin
Result := False;
if HookHandle = 0 then begin
HookHandle := SetWindowsHookEx(WH_KEYBOARD_LL, @KeyboardHookProc,
HInstance, 0);
WindowHandle := Hwnd;
Result := TRUE;
end;
if (strictParam) then begin
strict:=true;
enabled:=false;
end
else begin
strict:=false;
end;
end;
function UninstallHook: Boolean; stdcall;
begin
Result := UnhookWindowsHookEx(HookHandle);
HookHandle := 0;
end;
function ControlHook(mode: boolean): Boolean; stdcall;
begin
if (mode) then begin
enabled:=true;
end
else begin
enabled:=false;
end;
result:=true;
end;
exports
InstallHook,
UninstallHook,
ControlHook;
end.