-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMouse.Hook.pas
182 lines (154 loc) · 4.88 KB
/
Mouse.Hook.pas
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
unit Mouse.Hook;
interface
uses
Winapi.Windows, Winapi.Messages,
System.Generics.Collections,
System.SysUtils, System.Classes, System.SyncObjs;
const
WM_MOUSEWHEELHOOK = WM_USER + 400;
WM_MOUSEHWHEELHOOK = WM_USER + 401;
type
TMouseHook = class
private const
WH_MOUSE_LL = 14;
HC_ACTION = 0;
LLMHF_INJECTED = $00000001;
LLMHF_LOWER_IL_INJECTED = $00000002;
WM_HOOK = WM_USER + 256;
WM_UNHOOK = WM_USER + 257;
private type
tagMSLLHOOKSTRUCT = record
pt: TPoint;
mouseData: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: ULONG_PTR;
end;
MSLLHOOKSTRUCT = tagMSLLHOOKSTRUCT;
PMSLLHOOKSTRUCT = ^MSLLHOOKSTRUCT;
LPMSLLHOOKSTRUCT = ^MSLLHOOKSTRUCT;
private
class var HookHandle: HHOOK;
class var HThread: THandle;
class var IdThread: DWORD;
class var FRecipients: TList<THandle>;
class constructor Create;
class destructor Destroy;
class function Hook: boolean; static;
class function UnHook: boolean; static;
class function LowLevelMouseProc(nCode: Integer; wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; static;
class function ThreadExecute(lpParameter: LPVOID): DWORD; stdcall; static;
public
class procedure RegisterHook(hRecipient: THandle);
class procedure UnregisterHook(hRecipient: THandle);
end;
implementation
{ TMouseHook }
class procedure TMouseHook.RegisterHook(hRecipient: THandle);
begin
if FRecipients.Contains(hRecipient) then Exit;
FRecipients.Add(hRecipient);
if FRecipients.Count = 1 then Hook;
end;
class procedure TMouseHook.UnregisterHook(hRecipient: THandle);
begin
if not FRecipients.Contains(hRecipient) then Exit;
FRecipients.Remove(hRecipient);
if FRecipients.Count = 0 then UnHook;
end;
class function TMouseHook.Hook: Boolean;
var
ThreadAvalibleEvent: TEvent;
begin
if HThread = 0 then
begin
ThreadAvalibleEvent := TEvent.Create;
HThread := CreateThread(nil, 0, @ThreadExecute, @ThreadAvalibleEvent, 0, IdThread);
SetThreadPriority(HThread, THREAD_PRIORITY_TIME_CRITICAL);
ThreadAvalibleEvent.WaitFor;
ThreadAvalibleEvent.Free;
end;
Result:= PostThreadMessage(IdThread, WM_HOOK, 0, 0);
end;
class function TMouseHook.UnHook: Boolean;
begin
Result:= PostThreadMessage(IdThread, WM_UNHOOK, 0, 0);
end;
class function TMouseHook.LowLevelMouseProc(nCode: Integer; wParam: WPARAM;
lParam: LPARAM): LRESULT;
var
WheelMessage: Winapi.Windows.WPARAM;
PMsLl: LPMSLLHOOKSTRUCT;
WheelDelta: SHORT;
KeyState: Word;
wP: Winapi.Windows.WPARAM;
lP: Winapi.Windows.LPARAM;
hRecipient: THandle;
begin
if nCode <> HC_ACTION then
Exit(CallNextHookEx(HookHandle, nCode, wParam, lParam));
case wParam of
WM_MOUSEWHEEL, WM_MOUSEHWHEEL: begin
case wParam of
WM_MOUSEHWHEEL: WheelMessage := WM_MOUSEHWHEELHOOK;
else WheelMessage := WM_MOUSEWHEELHOOK;
end;
PMsLl := LPMSLLHOOKSTRUCT(lParam);
WheelDelta := HiWord(PMsLl^.mouseData);
KeyState:= 0;
if HiByte(GetKeyState(VK_SHIFT)) <> 0 then KeyState := KeyState or MK_SHIFT;
if HiByte(GetKeyState(VK_LBUTTON)) <> 0 then KeyState := KeyState or MK_LBUTTON;
if HiByte(GetKeyState(VK_RBUTTON)) <> 0 then KeyState := KeyState or MK_RBUTTON;
if HiByte(GetKeyState(VK_CONTROL)) <> 0 then KeyState := KeyState or MK_CONTROL;
if HiByte(GetKeyState(VK_MBUTTON)) <> 0 then KeyState := KeyState or MK_MBUTTON;
wP := MakeWParam(KeyState, WheelDelta);
lP := MakeLParam(PMsLl^.pt.X, PMsLl^.pt.Y);
for hRecipient in FRecipients do
PostMessage(hRecipient, WheelMessage, wP, lP);
end;
end;
Exit(CallNextHookEx(HookHandle, nCode, wParam, lParam));
end;
class function TMouseHook.ThreadExecute(lpParameter: LPVOID): DWORD;
var
Msg: TMsg;
bRet: BOOL;
begin
Result := 0;
PeekMessage(Msg, HWND(-1), 0, 0, PM_NOREMOVE);
TEvent(lpParameter^).SetEvent;
try
repeat
bRet := GetMessage(Msg, HWND(-1), 0, 0);
if LONG(bRet) <> -1 then begin
case Msg.message of
WM_HOOK: begin
HookHandle:= SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseProc, HInstance, 0);
end;
WM_UNHOOK: begin
if HookHandle <> 0 then
if UnhookWindowsHookEx(HookHandle) then HookHandle:= 0;
end;
end;
DispatchMessage(Msg);
end;
until (not bRet);
finally
if HookHandle <> 0 then
if UnhookWindowsHookEx(HookHandle) then HookHandle:= 0;
end;
end;
class constructor TMouseHook.Create;
begin
HookHandle := 0;
HThread := 0;
FRecipients := TList<THandle>.Create;
end;
class destructor TMouseHook.Destroy;
begin
UnHook;
PostThreadMessage(IdThread, WM_QUIT, 0, 0);
FreeAndNil(FRecipients);
end;
end.