-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.RawInput.pas
More file actions
197 lines (155 loc) · 5.52 KB
/
Copy pathMaxLogic.RawInput.pas
File metadata and controls
197 lines (155 loc) · 5.52 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
unit MaxLogic.RawInput;
interface
{
Version: 1
}
uses
windows, classes, sysUtils, controls, types, messages,
CnRawInput, diagnostics, forms, maxWndSubClassing;
Type
TRawMouseEvent = procedure(sender: Tobject; const RawMouse: RawMouse; FromMouse: THandle) of object;
TmaxRawInput = class
private
FOnRawKeyDown: TOnRawKeyDown;
FOnRawKeyUp: TOnRawKeyUp;
fLastMouseMessage,
fLastKeyboardMessage: TStopWatch;
FOnAnyKeyOrMessageEvent: TThreadProcedure;
FOnRawMouseEvent: TRawMouseEvent;
fForm: TForm;
fWndSubClass: TWndSubClass;
fData:TBytes;
procedure SetOnAnyKeyOrMessageEvent(const Value: TThreadProcedure);
procedure SetOnRawMouseEvent(const Value: TRawMouseEvent);
function GetElapsedMillisecondsSinceLastKeyEvent: int64;
function GetElapsedMillisecondsSinceLastMouseEvent: int64;
function GetElapsedMillisecondsSinceLastKeyOrMouseEvent: int64;
procedure StartUp;
protected
procedure WMInput(var Message: TMessage; var PrevendDefaultHandler: boolean); // message WM_INPUT;
public
constructor Create;
destructor Destroy; override;
property OnRawKeyDown: TOnRawKeyDown read FOnRawKeyDown write FOnRawKeyDown;
property OnRawKeyUp: TOnRawKeyUp read FOnRawKeyUp write FOnRawKeyUp;
// see details of the mouse item here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645578.aspx
property OnRawMouseEvent: TRawMouseEvent read FOnRawMouseEvent write SetOnRawMouseEvent;
// if you just need to know if there was a key r mouse event. just like a ping to let you know
property OnAnyKeyOrMessageEvent: TThreadProcedure read FOnAnyKeyOrMessageEvent write SetOnAnyKeyOrMessageEvent;
property ElapsedMillisecondsSinceLastKeyEvent: int64 read GetElapsedMillisecondsSinceLastKeyEvent;
property ElapsedMillisecondsSinceLastMouseEvent: int64 read GetElapsedMillisecondsSinceLastMouseEvent;
property ElapsedMillisecondsSinceLastKeyOrMouseEvent: int64 read GetElapsedMillisecondsSinceLastKeyOrMouseEvent;
end;
implementation
uses
math;
constructor TmaxRawInput.Create;
begin
inherited;
fForm := TForm.Create(nil);
fWndSubClass := TWndSubClass.Create(fForm);
fWndSubClass.OnMessage := self.WMInput;
fWndSubClass.HookedWnd := fForm.handle;
fLastMouseMessage := TStopWatch.startnew;
fLastKeyboardMessage := TStopWatch.startnew;
StartUp;
end;
procedure TmaxRawInput.StartUp;
var
RID: array [0 .. 1] of TRawInputDevice;
begin
inherited;
// In this sample, an application wants raw input from the keyboard and mouse but wants to ignore legacy window messages (which would come from the same keyboard and mouse).
RID[0].usUsagePage := $01;
RID[0].usUsage := $02;
RID[0].dwFlags := RIDEV_INPUTSINK;
RID[0].hwndTarget := fForm.handle;
RID[1].usUsagePage := $01;
RID[1].usUsage := $06;
RID[1].dwFlags := RIDEV_INPUTSINK;
RID[1].hwndTarget := fForm.handle;
if not RegisterRawInputDevices(@RID, 2, sizeof(RID[0])) then
raise Exception.Create('RegisterRawInputDevices error!');
end;
procedure TmaxRawInput.WMInput(var Message: TMessage; var PrevendDefaultHandler: boolean);
var
Ri: tagRAWINPUT;
Size: UINT;
begin
if (Message.Msg = WM_INPUT) then
// if (Message.WParam and $FF) in [RIM_INPUT, RIM_INPUTSINK] then
begin
// get size
Size := sizeof(tagRAWINPUT);
GetRawInputData(
HRAWINPUT(Message.LParam),
RID_INPUT, nil, Size, sizeof(RAWINPUTHEADER));
if size<>0 then
begin
if size>length(fData) then
SetLength(fdata, Size);
{ ri:=default(tagRAWINPUT);
Ri.header.dwSize := sizeof(RAWINPUTHEADER);
Move(ri, data[0],
min(sizeOf(ri), length(data)));}
if GetRawInputData(
HRAWINPUT(Message.LParam),
RID_INPUT, @fdata[0], Size, sizeof(RAWINPUTHEADER)) = Size then
begin
move(fdata[0], ri,
min(sizeOf(ri), size));
if (Ri.header.dwType = RIM_TYPEKEYBOARD) then
begin
case Ri.keyboard.Message of
WM_KEYDOWN, WM_KEYUP:
begin
fLastKeyboardMessage := TStopWatch.startnew;
if assigned(FOnAnyKeyOrMessageEvent) then
FOnAnyKeyOrMessageEvent();
end;
end;
if (Ri.keyboard.Message = WM_KEYDOWN) and assigned(FOnRawKeyDown) then
FOnRawKeyDown(self, Ri.keyboard.VKey, Ri.header.hDevice)
else if (Ri.keyboard.Message = WM_KEYUP) and assigned(FOnRawKeyUp) then
FOnRawKeyUp(self, Ri.keyboard.VKey, Ri.header.hDevice);
end else
if (Ri.header.dwType = RIM_TYPEMOUSE) then
begin
fLastMouseMessage := TStopWatch.startnew;
if assigned(FOnAnyKeyOrMessageEvent) then
FOnAnyKeyOrMessageEvent();
if assigned(FOnRawMouseEvent) then
FOnRawMouseEvent(self, Ri.mouse, Ri.header.hDevice);
end;
end;
end;
end;
end;
destructor TmaxRawInput.Destroy;
begin
fWndSubClass.free;
fForm.free;
inherited;
end;
procedure TmaxRawInput.SetOnAnyKeyOrMessageEvent(const Value: TThreadProcedure);
begin
FOnAnyKeyOrMessageEvent := Value;
end;
procedure TmaxRawInput.SetOnRawMouseEvent(const Value: TRawMouseEvent);
begin
FOnRawMouseEvent := Value;
end;
function TmaxRawInput.GetElapsedMillisecondsSinceLastKeyEvent: int64;
begin
result := fLastKeyboardMessage.ElapsedMilliseconds
end;
function TmaxRawInput.GetElapsedMillisecondsSinceLastMouseEvent: int64;
begin
result := fLastMouseMessage.ElapsedMilliseconds;
end;
function TmaxRawInput.GetElapsedMillisecondsSinceLastKeyOrMouseEvent: int64;
begin
result := min(ElapsedMillisecondsSinceLastKeyEvent,
ElapsedMillisecondsSinceLastMouseEvent);
end;
end.