-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSystem.MouseHookEvents.pas
More file actions
103 lines (83 loc) · 2.47 KB
/
System.MouseHookEvents.pas
File metadata and controls
103 lines (83 loc) · 2.47 KB
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
unit System.MouseHookEvents;
interface
uses
Winapi.Windows,
System.SysUtils;
type
TMouseLLHookStruct = record // PMouseLLHookStruct = ^TMouseLLHookStruct;
pt: TPoint;
mouseData: DWORD;
Flags: DWORD;
Time: DWORD;
dwExtraInfo: Cardinal;
end;
type
IMouseHookEvents = interface(IInvokable)
['{0D9FF1EB-B17D-492D-AED2-37AC21AAA4A1}']
function MouseHookEvent(AValue: TProc<TPoint, WPARAM>): IMouseHookEvents; overload;
function MouseHookEvent(): TProc<TPoint, WPARAM> overload;
function Start(): IMouseHookEvents;
function Stop(): IMouseHookEvents;
end;
TMouseHookEvents = class(TInterfacedObject, IMouseHookEvents)
strict private
FIsRecording: Boolean;
FOutDir: string;
private
class var MHook: Cardinal;
class var GlobalMouseHookEvent: TProc<TPoint, WPARAM>;
public
constructor Create();
destructor Destroy; override;
function MouseHookEvent(AValue: TProc<TPoint, WPARAM>): IMouseHookEvents; overload;
function MouseHookEvent(): TProc<TPoint, WPARAM> overload;
function Start(): IMouseHookEvents;
function Stop(): IMouseHookEvents;
end;
implementation
uses
Winapi.Messages;
{ TMouseHookEvents }
function LowLevelMouseHookProc(nCode: LongInt; WPARAM: WPARAM; lParam: lParam): LRESULT; stdcall;
var
info: ^TMouseLLHookStruct absolute lParam;
begin
Result := CallNextHookEx(TMouseHookEvents.MHook, nCode, WPARAM, lParam);
if Assigned(TMouseHookEvents.GlobalMouseHookEvent) then
begin
TMouseHookEvents.GlobalMouseHookEvent(TMouseLLHookStruct(info^).pt, WPARAM);
end;
end;
constructor TMouseHookEvents.Create;
begin
inherited Create();
GlobalMouseHookEvent := nil;
TMouseHookEvents.MHook := 0;
end;
destructor TMouseHookEvents.Destroy;
begin
if (TMouseHookEvents.MHook <> 0) then
begin
Stop;
end;
inherited;
end;
function TMouseHookEvents.Start: IMouseHookEvents;
begin
TMouseHookEvents.MHook := SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseHookProc, hInstance, 0);
end;
function TMouseHookEvents.Stop: IMouseHookEvents;
begin
UnhookWindowsHookEx(TMouseHookEvents.MHook);
TMouseHookEvents.MHook := 0;
end;
function TMouseHookEvents.MouseHookEvent: TProc<TPoint, WPARAM>;
begin
Result := GlobalMouseHookEvent;
end;
function TMouseHookEvents.MouseHookEvent(AValue: TProc<TPoint, WPARAM>): IMouseHookEvents;
begin
GlobalMouseHookEvent := AValue;
Result := Self;
end;
end.