-
Notifications
You must be signed in to change notification settings - Fork 2
/
RawInputKeyboard.pas
696 lines (585 loc) · 28.1 KB
/
RawInputKeyboard.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
RawInput managing library
Keyboard processing
©František Milt 2018-10-22
Version 0.9.3
Dependencies:
AuxTypes - github.com/ncs-sniper/Lib.AuxTypes
AuxClasses - github.com/ncs-sniper/Lib.AuxClasses
BitOps - github.com/ncs-sniper/Lib.BitOps
MulticastEvent - github.com/ncs-sniper/Lib.MulticastEvent
WndAlloc - github.com/ncs-sniper/Lib.WndAlloc
WinRawInput - github.com/ncs-sniper/Lib.WinRawInput
BitVector - github.com/ncs-sniper/Lib.BitVector
UtilityWindow - github.com/ncs-sniper/Lib.UtilityWindow
DefRegistry - github.com/ncs-sniper/Lib.DefRegistry
StrRect - github.com/ncs-sniper/Lib.StrRect
* SimpleCPUID - github.com/ncs-sniper/Lib.SimpleCPUID
SimpleCPUID might not be needed, see BitOps library for details.
===============================================================================}
unit RawInputKeyboard;
{$INCLUDE 'RawInput_defs.inc'}
interface
uses
Windows, Classes, Contnrs,
WinRawInput, BitVector,
RawInputCommon;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboard }
{------------------------------------------------------------------------------}
{==============================================================================}
const
VK_NUMPADENTER = $0100;
type
TKeyboardShiftStates = set of (kssControl,kssLControl,kssRControl,
kssAlt,kssLAlt,kssRAlt,
kssShift,kssLShift,kssRShift);
TKeyboardIndicators = set of (kiNumLock,kiCapsLock,kiScrollLock);
TRawKeyboardEvent = procedure(Sender: TObject; Input: TRawKeyboard) of object;
TVirtualKeyEvent = procedure(Sender: TObject; VirtualKeyCode: USHORT) of object;
{==============================================================================}
{ TRawInputKeyboard - declaration }
{==============================================================================}
TRawInputKeyboard = class(TRawInputProcessingObject)
private
fDoInputCorrection: Boolean;
fDiscernSides: Boolean;
fAdditionalVKeys: Boolean;
fKeyStates: TBitVector;
fOnRawKeyboard: TRawKeyboardEvent;
fOnBeforeKeyPress: TVirtualKeyEvent;
fOnAfterKeyPress: TVirtualKeyEvent;
fOnBeforeKeyRelease: TVirtualKeyEvent;
fOnAfterKeyRelease: TVirtualKeyEvent;
fOnKeyPress: TVirtualKeyEvent;
fOnKeyRelease: TVirtualKeyEvent;
protected
procedure ProcessRawInputSpecific(RawInput: PRawInput); override;
Function InputCorrection(var Input: RawKeyboard): Boolean; virtual;
procedure ProcessKeyPress(VirtualKeyCode: USHORT); virtual;
procedure ProcessKeyRelease(VirtualKeyCode: USHORT); virtual;
procedure DoRawKeyboard(Sender: TObject; Input: TRawKeyboard); virtual;
procedure DoBeforeKeyPress(Sender: TObject; VirtualKeyCode: USHORT); virtual;
procedure DoAfterKeyPress(Sender: TObject; VirtualKeyCode: USHORT); virtual;
procedure DoBeforeKeyRelease(Sender: TObject; VirtualKeyCode: USHORT); virtual;
procedure DoAfterKeyRelease(Sender: TObject; VirtualKeyCode: USHORT); virtual;
public
class Function GetVirtualKeyName(VirtualKeyCode: USHORT; NumberForUnknown: Boolean = True): String; virtual;
class Function ShiftStatesToStr(ShiftStates: TKeyboardShiftStates; const Separator: String = ' '; DiscernSides: Boolean = False): String; virtual;
constructor Create(DeviceInfo: PDeviceListItem = nil);
destructor Destroy; override;
procedure Invalidate; override;
Function GetShiftStates: TKeyboardShiftStates; virtual;
Function GetIndicators: TKeyboardIndicators; virtual;
property DoInputCorrection: Boolean read fDoInputCorrection write fDoInputCorrection;
property DiscernSides: Boolean read fDiscernSides write fDiscernSides;
property AdditionalVirtualKeys: Boolean read fAdditionalVKeys write fAdditionalVKeys;
property KeyStates: TBitVector read fKeyStates;
property OnRawKeyboard: TRawKeyboardEvent read fOnRawKeyboard write fOnRawKeyboard;
property OnBeforeKeyPress: TVirtualKeyEvent read fOnBeforeKeyPress write fOnBeforeKeyPress;
property OnAfterKeyPress: TVirtualKeyEvent read fOnAfterKeyPress write fOnAfterKeyPress;
property OnBeforeKeyRelease: TVirtualKeyEvent read fOnBeforeKeyRelease write fOnBeforeKeyRelease;
property OnAfterKeyRelease: TVirtualKeyEvent read fOnAfterKeyRelease write fOnAfterKeyRelease;
property OnKeyPress: TVirtualKeyEvent read fOnKeyPress write fOnKeyPress;
property OnKeyRelease: TVirtualKeyEvent read fOnKeyRelease write fOnKeyRelease;
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMaster }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputKeyboardMaster - declaration }
{==============================================================================}
TRawInputKeyboardMaster = class(TRawInputKeyboard)
private
fDevices: TObjectList;
fOnDevicesChange: TNotifyEvent;
Function GetDeviceCount: Integer;
Function GetDevice(Index: Integer): TRawInputKeyboard;
protected
fOnUnknownDeviceIntercepted: TUnknownDeviceEvent;
procedure ProcessRawInputSpecific(RawInput: PRawInput); override;
Function AddDeviceInternal(DeviceInfo: PDeviceListItem): Integer; virtual;
procedure DeleteDeviceInternal(Index: Integer); virtual;
public
constructor Create;
destructor Destroy; override;
procedure Invalidate; override;
Function IndexOfDevice(DeviceHandle: THandle): Integer; virtual;
property Devices[Index: Integer]: TRawInputKeyboard read GetDevice; default;
property DeviceCount: Integer read GetDeviceCount;
property OnDevicesChange: TNotifyEvent read fOnDevicesChange write fOnDevicesChange;
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardDeviceInternal }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputKeyboardDeviceInternal - declaration }
{==============================================================================}
TRawInputKeyboardDeviceInternal = class(TRawInputKeyboard)
private
fOnRawKeyboardInternal: TRawKeyboardEvent;
fOnBeforeKeyPressInternal: TVirtualKeyEvent;
fOnAfterKeyPressInternal: TVirtualKeyEvent;
fOnBeforeKeyReleaseInternal: TVirtualKeyEvent;
fOnAfterKeyReleaseInternal: TVirtualKeyEvent;
protected
procedure DoRawKeyboard(Sender: TObject; Input: TRawKeyboard); override;
procedure DoBeforeKeyPress(Sender: TObject; VirtualKeyCode: USHORT); override;
procedure DoAfterKeyPress(Sender: TObject; VirtualKeyCode: USHORT); override;
procedure DoBeforeKeyRelease(Sender: TObject; VirtualKeyCode: USHORT); override;
procedure DoAfterKeyRelease(Sender: TObject; VirtualKeyCode: USHORT); override;
public
property OnRawKeyboardInternal: TRawKeyboardEvent read fOnRawKeyboardInternal write fOnRawKeyboardInternal;
property OnBeforeKeyPressInternal: TVirtualKeyEvent read fOnBeforeKeyPressInternal write fOnBeforeKeyPressInternal;
property OnAfterKeyPressInternal: TVirtualKeyEvent read fOnAfterKeyPressInternal write fOnAfterKeyPressInternal;
property OnBeforeKeyReleaseInternal: TVirtualKeyEvent read fOnBeforeKeyReleaseInternal write fOnBeforeKeyReleaseInternal;
property OnAfterKeyReleaseInternal: TVirtualKeyEvent read fOnAfterKeyReleaseInternal write fOnAfterKeyReleaseInternal;
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMasterInternal }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputKeyboardMasterInternal - declaration }
{==============================================================================}
TRawInputKeyboardMasterInternal = class(TRawInputKeyboardMaster)
public
Function AddDevice(DeviceInfo: PDeviceListItem; AssignCallbacks: Boolean = False): Integer; virtual;
Function RemoveDevice(DeviceHandle: THandle): Integer; virtual;
procedure DeleteDevice(Index: Integer); virtual;
property OnUnknownDeviceIntercepted: TUnknownDeviceEvent read fOnUnknownDeviceIntercepted write fOnUnknownDeviceIntercepted;
end;
implementation
uses
SysUtils, StrRect;
{$IF not Declared(MAPVK_VK_TO_VSC)}
const
MAPVK_VK_TO_VSC = 0;
{$IFEND}
{$IF not Declared(MAPVK_VSC_TO_VK_EX)}
const
MAPVK_VSC_TO_VK_EX = 3;
{$IFEND}
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboard }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputKeyboard - implementation }
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboard - protected methods }
{------------------------------------------------------------------------------}
procedure TRawInputKeyboard.ProcessRawInputSpecific(RawInput: PRawInput);
begin
DoRawKeyboard(Self,RawInput^.keyboard);
If fDoInputCorrection then
If not InputCorrection(RawInput^.keyboard) then Exit;
If (RawInput^.keyboard.Flags and RI_KEY_BREAK) <> 0 then
ProcessKeyRelease(RawInput^.keyboard.VKey)
else
ProcessKeyPress(RawInput^.keyboard.VKey);
end;
//------------------------------------------------------------------------------
Function TRawInputKeyboard.InputCorrection(var Input: RawKeyboard): Boolean;
var
Flag_E0: Boolean;
Flag_E1: Boolean;
begin
Result := True;
// Repair input (because raw input in Windows is bugged and generally weird)
Flag_E0 := (Input.Flags and RI_KEY_E0) <> 0;
Flag_E1 := (Input.Flags and RI_KEY_E1) <> 0;
case Input.VKey of
VK_SHIFT: If fDiscernSides then
Input.VKey := MapVirtualKey(Input.MakeCode,MAPVK_VSC_TO_VK_EX);
VK_CONTROL: If fDiscernSides then
If Flag_E0 then Input.VKey := VK_RCONTROL
else Input.VKey := VK_LCONTROL;
VK_MENU: If fDiscernSides then
If Flag_E0 then Input.VKey := VK_RMENU
else Input.VKey := VK_LMENU;
VK_RETURN: If fAdditionalVKeys then
If Flag_E0 then Input.VKey := VK_NUMPADENTER;
VK_INSERT: If not Flag_E0 then Input.VKey := VK_NUMPAD0;
VK_DELETE: If not Flag_E0 then Input.VKey := VK_DECIMAL;
VK_HOME: If not Flag_E0 then Input.VKey := VK_NUMPAD7;
VK_END: If not Flag_E0 then Input.VKey := VK_NUMPAD1;
VK_PRIOR: If not Flag_E0 then Input.VKey := VK_NUMPAD9;
VK_NEXT: If not Flag_E0 then Input.VKey := VK_NUMPAD3;
VK_CLEAR: If not Flag_E0 then Input.VKey := VK_NUMPAD5;
VK_LEFT: If not Flag_E0 then Input.VKey := VK_NUMPAD4;
VK_RIGHT: If not Flag_E0 then Input.VKey := VK_NUMPAD6;
VK_UP: If not Flag_E0 then Input.VKey := VK_NUMPAD8;
VK_DOWN: If not Flag_E0 then Input.VKey := VK_NUMPAD2;
VK_NUMLOCK: Input.MakeCode := MapVirtualKey(Input.VKey,MAPVK_VK_TO_VSC) or $100;
0,$FF..High(USHORT): Result := False;
end;
If Flag_E1 then
begin
If Input.VKey = VK_PAUSE then
Input.MakeCode := $45
else
Input.MakeCode := MapVirtualKey(Input.VKey,MAPVK_VK_TO_VSC);
end;
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.ProcessKeyPress(VirtualKeyCode: USHORT);
begin
DoBeforeKeyPress(Self,VirtualKeyCode);
fKeyStates[Byte(VirtualKeyCode)] := True;
DoAfterKeyPress(Self,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.ProcessKeyRelease(VirtualKeyCode: USHORT);
begin
DoBeforeKeyRelease(Self,VirtualKeyCode);
fKeyStates[Byte(VirtualKeyCode)] := False;
DoAfterKeyRelease(Self,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.DoRawKeyboard(Sender: TObject; Input: TRawKeyboard);
begin
If Assigned(fOnRawKeyboard) then fOnRawKeyboard(Sender,Input);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.DoBeforeKeyPress(Sender: TObject; VirtualKeyCode: USHORT);
begin
If Assigned(fOnBeforeKeyPress) then fOnBeforeKeyPress(Sender,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.DoAfterKeyPress(Sender: TObject; VirtualKeyCode: USHORT);
begin
If Assigned(fOnAfterKeyPress) then fOnAfterKeyPress(Sender,VirtualKeyCode);
If Assigned(fOnKeyPress) then fOnKeyPress(Sender,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.DoBeforeKeyRelease(Sender: TObject; VirtualKeyCode: USHORT);
begin
If Assigned(fOnBeforeKeyRelease) then fOnBeforeKeyRelease(Sender,VirtualKeyCode);
If Assigned(fOnKeyRelease) then fOnKeyRelease(Sender,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.DoAfterKeyRelease(Sender: TObject; VirtualKeyCode: USHORT);
begin
If Assigned(fOnAfterKeyRelease) then fOnAfterKeyRelease(Sender,VirtualKeyCode);
end;
{------------------------------------------------------------------------------}
{ TRawInputKeyboard - public methods }
{------------------------------------------------------------------------------}
class Function TRawInputKeyboard.GetVirtualKeyName(VirtualKeyCode: USHORT; NumberForUnknown: Boolean = True): String;
var
Flag_E0: Boolean;
ScanCode: Integer;
begin
case VirtualKeyCode of
VK_NUMLOCK,VK_RCONTROL,VK_RMENU,VK_LWIN,VK_RWIN,VK_INSERT,VK_DELETE,VK_HOME,
VK_END,VK_PRIOR,VK_NEXT,VK_LEFT,VK_RIGHT,VK_UP,VK_DOWN,VK_DIVIDE,VK_APPS,
VK_SNAPSHOT,VK_CLEAR,VK_CANCEL: Flag_E0 := True;
VK_NUMPADENTER:
begin
VirtualKeyCode := VK_RETURN;
Flag_E0 := True;
end;
else
Flag_E0 := False;
end;
// MapVirtualKey(Ex) is unable to map following VK to SC, have to do it manually
case VirtualKeyCode of
VK_PAUSE: ScanCode := $45;
VK_SNAPSHOT: ScanCode := $37;
else
ScanCode := MapVirtualKey(VirtualKeyCode,MAPVK_VK_TO_VSC);
end;
If Flag_E0 then ScanCode := ScanCode or $100;
SetLength(Result,32);
SetLength(Result,GetKeyNameText(ScanCode shl 16,PChar(Result),Length(Result)));
Result := WinToStr(Result);
If (Length(Result) <= 0) and NumberForUnknown then
Result := '0x' + IntToHex(VirtualKeyCode,2);
end;
//------------------------------------------------------------------------------
class Function TRawInputKeyboard.ShiftStatesToStr(ShiftStates: TKeyboardShiftStates; const Separator: String = ' '; DiscernSides: Boolean = False): String;
procedure AddShiftStateStr(var Str: String; const ShiftStr: String);
begin
If Str <> '' then
Str := Str + Separator + ShiftStr
else
Str := Str + ShiftStr;
end;
begin
Result := '';
If DiscernSides then
begin
If kssLControl in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_LCONTROL));
If kssRControl in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_RCONTROL));
If kssLAlt in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_LMENU));
If kssRAlt in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_RMENU));
If kssLShift in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_LSHIFT));
If kssRShift in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_RSHIFT));
end
else
begin
If kssControl in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_CONTROL));
If kssAlt in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_MENU));
If kssShift in ShiftStates then AddShiftStateStr(Result,GetVirtualKeyName(VK_SHIFT));
end;
end;
//------------------------------------------------------------------------------
constructor TRawInputKeyboard.Create(DeviceInfo: PDeviceListItem = nil);
begin
inherited Create(DeviceInfo);
fDoInputCorrection := True;
fDiscernSides := False;
fAdditionalVKeys := False;
fKeyStates := TBitVector.Create(High(Byte));
end;
//------------------------------------------------------------------------------
destructor TRawInputKeyboard.Destroy;
begin
fKeyStates.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboard.Invalidate;
begin
fKeyStates.Fill(False);
end;
//------------------------------------------------------------------------------
Function TRawInputKeyboard.GetShiftStates: TKeyboardShiftStates;
begin
Result := [];
If fKeyStates[VK_CONTROL] or fKeyStates[VK_RCONTROL] or fKeyStates[VK_LCONTROL] then
Result := Result + [kssControl];
If fKeyStates[VK_MENU] or fKeyStates[VK_RMENU] or fKeyStates[VK_LMENU] then
Result := Result + [kssAlt];
If fKeyStates[VK_SHIFT] or fKeyStates[VK_RSHIFT] or fKeyStates[VK_LSHIFT] then
Result := Result + [kssShift];
If fDiscernSides then
begin
If fKeyStates[VK_RCONTROL] then
Result := Result + [kssRControl];
If fKeyStates[VK_LCONTROL] then
Result := Result + [kssLControl];
If fKeyStates[VK_RMENU] then
Result := Result + [kssRAlt];
If fKeyStates[VK_LMENU] then
Result := Result + [kssLAlt];
If fKeyStates[VK_RSHIFT] then
Result := Result + [kssRShift];
If fKeyStates[VK_LSHIFT] then
Result := Result + [kssLShift];
end;
end;
//------------------------------------------------------------------------------
Function TRawInputKeyboard.GetIndicators: TKeyboardIndicators;
begin
Result := [];
If (GetKeyState(VK_NUMLOCK) and 1) <> 0 then
Result := Result + [kiNumLock];
If (GetKeyState(VK_CAPITAL) and 1) <> 0 then
Result := Result + [kiCapsLock];
If (GetKeyState(VK_SCROLL) and 1) <> 0 then
Result := Result + [kiScrollLock];
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMaster }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputKeyboardMaster - implementation }
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMaster - private methods }
{------------------------------------------------------------------------------}
Function TRawInputKeyboardMaster.GetDeviceCount: Integer;
begin
Result := fDevices.Count;
end;
//------------------------------------------------------------------------------
Function TRawInputKeyboardMaster.GetDevice(Index: Integer): TRawInputKeyboard;
begin
If (Index >= 0) and (Index < fDevices.Count) then
Result := TRawInputKeyboard(fDevices[Index])
else
raise Exception.CreateFmt('TRawInputKeyboardMaster.GetDevice: Index (%d) out of bounds.',[Index]);
end;
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMaster - protected methods }
{------------------------------------------------------------------------------}
procedure TRawInputKeyboardMaster.ProcessRawInputSpecific(RawInput: PRawInput);
var
Index: Integer;
begin
If Assigned(fOnUnknownDeviceIntercepted) and (RawInput^.header.hDevice <> 0) then
begin
Index := IndexOfDevice(RawInput^.header.hDevice);
If Index >= 0 then
TRawInputKeyboard(fDevices[Index]).ProcessRawInput(RawInput)
else
begin
fOnUnknownDeviceIntercepted(Self,RawInput^.header.hDevice);
Index := IndexOfDevice(RawInput^.header.hDevice);
If Index >= 0 then
TRawInputKeyboard(fDevices[Index]).ProcessRawInput(RawInput);
end;
end;
inherited;
end;
//------------------------------------------------------------------------------
Function TRawInputKeyboardMaster.AddDeviceInternal(DeviceInfo: PDeviceListItem): Integer;
var
TempObject: TRawInputKeyboardDeviceInternal;
i: Integer;
begin
TempObject := TRawInputKeyboardDeviceInternal.Create(DeviceInfo);
Result := fDevices.Add(TempObject);
If Result < 0 then
TempObject.Free
else
begin
For i := 0 to Pred(fDevices.Count) do
TRawInputKeyboardDeviceInternal(fDevices[i]).Index := i;
If Assigned(fOnDevicesChange) then fOnDevicesChange(Self);
end;
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboardMaster.DeleteDeviceInternal(Index: Integer);
var
i: Integer;
begin
If (Index >= 0) and (Index < fDevices.Count) then
begin
fDevices.Delete(Index);
For i := 0 to Pred(fDevices.Count) do
TRawInputKeyboardDeviceInternal(fDevices[i]).Index := i;
If Assigned(fOnDevicesChange) then fOnDevicesChange(Self);
end
else
raise Exception.CreateFmt('TRawInputKeyboardMaster.DeleteDeviceInternal: Index (%d) out of bounds.',[Index]);
end;
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMaster - public methods }
{------------------------------------------------------------------------------}
constructor TRawInputKeyboardMaster.Create;
begin
inherited Create(nil);
fDevices := TObjectList.Create(True);
end;
//------------------------------------------------------------------------------
destructor TRawInputKeyboardMaster.Destroy;
begin
fDevices.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboardMaster.Invalidate;
var
i: Integer;
begin
inherited;
For i := 0 to Pred(fDevices.Count) do
TRawInputKeyboard(fDevices[i]).Invalidate;
end;
//------------------------------------------------------------------------------
Function TRawInputKeyboardMaster.IndexOfDevice(DeviceHandle: THandle): Integer;
var
i: Integer;
begin
Result := -1;
For i := 0 to Pred(fDevices.Count) do
If TRawInputKeyboard(fDevices[i]).DeviceInfo.Handle = DeviceHandle then
begin
Result := i;
Break;
end;
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardDeviceInternal }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputKeyboardDeviceInternal - implementation }
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardDeviceInternal - protected methods }
{------------------------------------------------------------------------------}
procedure TRawInputKeyboardDeviceInternal.DoRawKeyboard(Sender: TObject; Input: TRawKeyboard);
begin
inherited;
If Assigned(fOnRawKeyboardInternal) then fOnRawKeyboardInternal(Sender,Input);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboardDeviceInternal.DoBeforeKeyPress(Sender: TObject; VirtualKeyCode: USHORT);
begin
inherited;
If Assigned(fOnBeforeKeyPressInternal) then fOnBeforeKeyPressInternal(Sender,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboardDeviceInternal.DoAfterKeyPress(Sender: TObject; VirtualKeyCode: USHORT);
begin
inherited;
If Assigned(fOnAfterKeyPressInternal) then fOnAfterKeyPressInternal(Sender,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboardDeviceInternal.DoBeforeKeyRelease(Sender: TObject; VirtualKeyCode: USHORT);
begin
inherited;
If Assigned(fOnBeforeKeyReleaseInternal) then fOnBeforeKeyReleaseInternal(Sender,VirtualKeyCode);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboardDeviceInternal.DoAfterKeyRelease(Sender: TObject; VirtualKeyCode: USHORT);
begin
inherited;
If Assigned(fOnAfterKeyReleaseInternal) then fOnAfterKeyReleaseInternal(Sender,VirtualKeyCode);
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMasterInternal }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputKeyboardMasterInternal - implementation }
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputKeyboardMasterInternal - public methods }
{------------------------------------------------------------------------------}
Function TRawInputKeyboardMasterInternal.AddDevice(DeviceInfo: PDeviceListItem; AssignCallbacks: Boolean = False): Integer;
begin
Result := AddDeviceInternal(DeviceInfo);
If (Result >= 0) and AssignCallbacks then
with TRawInputKeyboardDeviceInternal(Devices[Result]) do
begin
OnRawKeyboardInternal := Self.DoRawKeyboard;
OnBeforeKeyPressInternal := Self.DoBeforeKeyPress;
OnAfterKeyPressInternal := Self.DoAfterKeyPress;
OnBeforeKeyReleaseInternal := Self.DoBeforeKeyRelease;
OnAfterKeyReleaseInternal := Self.DoAfterKeyRelease;
end;
end;
//------------------------------------------------------------------------------
Function TRawInputKeyboardMasterInternal.RemoveDevice(DeviceHandle: THandle): Integer;
begin
Result := IndexOfDevice(DeviceHandle);
If Result >= 0 then DeleteDeviceInternal(Result);
end;
//------------------------------------------------------------------------------
procedure TRawInputKeyboardMasterInternal.DeleteDevice(Index: Integer);
begin
DeleteDeviceInternal(Index);
end;
end.