-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathretrokeyboard.pas
4837 lines (4249 loc) · 146 KB
/
retrokeyboard.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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Ultibo Keyboard interface unit.
Copyright (C) 2015 - SoftOz Pty Ltd.
// -----------------------------------------------------------------------------
// ---This version patched for using in retromachine sidplayer - pik33 @20161125
// -----------------------------------------------------------------------------
Arch
====
<All>
Boards
======
<All>
Licence
=======
LGPLv2.1 with static linking exception (See COPYING.modifiedLGPL.txt)
Credits
=======
Information for this unit was obtained from:
U-Boot - \common\usb_kbd.c
References
==========
USB HID Device Class Definition 1_11.pdf
http://www.usb.org/developers/hidpage/HID1_11.pdf
USB HID Usage Tables 1_12v2.pdf
http://www.usb.org/developers/hidpage/Hut1_12v2.pdf
Pascal Keyboard scan codes
http://www.freepascal.org/docs-html/rtl/keyboard/kbdscancode.html
ASCII Control codes
https://en.wikipedia.org/wiki/ASCII
Keyboard Devices
================
This unit provides both the Keyboard device interface and the generic USB HID keyboard driver.
The keyboard unit also provides the STDIN interface for the Run Time Library (RTL)
USB Keyboard Devices
====================
This driver currently uses HID Boot Protocol only and could be redesigned in future to use
the HID Report Protocol instead to allow for greater language support etc.
}
{$mode delphi} {Default to Delphi compatible syntax}
{$H+} {Default to AnsiString}
{$inline on} {Allow use of Inline procedures}
unit retrokeyboard;
interface
uses GlobalConfig,GlobalConst,GlobalTypes,Platform,Threads,Devices,USB,Keymap,SysUtils;
{==============================================================================}
{Global definitions}
{$INCLUDE GlobalDefines.inc}
{==============================================================================}
const
{Keyboard specific constants}
KEYBOARD_NAME_PREFIX = 'Keyboard'; {Name prefix for Keyboard Devices}
{Keyboard Device Types}
KEYBOARD_TYPE_NONE = 0;
KEYBOARD_TYPE_USB = 1;
KEYBOARD_TYPE_PS2 = 2;
KEYBOARD_TYPE_SERIAL = 3;
KEYBOARD_TYPE_MAX = 3;
{Keyboard Type Names}
KEYBOARD_TYPE_NAMES:array[KEYBOARD_TYPE_NONE..KEYBOARD_TYPE_MAX] of String = (
'KEYBOARD_TYPE_NONE',
'KEYBOARD_TYPE_USB',
'KEYBOARD_TYPE_PS2',
'KEYBOARD_TYPE_SERIAL');
{Keyboard Device States}
KEYBOARD_STATE_DETACHED = 0;
KEYBOARD_STATE_DETACHING = 1;
KEYBOARD_STATE_ATTACHING = 2;
KEYBOARD_STATE_ATTACHED = 3;
KEYBOARD_STATE_MAX = 3;
{Keyboard State Names}
KEYBOARD_STATE_NAMES:array[KEYBOARD_STATE_DETACHED..KEYBOARD_STATE_MAX] of String = (
'KEYBOARD_STATE_DETACHED',
'KEYBOARD_STATE_DETACHING',
'KEYBOARD_STATE_ATTACHING',
'KEYBOARD_STATE_ATTACHED');
{Keyboard Device Flags}
KEYBOARD_FLAG_NONE = $00000000;
KEYBOARD_FLAG_NON_BLOCK = $00000001;
KEYBOARD_FLAG_DIRECT_READ = $00000002;
KEYBOARD_FLAG_PEEK_BUFFER = $00000004;
KEYBOARD_FLAG_MASK = KEYBOARD_FLAG_NON_BLOCK or KEYBOARD_FLAG_DIRECT_READ or KEYBOARD_FLAG_PEEK_BUFFER;
{Keyboard Device Control Codes}
KEYBOARD_CONTROL_GET_FLAG = 1; {Get Flag}
KEYBOARD_CONTROL_SET_FLAG = 2; {Set Flag}
KEYBOARD_CONTROL_CLEAR_FLAG = 3; {Clear Flag}
KEYBOARD_CONTROL_FLUSH_BUFFER = 4; {Flush Buffer}
KEYBOARD_CONTROL_GET_LED = 5; {Get LED}
KEYBOARD_CONTROL_SET_LED = 6; {Set LED}
KEYBOARD_CONTROL_CLEAR_LED = 7; {Clear LED}
KEYBOARD_CONTROL_GET_REPEAT_RATE = 8; {Get Repeat Rate}
KEYBOARD_CONTROL_SET_REPEAT_RATE = 9; {Set Repeat Rate}
KEYBOARD_CONTROL_GET_REPEAT_DELAY = 10; {Get Repeat Delay}
KEYBOARD_CONTROL_SET_REPEAT_DELAY = 11; {Set Repeat Delay}
{Keyboard Device LEDs}
KEYBOARD_LED_NONE = $00000000;
KEYBOARD_LED_NUMLOCK = $00000001;
KEYBOARD_LED_CAPSLOCK = $00000002;
KEYBOARD_LED_SCROLLLOCK = $00000004;
KEYBOARD_LED_COMPOSE = $00000008;
KEYBOARD_LED_KANA = $00000010;
KEYBOARD_LED_MASK = KEYBOARD_LED_NUMLOCK or KEYBOARD_LED_CAPSLOCK or KEYBOARD_LED_SCROLLLOCK or KEYBOARD_LED_COMPOSE or KEYBOARD_LED_KANA;
{Keyboard Buffer Size}
KEYBOARD_BUFFER_SIZE = 512;
{Keyboard Sampling Rate}
KEYBOARD_REPEAT_RATE = 0;//(200 div 4); {40msec -> 25cps}
KEYBOARD_REPEAT_DELAY = 0;// 10; {10 x KEYBOARD_REPEAT_RATE = 400msec initial delay before repeat}
{Keyboard Data Definitions (Values for TKeyboardData.Modifiers)}
KEYBOARD_LEFT_CTRL = $00000001; {The Left Control key is pressed}
KEYBOARD_LEFT_SHIFT = $00000002; {The Left Shift key is pressed}
KEYBOARD_LEFT_ALT = $00000004; {The Left Alt key is pressed}
KEYBOARD_LEFT_GUI = $00000008; {The Left GUI (or Windows) key is pressed}
KEYBOARD_RIGHT_CTRL = $00000010; {The Right Control key is pressed}
KEYBOARD_RIGHT_SHIFT = $00000020; {The Right Shift key is pressed}
KEYBOARD_RIGHT_ALT = $00000040; {The Right Alt key is pressed}
KEYBOARD_RIGHT_GUI = $00000080; {The Right GUI (or Windows) key is pressed}
KEYBOARD_NUM_LOCK = $00000100; {Num Lock is currently on}
KEYBOARD_CAPS_LOCK = $00000200; {Caps Lock is currently on}
KEYBOARD_SCROLL_LOCK = $00000400; {Scroll Lock is currently on}
KEYBOARD_COMPOSE = $00000800; {Compose is currently on}
KEYBOARD_KANA = $00001000; {Kana is currently on}
KEYBOARD_KEYUP = $00002000; {The key state changed to up}
KEYBOARD_KEYDOWN = $00004000; {The key state changed to down}
KEYBOARD_KEYREPEAT = $00008000; {The key is being repeated}
KEYBOARD_DEADKEY = $00010000; {The key is a being handled as a deadkey}
KEYBOARD_ALTGR = $00020000; {The AltGr key is pressed (Normally also Right Alt but may be Ctrl-Alt)}
{Keyboard logging}
KEYBOARD_LOG_LEVEL_DEBUG = LOG_LEVEL_DEBUG; {Keyboard debugging messages}
KEYBOARD_LOG_LEVEL_INFO = LOG_LEVEL_INFO; {Keyboard informational messages, such as a device being attached or detached}
KEYBOARD_LOG_LEVEL_ERROR = LOG_LEVEL_ERROR; {Keyboard error messages}
KEYBOARD_LOG_LEVEL_NONE = LOG_LEVEL_NONE; {No Keyboard messages}
var
KEYBOARD_DEFAULT_LOG_LEVEL:LongWord = KEYBOARD_LOG_LEVEL_DEBUG; {Minimum level for Keyboard messages. Only messages with level greater than or equal to this will be printed}
var
{Keyboard logging}
KEYBOARD_LOG_ENABLED:Boolean;
{==============================================================================}
const
{USB Keyboard specific constants}
USBKEYBOARD_DRIVER_NAME = 'USB Keyboard Driver (HID boot protocol)'; {Name of USB keyboard driver}
USBKEYBOARD_KEYBOARD_DESCRIPTION = 'USB HID Keyboard'; {Description of USB keyboard device}
{HID Interface Subclass types (See USB HID v1.11 specification)}
USB_HID_SUBCLASS_BOOT = 1; {Section 4.2}
{HID Interface Protocol types (See USB HID v1.11 specification)}
USB_HID_BOOT_PROTOCOL_KEYBOARD = 1; {Section 4.3}
USB_HID_BOOT_PROTOCOL_MOUSE = 2; {Section 4.3}
{HID Request types}
USB_HID_REQUEST_GET_REPORT = $01;
USB_HID_REQUEST_GET_IDLE = $02;
USB_HID_REQUEST_GET_PROTOCOL = $03; {Section 7.2}
USB_HID_REQUEST_SET_REPORT = $09;
USB_HID_REQUEST_SET_IDLE = $0A;
USB_HID_REQUEST_SET_PROTOCOL = $0B; {Section 7.2}
{HID Protocol types}
USB_HID_PROTOCOL_BOOT = 0; {Section 7.2.5}
USB_HID_PROTOCOL_REPORT = 1; {Section 7.2.5}
{HID Report types}
USB_HID_REPORT_INPUT = 1; {Section 7.2.1}
USB_HID_REPORT_OUTPUT = 2; {Section 7.2.1}
USB_HID_REPORT_FEATURE = 3; {Section 7.2.1}
{HID Report IDs}
USB_HID_REPORTID_NONE = 0; {Section 7.2.1}
{HID Boot Protocol Modifier bits}
USB_HID_BOOT_LEFT_CTRL = (1 shl 0);
USB_HID_BOOT_LEFT_SHIFT = (1 shl 1);
USB_HID_BOOT_LEFT_ALT = (1 shl 2);
USB_HID_BOOT_LEFT_GUI = (1 shl 3);
USB_HID_BOOT_RIGHT_CTRL = (1 shl 4);
USB_HID_BOOT_RIGHT_SHIFT = (1 shl 5);
USB_HID_BOOT_RIGHT_ALT = (1 shl 6);
USB_HID_BOOT_RIGHT_GUI = (1 shl 7);
{HID Boot Protocol Report data}
USB_HID_BOOT_REPORT_SIZE = 8; {Appendix B of HID Device Class Definition 1.11}
{HID Boot Protocol Output bits}
USB_HID_BOOT_NUMLOCK_LED = (1 shl 0);
USB_HID_BOOT_CAPSLOCK_LED = (1 shl 1);
USB_HID_BOOT_SCROLLLOCK_LED = (1 shl 2);
USB_HID_BOOT_COMPOSE_LED = (1 shl 3);
USB_HID_BOOT_KANA_LED = (1 shl 4);
USB_HIB_BOOT_LEDMASK = USB_HID_BOOT_NUMLOCK_LED or USB_HID_BOOT_CAPSLOCK_LED or USB_HID_BOOT_SCROLLLOCK_LED or USB_HID_BOOT_COMPOSE_LED or USB_HID_BOOT_KANA_LED;
{HID Boot Protocol Output data}
USB_HID_BOOT_OUTPUT_SIZE = 1; {Appendix B of HID Device Class Definition 1.11}
{Map of HID Boot Protocol keyboard Usage IDs to Characters}
{Entries not filled in are left 0 and are interpreted as unrecognized input and ignored (Section 10 of the Universal Serial Bus HID Usage Tables v1.12)}
{Note: These are no longer used, see the Keymap unit for scan code to key code translation tables}
// ----- the table uncommented and modified by pik33 @20161123 ----------------
USB_HID_BOOT_USAGE_ID:array[0..255] of array[0..3] of Char = (
{0} (#0, #0, #0, #0), {Reserved (no event indicated)}
{1} (#0, #0, #0, #0), {Keyboard ErrorRollOver}
{2} (#0, #0, #0, #0), {Keyboard POSTFail}
{3} (#0, #0, #0, #0), {Keyboard ErrorUndefined}
{4} ('a', 'A', #23, #14), {Keyboard a or A}
{5} ('b', 'B', #0, #0), {Keyboard b or B}
{6} ('c', 'C', #25, #16), {Keyboard c or C}
{7} ('d', 'D', #0, #0), {Keyboard d or D}
{8} ('e', 'E', #24, #15), {Keyboard e or E}
{9} ('f', 'F', #0, #0), {Keyboard f or F}
{10} ('g', 'G', #0, #0), {Keyboard g or G}
{11} ('h', 'H', #0, #0), {Keyboard h or H}
{12} ('i', 'I', #0, #0), {Keyboard i or I}
{13} ('j', 'J', #0, #0), {Keyboard j or J}
{14} ('k', 'K', #0, #0), {Keyboard k or K}
{15} ('l', 'L', #31, #22), {Keyboard l or L}
{16} ('m', 'M', #0, #0), {Keyboard m or M}
{17} ('n', 'N', #26, #17), {Keyboard n or N}
{18} ('o', 'O', #30, #21), {Keyboard o or O}
{19} ('p', 'P', #0, #0), {Keyboard p or P}
{20} ('q', 'Q', #0, #0), {Keyboard q or Q}
{21} ('r', 'R', #0, #0), {Keyboard r or R}
{22} ('s', 'S', #27, #18), {Keyboard s or S}
{23} ('t', 'T', #0, #0), {Keyboard t or T}
{24} ('u', 'U', #0, #0), {Keyboard u or U}
{25} ('v', 'V', #0, #0), {Keyboard v or V}
{26} ('w', 'W', #0, #0), {Keyboard w or W}
{27} ('x', 'X', #28, #19), {Keyboard x or X}
{28} ('y', 'Y', #0, #0), {Keyboard y or Y}
{29} ('z', 'Z', #29, #20), {Keyboard z or Z}
{30} ('1', '!', #4, #0), {Keyboard 1 or !}
{31} ('2', '@', #5, #0), {Keyboard 2 or @}
{32} ('3', '#', #6, #0), {Keyboard 3 or #}
{33} ('4', '$', #7, #0), {Keyboard 4 or $}
{34} ('5', '%', #8, #0), {Keyboard 5 or %}
{35} ('6', '^', #9, #0), {Keyboard 6 or ^}
{36} ('7', '&', #10, #0), {Keyboard 7 or &}
{37} ('8', '*', #11, #0), {Keyboard 8 or *}
{38} ('9', '(', #12, #0), {Keyboard 9 or (}
{39} ('0', ')', #13, #0), {Keyboard 0 or )}
{40} (#141, #141, #0, #0), {Keyboard Enter)}
{41} (#155, #155, #0, #0), {Keyboard Escape}
{42} (#136, #136, #0, #0), {Keyboard Backspace}
{43} (#137, #137, #0, #0), {Keyboard Tab}
{44} (' ', ' ', #0, #0), {Keyboard Spacebar}
{45} ('-', '_', #0, #0), {Keyboard - or _}
{46} ('=', '+', #0, #0), {Keyboard = or +}
{47} ('[', '{', #0, #0), {Keyboard [ or Left Brace}
{48} (']', '}', #0, #0), {Keyboard ] or Right Brace}
{49} ('\', '|', #0, #0), {Keyboard \ or |}
{50} ('#', '~', #0, #0), {Keyboard Non-US # and ~}
{51} (';', ':', #0, #0), {Keyboard ; or :}
{52} ('''', '"', #0, #0), {Keyboard ' or "}
{53} ('`', '~', #3, #0), {Keyboard ` or ~}
{54} (',', '<', #0, #0), {Keyboard , or <}
{55} ('.', '>', #0, #0), {Keyboard . or >}
{56} ('/', '?', #0, #0), {Keyboard / or ?}
{57} (#185, #185, #0, #0), {Keyboard Caps Lock}
{58} (#186, #0, #0, #0), {Keyboard F1}
{59} (#187, #0, #0, #0), {Keyboard F2}
{60} (#188, #0, #0, #0), {Keyboard F3}
{61} (#189, #0, #0, #0), {Keyboard F4}
{62} (#190, #0, #0, #0), {Keyboard F5}
{63} (#191, #0, #0, #0), {Keyboard F6}
{64} (#192, #0, #0, #0), {Keyboard F7}
{65} (#193, #0, #0, #0), {Keyboard F8}
{66} (#194, #0, #0, #0), {Keyboard F9}
{67} (#195, #0, #0, #0), {Keyboard F10}
{68} (#196, #0, #0, #0), {Keyboard F11}
{69} (#197, #0, #0, #0), {Keyboard F12}
{70} (#198, #0, #0, #0), {Keyboard Print Screen}
{71} (#199, #0, #0, #0), {Keyboard Scroll Lock}
{72} (#200, #0, #0, #0), {Keyboard Pause}
{73} (#201, #0, #0, #0), {Keyboard Insert}
{74} (#202, #0, #0, #0), {Keyboard Home}
{75} (#203, #0, #0, #0), {Keyboard PageUp}
{76} (#127, #127, #0, #0), {Keyboard Delete}
{77} (#204, #0, #0, #0), {Keyboard End}
{78} (#205, #0, #0, #0), {Keyboard PageDn}
{79} (#206, #0, #0, #0), {Keyboard Right Arrow}
{80} (#207, #0, #0, #0), {Keyboard Left Arrow}
{81} (#208, #0, #0, #0), {Keyboard Down Arrow}
{82} (#209, #0, #0, #0), {Keyboard Up Arrow}
{83} (#210, #0, #0, #0), {Keyboard Num Lock}
{84} ('/', '/', #0, #0), {Keypad /}
{85} ('*', '*', #0, #0), {Keypad *}
{86} ('-', '-', #0, #0), {Keypad -}
{87} ('+', '+', #0, #0), {Keypad +}
{88} (#141,#141, #0, #0), {Keypad Enter}
{89} ('1', '1', #0, #0), {Keypad 1 and End}
{90} ('2', '2', #0, #0), {Keypad 2 and Down Arrow}
{91} ('3', '3', #0, #0), {Keypad 3 and PageDn}
{92} ('4', '4', #0, #0), {Keypad 4 and Left Arrow}
{93} ('5', '5', #0, #0), {Keypad 5}
{94} ('6', '6', #0, #0), {Keypad 6 and Right Arrow}
{95} ('7', '7', #0, #0), {Keypad 7 and Home}
{96} ('8', '8', #0, #0), {Keypad 8 and Up Arrow}
{97} ('9', '9', #0, #0), {Keypad 9 and PageUp}
{98} ('0', '0', #0, #0), {Keypad 0 and Insert}
{99} ('.', #127, #0, #0), {Keypad . and Delete}
{100} ('\', '|', #0, #0), {Keyboard Non-US \ and |}
{101} (#0, #0, #0, #0), {Keyboard Application}
{102} (#0, #0, #0, #0), {Keyboard Power}
{103} ('=', '=', #0, #0), {Keypad =}
{104} (#0, #0, #0, #0), {Keyboard F13}
{105} (#0, #0, #0, #0), {Keyboard F14}
{106} (#0, #0, #0, #0), {Keyboard F15}
{107} (#0, #0, #0, #0), {Keyboard F16}
{108} (#0, #0, #0, #0), {Keyboard F17}
{109} (#0, #0, #0, #0), {Keyboard F18}
{110} (#0, #0, #0, #0), {Keyboard F19}
{111} (#0, #0, #0, #0), {Keyboard F20}
{112} (#0, #0, #0, #0), {Keyboard F21}
{113} (#0, #0, #0, #0), {Keyboard F22}
{114} (#0, #0, #0, #0), {Keyboard F23}
{115} (#0, #0, #0, #0), {Keyboard F24}
{116} (#0, #0, #0, #0), {Keyboard Execute}
{117} (#0, #0, #0, #0), {Keyboard Help}
{118} (#0, #0, #0, #0), {Keyboard Menu}
{119} (#0, #0, #0, #0), {Keyboard Select}
{120} (#0, #0, #0, #0), {Keyboard Stop}
{121} (#0, #0, #0, #0), {Keyboard Again}
{122} (#0, #0, #0, #0), {Keyboard Undo}
{123} (#0, #0, #0, #0), {Keyboard Cut}
{124} (#0, #0, #0, #0), {Keyboard Copy}
{125} (#0, #0, #0, #0), {Keyboard Paste}
{126} (#0, #0, #0, #0), {Keyboard Find}
{127} (#0, #0, #0, #0), {Keyboard Mute}
{128} (#0, #0, #0, #0), {Keyboard Volume Up}
{129} (#0, #0, #0, #0), {Keyboard Volume Down}
{130} (#0, #0, #0, #0), {Keyboard Locking Caps Lock}
{131} (#0, #0, #0, #0), {Keyboard Locking Num Lock}
{132} (#0, #0, #0, #0), {Keyboard Locking Scroll Lock}
{133} (',', ',', #0, #0), {Keypad Comma}
{134} (#0, #0, #0, #0), {Keypad Equal Sign}
{135} (#0, #0, #0, #0), {Keyboard International1}
{136} (#0, #0, #0, #0), {Keyboard International2}
{137} (#0, #0, #0, #0), {Keyboard International3}
{138} (#0, #0, #0, #0), {Keyboard International4}
{139} (#0, #0, #0, #0), {Keyboard International5}
{140} (#0, #0, #0, #0), {Keyboard International6}
{141} (#0, #0, #0, #0), {Keyboard International7}
{142} (#0, #0, #0, #0), {Keyboard International8}
{143} (#0, #0, #0, #0), {Keyboard International9}
{144} (#0, #0, #0, #0), {Keyboard LANG1}
{145} (#0, #0, #0, #0), {Keyboard LANG2}
{146} (#0, #0, #0, #0), {Keyboard LANG3}
{147} (#0, #0, #0, #0), {Keyboard LANG4}
{148} (#0, #0, #0, #0), {Keyboard LANG5}
{149} (#0, #0, #0, #0), {Keyboard LANG6}
{150} (#0, #0, #0, #0), {Keyboard LANG7}
{151} (#0, #0, #0, #0), {Keyboard LANG8}
{152} (#0, #0, #0, #0), {Keyboard LANG9}
{153} (#0, #0, #0, #0), {Keyboard Alternate Erase}
{154} (#0, #0, #0, #0), {Keyboard SysReq/Attention}
{155} (#0, #0, #0, #0), {Keyboard Cancel}
{156} (#0, #0, #0, #0), {Keyboard Clear}
{157} (#0, #0, #0, #0), {Keyboard Prior}
{158} (#0, #0, #0, #0), {Keyboard Return}
{159} (#0, #0, #0, #0), {Keyboard Separator}
{160} (#0, #0, #0, #0), {Keyboard Out}
{161} (#0, #0, #0, #0), {Keyboard Oper}
{162} (#0, #0, #0, #0), {Keyboard Clear/Again}
{163} (#0, #0, #0, #0), {Keyboard CrSel/Props}
{164} (#0, #0, #0, #0), {Keyboard ExSel}
{165} (#0, #0, #0, #0), {Reserved}
{166} (#0, #0, #0, #0), {Reserved}
{167} (#0, #0, #0, #0), {Reserved}
{168} (#0, #0, #0, #0), {Reserved}
{169} (#0, #0, #0, #0), {Reserved}
{170} (#0, #0, #0, #0), {Reserved}
{171} (#0, #0, #0, #0), {Reserved}
{172} (#0, #0, #0, #0), {Reserved}
{173} (#0, #0, #0, #0), {Reserved}
{174} (#0, #0, #0, #0), {Reserved}
{175} (#0, #0, #0, #0), {Reserved}
{176} (#0, #0, #0, #0), {Keypad 00}
{177} (#0, #0, #0, #0), {Keypad 000}
{178} (#0, #0, #0, #0), {Thousands Separator}
{179} (#0, #0, #0, #0), {Decimal Separator}
{180} (#0, #0, #0, #0), {Currency Unit}
{181} (#0, #0, #0, #0), {Currenct Sub-unit}
{182} (#0, #0, #0, #0), {Keypad (}
{183} (#0, #0, #0, #0), {Keypad )}
{184} (#0, #0, #0, #0), {Keypad Left Brace}
{185} (#0, #0, #0, #0), {Keypad Right Brace}
{186} (#0, #0, #0, #0), {Keypad Tab}
{187} (#0, #0, #0, #0), {Keypad Backspace}
{188} (#0, #0, #0, #0), {Keypad A}
{189} (#0, #0, #0, #0), {Keypad B}
{190} (#0, #0, #0, #0), {Keypad C}
{191} (#0, #0, #0, #0), {Keypad D}
{192} (#0, #0, #0, #0), {Keypad E}
{193} (#0, #0, #0, #0), {Keypad F}
{194} (#0, #0, #0, #0), {Keypad XOR}
{195} (#0, #0, #0, #0), {Keypad ^}
{196} (#0, #0, #0, #0), {Keypad %}
{197} (#0, #0, #0, #0), {Keypad <}
{198} (#0, #0, #0, #0), {Keypad >}
{199} (#0, #0, #0, #0), {Keypad &}
{200} (#0, #0, #0, #0), {Keypad &&}
{201} (#0, #0, #0, #0), {Keypad |}
{202} (#0, #0, #0, #0), {Keypad ||}
{203} (#0, #0, #0, #0), {Keypad :}
{204} (#0, #0, #0, #0), {Keypad #}
{205} (#0, #0, #0, #0), {Keypad Space}
{206} (#0, #0, #0, #0), {Keypad @}
{207} (#0, #0, #0, #0), {Keypad !}
{208} (#0, #0, #0, #0), {Keypad Memory Store}
{209} (#0, #0, #0, #0), {Keypad Memory Recall}
{210} (#0, #0, #0, #0), {Keypad Memory Clear}
{211} (#0, #0, #0, #0), {Keypad Memory Add}
{212} (#0, #0, #0, #0), {Keypad Memory Subtract}
{213} (#0, #0, #0, #0), {Keypad Memory Multiply}
{214} (#0, #0, #0, #0), {Keypad Memory Divide}
{215} (#0, #0, #0, #0), {Keypad +/-}
{216} (#0, #0, #0, #0), {Keypad Clear}
{217} (#0, #0, #0, #0), {Keypad Clear Entry}
{218} (#0, #0, #0, #0), {Keypad Binary}
{219} (#0, #0, #0, #0), {Keypad Octal}
{220} (#0, #0, #0, #0), {Keypad Decimal}
{221} (#0, #0, #0, #0), {Keypad Hexadecimal}
{222} (#0, #0, #0, #0), {Reserved}
{223} (#0, #0, #0, #0), {Reserved}
{224} (#0, #0, #0, #0), {Keyboard LeftControl}
{225} (#0, #0, #0, #0), {Keyboard LeftShift}
{226} (#0, #0, #0, #0), {Keyboard LeftAlt}
{227} (#0, #0, #0, #0), {Keyboard Left GUI}
{228} (#0, #0, #0, #0), {Keyboard RightControl}
{229} (#0, #0, #0, #0), {Keyboard RightShift}
{230} (#0, #0, #0, #0), {Keyboard RightAlt}
{231} (#0, #0, #0, #0), {Keyboard Right GUI}
{232} (#0, #0, #0, #0), {Reserved}
{233} (#0, #0, #0, #0), {Reserved}
{234} (#0, #0, #0, #0), {Reserved}
{235} (#0, #0, #0, #0), {Reserved}
{236} (#0, #0, #0, #0), {Reserved}
{237} (#0, #0, #0, #0), {Reserved}
{238} (#0, #0, #0, #0), {Reserved}
{239} (#0, #0, #0, #0), {Reserved}
{240} (#0, #0, #0, #0), {Reserved}
{241} (#0, #0, #0, #0), {Reserved}
{242} (#0, #0, #0, #0), {Reserved}
{243} (#0, #0, #0, #0), {Reserved}
{244} (#0, #0, #0, #0), {Reserved}
{245} (#0, #0, #0, #0), {Reserved}
{246} (#0, #0, #0, #0), {Reserved}
{247} (#0, #0, #0, #0), {Reserved}
{248} (#0, #0, #0, #0), {Reserved}
{249} (#0, #0, #0, #0), {Reserved}
{250} (#0, #0, #0, #0), {Reserved}
{251} (#0, #0, #0, #0), {Reserved}
{252} (#0, #0, #0, #0), {Reserved}
{253} (#0, #0, #0, #0), {Reserved}
{254} (#0, #0, #0, #0), {Reserved}
{255} (#0, #0, #0, #0) {Reserved (256 to 65535 Reserved)}
);
key_enter=141; //USB_HID_BOOT_USAGE_ID[40,0]; //141;
key_escape=155; //USB_HID_BOOT_USAGE_ID[41,0]; //155;
key_backspace=136; //USB_HID_BOOT_USAGE_ID[42,0]; //136;
key_tab=137; //USB_HID_BOOT_USAGE_ID[43,0]; //137;
key_f1=186; //USB_HID_BOOT_USAGE_ID[58,0]; //186;
key_f2=187; //USB_HID_BOOT_USAGE_ID[59,0]; //187;
key_f3=188; //USB_HID_BOOT_USAGE_ID[60,0]; //188;
key_f4=189; //USB_HID_BOOT_USAGE_ID[61,0]; //189;
key_f5=190; //USB_HID_BOOT_USAGE_ID[62,0]; //190;
key_f6=191; //USB_HID_BOOT_USAGE_ID[63,0]; //191;
key_f7=192; //USB_HID_BOOT_USAGE_ID[64,0]; //192;
key_f8=193; //USB_HID_BOOT_USAGE_ID[65,0]; //193;
key_f9=194; //USB_HID_BOOT_USAGE_ID[66,0]; //194;
key_f10=195; //USB_HID_BOOT_USAGE_ID[67,0]; //195;
key_f11=196; //USB_HID_BOOT_USAGE_ID[68,0]; //196;
key_f12=197; //USB_HID_BOOT_USAGE_ID[69,0]; //197;
key_rightarrow=206;//USB_HID_BOOT_USAGE_ID[79,0]; //206;
key_leftarrow=207; //USB_HID_BOOT_USAGE_ID[80,0]; //207;
key_downarrow=208; //USB_HID_BOOT_USAGE_ID[81,0]; //208;
key_uparrow=209; //USB_HID_BOOT_USAGE_ID[82,0]; //209;
// ------------------- end of patch -------------------------------------------
USB_HID_BOOT_USAGE_NUMLOCK = 83;
USB_HID_BOOT_USAGE_CAPSLOCK = 57;
USB_HID_BOOT_USAGE_SCROLLLOCK = 71;
{==============================================================================}
type
{Keyboard specific types}
{Keyboard Data}
PKeyboardData = ^TKeyboardData;
TKeyboardData = record
Modifiers:LongWord; {Keyboard modifier flags for Shift, Alt, Control etc (eg KEYBOARD_LEFT_CTRL)}
ScanCode:Word; {Untranslated scan code value from keyboard (See SCAN_CODE_* constants)}
KeyCode:Word; {Translated key code value from keyboard (See KEY_CODE_* constants)}
CharCode:Char; {ANSI character representing the translatered key code}
CharUnicode:WideChar; {Unicode character representing the translatered key code}
end;
{Keyboard Buffer}
PKeyboardBuffer = ^TKeyboardBuffer;
TKeyboardBuffer = record
Wait:TSemaphoreHandle; {Data ready semaphore}
Start:LongWord; {Index of first buffer ready}
Count:LongWord; {Number of messages ready in buffer}
Buffer:array[0..(KEYBOARD_BUFFER_SIZE - 1)] of TKeyboardData;
end;
{Keyboard Device}
PKeyboardDevice = ^TKeyboardDevice;
{Keyboard Enumeration Callback}
TKeyboardEnumerate = function(Keyboard:PKeyboardDevice;Data:Pointer):LongWord;
{Keyboard Notification Callback}
TKeyboardNotification = function(Device:PDevice;Data:Pointer;Notification:LongWord):LongWord;
{Keyboard Device Methods}
TKeyboardDeviceGet = function(Keyboard:PKeyboardDevice;var KeyCode:Word):LongWord;
TKeyboardDeviceRead = function(Keyboard:PKeyboardDevice;Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
TKeyboardDeviceControl = function(Keyboard:PKeyboardDevice;Request:Integer;Argument1:LongWord;var Argument2:LongWord):LongWord;
TKeyboardDevice = record
{Device Properties}
Device:TDevice; {The Device entry for this Keyboard}
{Keyboard Properties}
KeyboardId:LongWord; {Unique Id of this Keyboard in the Keyboard table}
KeyboardState:LongWord; {Keyboard state (eg KEYBOARD_STATE_ATTACHED)}
KeyboardLEDs:LongWord; {Keyboard LEDs (eg KEYBOARD_LED_NUMLOCK)}
KeyboardRate:LongWord; {Keyboard repeat rate}
KeyboardDelay:LongWord; {Keyboard repeat delay}
DeviceGet:TKeyboardDeviceGet; {A Device specific DeviceGet method implementing a standard Keyboard device interface (Or nil if the default method is suitable)}
DeviceRead:TKeyboardDeviceRead; {A Device specific DeviceRead method implementing a standard Keyboard device interface (Or nil if the default method is suitable)}
DeviceControl:TKeyboardDeviceControl;{A Device specific DeviceControl method implementing a standard Keyboard device interface (Or nil if the default method is suitable)}
{Driver Properties}
Lock:TMutexHandle; {Keyboard lock}
Code:Word; {Scan code of current deadkey (If Applicable)}
Index:Word; {Index state for current deadkey (If Applicable)}
Modifiers:LongWord; {Modifier state for current deadkey (If Applicable)}
Buffer:TKeyboardBuffer; {Keyboard input buffer}
{Statistics Properties}
ReceiveCount:LongWord;
ReceiveErrors:LongWord;
BufferOverruns:LongWord;
{Internal Properties}
Prev:PKeyboardDevice; {Previous entry in Keyboard table}
Next:PKeyboardDevice; {Next entry in Keyboard table}
end;
{==============================================================================}
type
{USB Keyboard specific types}
PUSBKeyboardReport = ^TUSBKeyboardReport;
TUSBKeyboardReport = array[0..7] of Byte;
PUSBKeyboardDevice = ^TUSBKeyboardDevice;
TUSBKeyboardDevice = record
{Keyboard Properties}
Keyboard:TKeyboardDevice;
{USB Properties}
HIDInterface:PUSBInterface; {USB HID Keyboard Interface}
ReportRequest:PUSBRequest; {USB request for keyboard report data}
ReportEndpoint:PUSBEndpointDescriptor; {USB Keyboard Interrupt IN Endpoint}
LastCode:Word; {The scan code of the last key pressed}
LastCount:LongWord; {The repeat count of the last key pressed}
LastReport:TUSBKeyboardReport; {The last keyboard report received}
PendingCount:LongWord; {Number of USB requests pending for this keyboard}
WaiterThread:TThreadId; {Thread waiting for pending requests to complete (for keyboard detachment)}
end;
{==============================================================================}
{var}
{Keyboard specific variables}
{==============================================================================}
{var}
{USB Keyboard specific variables}
{==============================================================================}
{Initialization Functions}
procedure KeyboardInit;
{==============================================================================}
{Keyboard Functions}
function KeyboardGet(var KeyCode:Word):LongWord;
function KeyboardPeek:LongWord;
function KeyboardRead(Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord; inline;
function KeyboardReadEx(Buffer:Pointer;Size,Flags:LongWord;var Count:LongWord):LongWord;
function KeyboardPut(ScanCode,KeyCode:Word;Modifiers:LongWord):LongWord;
function KeyboardWrite(Buffer:Pointer;Size,Count:LongWord):LongWord;
function KeyboardFlush:LongWord;
function KeyboardDeviceGet(Keyboard:PKeyboardDevice;var KeyCode:Word):LongWord;
function KeyboardDeviceRead(Keyboard:PKeyboardDevice;Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
function KeyboardDeviceControl(Keyboard:PKeyboardDevice;Request:Integer;Argument1:LongWord;var Argument2:LongWord):LongWord;
function KeyboardDeviceSetState(Keyboard:PKeyboardDevice;State:LongWord):LongWord;
function KeyboardDeviceCreate:PKeyboardDevice;
function KeyboardDeviceCreateEx(Size:LongWord):PKeyboardDevice;
function KeyboardDeviceDestroy(Keyboard:PKeyboardDevice):LongWord;
function KeyboardDeviceRegister(Keyboard:PKeyboardDevice):LongWord;
function KeyboardDeviceDeregister(Keyboard:PKeyboardDevice):LongWord;
function KeyboardDeviceFind(KeyboardId:LongWord):PKeyboardDevice;
function KeyboardDeviceFindByName(const Name:String):PKeyboardDevice; inline;
function KeyboardDeviceFindByDescription(const Description:String):PKeyboardDevice; inline;
function KeyboardDeviceEnumerate(Callback:TKeyboardEnumerate;Data:Pointer):LongWord;
function KeyboardDeviceNotification(Keyboard:PKeyboardDevice;Callback:TKeyboardNotification;Data:Pointer;Notification,Flags:LongWord):LongWord;
{==============================================================================}
{RTL Console Functions}
function SysConsoleGetKey(var ACh:Char;AUserData:Pointer):Boolean;
function SysConsolePeekKey(var ACh:Char;AUserData:Pointer):Boolean;
function SysConsoleReadChar(var ACh:Char;AUserData:Pointer):Boolean;
function SysConsoleReadWideChar(var ACh:WideChar;AUserData:Pointer):Boolean;
{==============================================================================}
{USB Keyboard Functions}
function USBKeyboardDeviceRead(Keyboard:PKeyboardDevice;Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
function USBKeyboardDeviceControl(Keyboard:PKeyboardDevice;Request:Integer;Argument1:LongWord;var Argument2:LongWord):LongWord;
function USBKeyboardDriverBind(Device:PUSBDevice;Interrface:PUSBInterface):LongWord;
function USBKeyboardDriverUnbind(Device:PUSBDevice;Interrface:PUSBInterface):LongWord;
procedure USBKeyboardReportWorker(Request:PUSBRequest);
procedure USBKeyboardReportComplete(Request:PUSBRequest);
{==============================================================================}
{Keyboard Helper Functions}
function KeyboardGetCount:LongWord; inline;
function KeyboardDeviceCheck(Keyboard:PKeyboardDevice):PKeyboardDevice;
function KeyboardDeviceTypeToString(KeyboardType:LongWord):String;
function KeyboardDeviceStateToString(KeyboardState:LongWord):String;
function KeyboardDeviceStateToNotification(State:LongWord):LongWord;
function KeyboardRemapCtrlCode(KeyCode,CharCode:Word):Word;
function KeyboardRemapKeyCode(ScanCode,KeyCode:Word;var CharCode:Byte;Modifiers:LongWord):Boolean;
function KeyboardRemapScanCode(ScanCode,KeyCode:Word;var CharCode:Byte;Modifiers:LongWord):Boolean;
procedure KeyboardLog(Level:LongWord;Keyboard:PKeyboardDevice;const AText:String);
procedure KeyboardLogInfo(Keyboard:PKeyboardDevice;const AText:String);
procedure KeyboardLogError(Keyboard:PKeyboardDevice;const AText:String);
procedure KeyboardLogDebug(Keyboard:PKeyboardDevice;const AText:String);
{==============================================================================}
{USB Helper Functions}
function USBKeyboardInsertData(Keyboard:PUSBKeyboardDevice;Data:PKeyboardData):LongWord;
function USBKeyboardCheckPressed(Keyboard:PUSBKeyboardDevice;ScanCode:Byte):Boolean;
function USBKeyboardCheckRepeated(Keyboard:PUSBKeyboardDevice;ScanCode:Byte):Boolean;
function USBKeyboardCheckReleased(Keyboard:PUSBKeyboardDevice;Report:PUSBKeyboardReport;ScanCode:Byte):Boolean;
function USBKeyboardDeviceSetLEDs(Keyboard:PUSBKeyboardDevice;LEDs:Byte):LongWord;
function USBKeyboardDeviceSetIdle(Keyboard:PUSBKeyboardDevice;Duration,ReportId:Byte):LongWord;
function USBKeyboardDeviceSetProtocol(Keyboard:PUSBKeyboardDevice;Protocol:Byte):LongWord;
{==============================================================================}
{==============================================================================}
// ---------------------------------patch added by pik33 @ 20161123 ------------
type TKeyboardreport=array[0..7] of byte;
var report_buffer: array[0..512] of byte;
rb_start:integer=0;
rb_end:integer=0;
report_buffer_active:boolean=false;
function getkeyboardreport:TKeyboardreport;
procedure startreportbuffer;
procedure stopreportbuffer;
function translatescantochar(scan,shift:byte):char;
implementation
procedure stopreportbuffer;
begin
report_buffer_active:=false;
end;
procedure startreportbuffer;
begin
report_buffer_active:=true;
end;
function getkeyboardreport:TKeyboardreport;
var ii:integer;
begin
if rb_end <>rb_start then begin
for ii:=0 to 7 do result[ii]:=report_buffer[8*rb_start+ii];
rb_start:=(rb_start+1) and $3F;
end
else
for ii:=0 to 7 do result[ii]:=255;
end;
function translatescantochar(scan,shift:byte):char;
begin
if shift=0 then result:=USB_HID_BOOT_USAGE_ID[scan,0]
else if shift=1 then result:=USB_HID_BOOT_USAGE_ID[scan,1]
else if shift=2 then result:=USB_HID_BOOT_USAGE_ID[scan,2]
else if shift=3 then result:=USB_HID_BOOT_USAGE_ID[scan,3]
else result:=USB_HID_BOOT_USAGE_ID[scan,0];
end;
// --------- end of patch ------------------------------------------------------
{==============================================================================}
{==============================================================================}
var
{Keyboard specific variables}
KeyboardInitialized:Boolean;
KeyboardTable:PKeyboardDevice;
KeyboardTableLock:TCriticalSectionHandle = INVALID_HANDLE_VALUE;
KeyboardTableCount:LongWord;
KeyboardBuffer:PKeyboardBuffer; {Global keyboard input buffer}
KeyboardBufferLock:TMutexHandle = INVALID_HANDLE_VALUE; {Global keyboard buffer lock}
{==============================================================================}
{==============================================================================}
var
{RTL Console specific variables}
SysConsoleLastCode:Byte;
{==============================================================================}
{==============================================================================}
var
{USB Keyboard specific variables}
USBKeyboardDriver:PUSBDriver; {USB Keyboard Driver interface (Set by KeyboardInit)}
{==============================================================================}
{==============================================================================}
{Initialization Functions}
procedure KeyboardInit;
{Initialize the keyboard unit, device table and USB keyboard driver}
{Note: Called only during system startup}
var
Status:LongWord;
begin
{}
{Check Initialized}
if KeyboardInitialized then Exit;
{Initialize Logging}
KEYBOARD_LOG_ENABLED:=(KEYBOARD_DEFAULT_LOG_LEVEL <> KEYBOARD_LOG_LEVEL_NONE);
{Initialize Keyboard Table}
KeyboardTable:=nil;
KeyboardTableLock:=CriticalSectionCreate;
KeyboardTableCount:=0;
if KeyboardTableLock = INVALID_HANDLE_VALUE then
begin
if KEYBOARD_LOG_ENABLED then KeyboardLogError(nil,'Failed to create keyboard table lock');
end;
{Initialize Keyboard Buffer}
KeyboardBuffer:=AllocMem(SizeOf(TKeyboardBuffer));
KeyboardBufferLock:=INVALID_HANDLE_VALUE;
if KeyboardBuffer = nil then
begin
if KEYBOARD_LOG_ENABLED then KeyboardLogError(nil,'Failed to allocate keyboard buffer');
end
else
begin
{Create Semaphore}
KeyboardBuffer.Wait:=SemaphoreCreate(0);
if KeyboardBuffer.Wait = INVALID_HANDLE_VALUE then
begin
if KEYBOARD_LOG_ENABLED then KeyboardLogError(nil,'Failed to create keyboard buffer semaphore');
end;
{Create Lock}
KeyboardBufferLock:=MutexCreate;
if KeyboardBufferLock = INVALID_HANDLE_VALUE then
begin
if KEYBOARD_LOG_ENABLED then KeyboardLogError(nil,'Failed to create keyboard buffer lock');
end;
end;
{Create USB Keyboard Driver}
USBKeyboardDriver:=USBDriverCreate;
if USBKeyboardDriver <> nil then
begin
{Update USB Keyboard Driver}
{Driver}
USBKeyboardDriver.Driver.DriverName:=USBKEYBOARD_DRIVER_NAME;
{USB}
USBKeyboardDriver.DriverBind:=USBKeyboardDriverBind;
USBKeyboardDriver.DriverUnbind:=USBKeyboardDriverUnbind;
{Register USB Keyboard Driver}
Status:=USBDriverRegister(USBKeyboardDriver);
if Status <> USB_STATUS_SUCCESS then
begin
if USB_LOG_ENABLED then USBLogError(nil,'Keyboard: Failed to register USB keyboard driver: ' + USBStatusToString(Status));
end;
end
else
begin
if KEYBOARD_LOG_ENABLED then KeyboardLogError(nil,'Failed to create USB keyboard driver');
end;
{Setup Platform Console Handlers}
ConsoleGetKeyHandler:=SysConsoleGetKey;
ConsolePeekKeyHandler:=SysConsolePeekKey;
ConsoleReadCharHandler:=SysConsoleReadChar;
ConsoleReadWideCharHandler:=SysConsoleReadWideChar;
KeyboardInitialized:=True;
end;
{==============================================================================}
{==============================================================================}
{Keyboard Functions}
function KeyboardGet(var KeyCode:Word):LongWord;
{Get the first key code from the global keyboard buffer}
{KeyCode: The returned key code read from the buffer (eg KEY_CODE_A)}
{Return: ERROR_SUCCESS if completed or another error code on failure}
{Note: Key code is the value translated from the scan code using the current keymap
it may not be a character code and it may include non printable characters.
To translate a key code to a character call KeymapGetCharCode()}
var
Count:LongWord;
Data:TKeyboardData;
begin
{}
Result:=KeyboardReadEx(@Data,SizeOf(TKeyboardData),KEYBOARD_FLAG_NONE,Count);
while Result = ERROR_SUCCESS do
begin
{Exclude Key Up and Dead Key events}
if (Data.Modifiers and (KEYBOARD_KEYUP or KEYBOARD_DEADKEY)) = 0 then
begin
KeyCode:=Data.KeyCode;
Break;
end;
{Get Next Key}
Result:=KeyboardReadEx(@Data,SizeOf(TKeyboardData),KEYBOARD_FLAG_NONE,Count);
end;
end;
{==============================================================================}
function KeyboardPeek:LongWord;
{Peek at the global keyboard buffer to see if any data packets are ready}
{Return: ERROR_SUCCESS if packets are ready, ERROR_NO_MORE_ITEMS if not or another error code on failure}
var
Count:LongWord;
Data:TKeyboardData;
begin
{}
Result:=KeyboardReadEx(@Data,SizeOf(TKeyboardData),KEYBOARD_FLAG_NON_BLOCK or KEYBOARD_FLAG_PEEK_BUFFER,Count);
end;
{==============================================================================}
function KeyboardRead(Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord; inline;
{Read keyboard data packets from the global keyboard buffer}
{Buffer: Pointer to a buffer to copy the keyboard data packets to}
{Size: The size of the buffer in bytes (Must be at least TKeyboardData or greater)}
{Count: The number of keyboard data packets copied to the buffer}
{Return: ERROR_SUCCESS if completed or another error code on failure}
begin
{}
Result:=KeyboardReadEx(Buffer,Size,KEYBOARD_FLAG_NONE,Count);
end;
{==============================================================================}
function KeyboardReadEx(Buffer:Pointer;Size,Flags:LongWord;var Count:LongWord):LongWord;
{Read keyboard data packets from the global keyboard buffer}
{Buffer: Pointer to a buffer to copy the keyboard data packets to}
{Size: The size of the buffer in bytes (Must be at least TKeyboardData or greater)}
{Flags: The flags to use for the read (eg KEYBOARD_FLAG_NON_BLOCK)}
{Count: The number of keyboard data packets copied to the buffer}
{Return: ERROR_SUCCESS if completed or another error code on failure}
var
Offset:PtrUInt;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check Buffer}
if Buffer = nil then Exit;
{Check Size}
if Size < SizeOf(TKeyboardData) then Exit;
{$IFDEF KEYBOARD_DEBUG}
if KEYBOARD_LOG_ENABLED then KeyboardLogDebug(nil,'Attempting to read ' + IntToStr(Size) + ' bytes from keyboard');
{$ENDIF}
{Read to Buffer}
Count:=0;
Offset:=0;
while Size >= SizeOf(TKeyboardData) do
begin
{Check Non Blocking}
if ((Flags and KEYBOARD_FLAG_NON_BLOCK) <> 0) and (KeyboardBuffer.Count = 0) then
begin
if Count = 0 then Result:=ERROR_NO_MORE_ITEMS;
Break;
end;
{Check Peek Buffer}
if (Flags and KEYBOARD_FLAG_PEEK_BUFFER) <> 0 then
begin
{Acquire the Lock}
if MutexLock(KeyboardBufferLock) = ERROR_SUCCESS then
begin
try
if KeyboardBuffer.Count > 0 then
begin
{Copy Data}
PKeyboardData(PtrUInt(Buffer) + Offset)^:=KeyboardBuffer.Buffer[KeyboardBuffer.Start];
{Update Count}
Inc(Count);
Result:=ERROR_SUCCESS;
Break;
end
else
begin
Result:=ERROR_NO_MORE_ITEMS;
Break;
end;
finally
{Release the Lock}