This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathkeyboard.js
2329 lines (2192 loc) · 97.8 KB
/
keyboard.js
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
/**
* @fileoverview Implements the PCjs Keyboard component.
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @version 1.0
* Created 2012-Jun-20
*
* Copyright © 2012-2016 Jeff Parsons <[email protected]>
*
* This file is part of PCjs, which is part of the JavaScript Machines Project (aka JSMachines)
* at <http://jsmachines.net/> and <http://pcjs.org/>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every source code file of every
* copy or modified version of this work, and to display that copyright notice on every screen
* that loads or runs any version of this software (see Computer.COPYRIGHT).
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of the
* PCjs program for purposes of the GNU General Public License, and the author does not claim
* any copyright as to their contents.
*/
"use strict";
if (NODE) {
var str = require("../../shared/lib/strlib");
var web = require("../../shared/lib/weblib");
var Component = require("../../shared/lib/component");
var Messages = require("./messages");
var ChipSet = require("./chipset");
var State = require("./state");
var CPU = require("./cpu");
}
/**
* Keyboard(parmsKbd)
*
* The Keyboard component can be configured with the following (parmsKbd) properties:
*
* model: keyboard model string, which must match one of the values listed in Keyboard.MODELS:
*
* "US83" (default)
* "US84"
* "US101"
*
* Its main purpose is to receive binding requests for various keyboard events, and to use those events
* to simulate the PC's keyboard hardware.
*
* @constructor
* @extends Component
* @param {Object} parmsKbd
*/
function Keyboard(parmsKbd)
{
Component.call(this, "Keyboard", parmsKbd, Keyboard, Messages.KEYBOARD);
this.setModel(parmsKbd['model']);
this.fMobile = web.isMobile();
this.fMSIE = web.isUserAgent("MSIE");
this.printMessage("mobile keyboard support: " + (this.fMobile? "true" : "false"));
/*
* This is count of the number of "soft keyboard" keys present. At the moment, its only
* purpose is to signal findBinding() whether to waste any time looking for SOFTCODE matches.
*/
this.cSoftCodes = 0;
/*
* Updated by onFocusChange()
*/
this.fHasFocus = true;
/*
* This is true whenever the physical Escape key is disabled (eg, by pointer locking code),
* giving us the opportunity to map a different physical key to machine's virtual Escape key.
*/
this.fEscapeDisabled = false;
/*
* This is set whenever we notice a discrepancy between our internal CAPS_LOCK state and its
* apparent state; we check whenever aKeysActive has been emptied.
*/
this.fToggleCapsLock = false;
/*
* New unified approach to key event processing: When we process a key on the "down" event,
* we check the aKeysActive array: if the key is already active, do nothing; otherwise, insert
* it into the table, generate the "make" scan code(s), and set a timeout for "repeat" if it's
* a repeatable key (most are).
*
* Similarly, when a key goes "up", if it's already not active, do nothing; otherwise, generate
* the "break" scan code(s), cancel any pending timeout, and remove it from the active key table.
*
* If a "press" event is received, then if the key is already active, remove it and (re)insert
* it at the head of the table, generate the "make" scan code(s), set nRepeat to -1, and set a
* timeout for "break".
*
* This requires an aKeysActive array that keeps track of the status of every active key; only the
* first entry in the array is allowed to repeat. Each entry is a key object with the following
* properties:
*
* simCode: our simulated keyCode from onKeyDown, onKeyUp, or onKeyPress
* fDown: next state to simulate (true for down, false for up)
* nRepeat: > 0 if timer should generate more "make" scan code(s), -1 for "break" scan code(s)
* timer: timer for next key operation, if any
*
* Keys are inserted at the head of aKeysActive, using splice(0, 0, key), but not before zeroing
* nRepeat of any repeating key that already occupies the head (index 0), so that at most only one
* key (ie, the most recent) will ever be in a repeating state.
*
* IBM PC keyboard repeat behavior: when pressing CTRL, then C, and then releasing CTRL while still
* holding C, the repeated CTRL_C characters turn into 'c' characters. We emulate that behavior.
* However, when pressing C, then CTRL, all repeating stops: not a single CTRL_C is generated, and
* even if the CTRL is released before the C, no more more 'c' characters are generated either.
* We do NOT fully emulate that behavior -- we DO stop the repeating, but we also generate one CTRL_C.
* More investigation is required, because I need to confirm whether the IBM keyboard automatically
* "breaks" all non-shift keys before it "makes" the CTRL.
*/
this.aKeysActive = [];
this.msAutoRepeat = 500;
this.msNextRepeat = 100;
this.msAutoRelease = 50;
this.msInjectDelay = 300; // number of milliseconds between injected keystrokes
/*
* HACK: We set fAllDown to false to ignore all down/up events for keys not explicitly marked as ONDOWN;
* even though that prevents those keys from being repeated properly (ie, at the simulation's repeat rate
* rather than the browser's repeat rate), it's the safest thing to do when dealing with international keyboards,
* because our mapping tables are designed for US keyboards, and testing all the permutations of international
* keyboards and web browsers is more work than I can take on right now. TODO: Dig into this some day.
*/
this.fAllDown = false;
this.setReady();
}
Component.subclass(Keyboard);
/*
* Supported keyboard models (the first entry is the default if the specified model isn't recognized)
*/
Keyboard.MODELS = ["US83", "US84", "US101"];
/**
* Alphanumeric and other common (printable) ASCII codes.
*
* TODO: Determine what we can do to get ALL constants like these inlined (enum doesn't seem to
* get the job done); the problem seems to be limited to property references that use quotes, which
* is why I've 'unquoted' as many of them as possible.
*
* @enum {number}
*/
Keyboard.ASCII = {
CTRL_A: 1, CTRL_C: 3, CTRL_Z: 26,
' ': 32, '!': 33, '"': 34, '#': 35, '$': 36, '%': 37, '&': 38, "'": 39,
'(': 40, ')': 41, '*': 42, '+': 43, ',': 44, '-': 45, '.': 46, '/': 47,
'0': 48, '1': 49, '2': 50, '3': 51, '4': 52, '5': 53, '6': 54, '7': 55,
'8': 56, '9': 57, ':': 58, ';': 59, '<': 60, '=': 61, '>': 62, '?': 63,
'@': 64, A: 65, B: 66, C: 67, D: 68, E: 69, F: 70, G: 71,
H: 72, I: 73, J: 74, K: 75, L: 76, M: 77, N: 78, O: 79,
P: 80, Q: 81, R: 82, S: 83, T: 84, U: 85, V: 86, W: 87,
X: 88, Y: 89, Z: 90, '[': 91, '\\':92, ']': 93, '^': 94, '_': 95,
'`': 96, a: 97, b: 98, c: 99, d: 100, e: 101, f: 102, g: 103,
h: 104, i: 105, j: 106, k: 107, l: 108, m: 109, n: 110, o: 111,
p: 112, q: 113, r: 114, s: 115, t: 116, u: 117, v: 118, w: 119,
x: 120, y: 121, z: 122, '{':123, '|':124, '}':125, '~':126
};
/**
* Browser keyCodes we must pay particular attention to. For the most part, these are non-alphanumeric
* or function keys, some which may require special treatment (eg, preventDefault() if returning false on
* the initial keyDown event is insufficient).
*
* keyCodes for most common ASCII keys can simply use the appropriate ASCII code above.
*
* Most of these represent non-ASCII keys (eg, the LEFT arrow key), yet for some reason, browsers defined
* them using ASCII codes (eg, the LEFT arrow key uses the ASCII code for '%' or 37). This conflict is
* discussed further in the definition of CLICKCODE below.
*
* @enum {number}
*/
Keyboard.KEYCODE = {
/* 0x08 */ BS: 8,
/* 0x09 */ TAB: 9,
/* 0x0A */ LF: 10,
/* 0x0D */ CR: 13,
/* 0x10 */ SHIFT: 16,
/* 0x11 */ CTRL: 17,
/* 0x12 */ ALT: 18,
/* 0x13 */ PAUSE: 19, // PAUSE/BREAK
/* 0x14 */ CAPS_LOCK: 20,
/* 0x1B */ ESC: 27,
/* 0x20 */ SPACE: 32,
/* 0x21 */ PGUP: 33,
/* 0x22 */ PGDN: 34,
/* 0x23 */ END: 35,
/* 0x24 */ HOME: 36,
/* 0x25 */ LEFT: 37,
/* 0x26 */ UP: 38,
/* 0x27 */ RIGHT: 39,
/* 0x27 */ FF_QUOTE: 39,
/* 0x28 */ DOWN: 40,
/* 0x2C */ FF_COMMA: 44,
/* 0x2C */ PRTSC: 44,
/* 0x2D */ INS: 45,
/* 0x2E */ DEL: 46,
/* 0x2E */ FF_PERIOD: 46,
/* 0x2F */ FF_SLASH: 47,
/* 0x3B */ FF_SEMI: 59,
/* 0x3D */ FF_EQUALS: 61,
/* 0x5B */ CMD: 91, // aka WIN
/* 0x5B */ FF_LBRACK: 91,
/* 0x5C */ FF_BSLASH: 92,
/* 0x5D */ RCMD: 93, // aka MENU
/* 0x5D */ FF_RBRACK: 93,
/* 0x60 */ NUM_INS: 96, // 0
/* 0x60 */ FF_BQUOTE: 96,
/* 0x61 */ NUM_END: 97, // 1
/* 0x62 */ NUM_DOWN: 98, // 2
/* 0x63 */ NUM_PGDN: 99, // 3
/* 0x64 */ NUM_LEFT: 100, // 4
/* 0x65 */ NUM_CENTER: 101, // 5
/* 0x66 */ NUM_RIGHT: 102, // 6
/* 0x67 */ NUM_HOME: 103, // 7
/* 0x68 */ NUM_UP: 104, // 8
/* 0x69 */ NUM_PGUP: 105, // 9
/* 0x6A */ NUM_MUL: 106,
/* 0x6B */ NUM_ADD: 107,
/* 0x6D */ NUM_SUB: 109,
/* 0x6E */ NUM_DEL: 110, // .
/* 0x6F */ NUM_DIV: 111,
/* 0x70 */ F1: 112,
/* 0x71 */ F2: 113,
/* 0x72 */ F3: 114,
/* 0x73 */ F4: 115,
/* 0x74 */ F5: 116,
/* 0x75 */ F6: 117,
/* 0x76 */ F7: 118,
/* 0x77 */ F8: 119,
/* 0x78 */ F9: 120,
/* 0x79 */ F10: 121,
/* 0x7A */ F11: 122,
/* 0x7B */ F12: 123,
/* 0x90 */ NUM_LOCK: 144,
/* 0x91 */ SCROLL_LOCK: 145,
/* 0xAD */ FF_DASH: 173,
/* 0xBA */ SEMI: 186, // Firefox: 59
/* 0xBB */ EQUALS: 187, // Firefox: 61
/* 0xBC */ COMMA: 188, // Firefox: 44
/* 0xBD */ DASH: 189, // Firefox: 173
/* 0xBE */ PERIOD: 190, // Firefox: 46
/* 0xBF */ SLASH: 191, // Firefox: 47
/* 0xC0 */ BQUOTE: 192, // Firefox: 96
/* 0xDB */ LBRACK: 219, // Firefox: 91
/* 0xDC */ BSLASH: 220, // Firefox: 92
/* 0xDD */ RBRACK: 221, // Firefox: 93
/* 0xDE */ QUOTE: 222, // Firefox: 39
/* 0xE0 */ FF_CMD: 224, // Firefox only (used for both CMD and RCMD)
//
// The following biases use what I'll call Decimal Coded Binary or DCB (the opposite of BCD),
// where the thousands digit is used to store the sum of "binary" digits 1 and/or 2 and/or 4.
//
// Technically, that makes it DCO (Decimal Coded Octal), but then again, BCD should have really
// been called HCD (Hexadecimal Coded Decimal), so if "they" can take liberties, so can I.
//
// ONDOWN is a bias we add to browser keyCodes that we want to handle on "down" rather than on "press".
//
ONDOWN: 1000,
//
// ONRIGHT is a bias we add to browser keyCodes that need to check for a "right" location (default is "left")
//
ONRIGHT: 2000,
//
// FAKE is a bias we add to signal these are fake keyCodes corresponding to internal keystroke combinations.
// The actual values are for internal use only and merely need to be unique and used consistently.
//
FAKE: 4000
};
/*
* Maps "stupid" keyCodes to their "non-stupid" counterparts
*/
Keyboard.STUPID_KEYCODES = {};
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.SEMI] = Keyboard.ASCII[';']; // 186 -> 59
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.EQUALS] = Keyboard.ASCII['=']; // 187 -> 61
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.COMMA] = Keyboard.ASCII[',']; // 188 -> 44
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.DASH] = Keyboard.ASCII['-']; // 189 -> 45
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.PERIOD] = Keyboard.ASCII['.']; // 190 -> 46
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.SLASH] = Keyboard.ASCII['/']; // 191 -> 47
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.BQUOTE] = Keyboard.ASCII['`']; // 192 -> 96
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.LBRACK] = Keyboard.ASCII['[']; // 219 -> 91
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.BSLASH] = Keyboard.ASCII['\\']; // 220 -> 92
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.RBRACK] = Keyboard.ASCII[']']; // 221 -> 93
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.QUOTE] = Keyboard.ASCII["'"]; // 222 -> 39
Keyboard.STUPID_KEYCODES[Keyboard.KEYCODE.FF_DASH] = Keyboard.ASCII['-'];
/*
* Maps unshifted keyCodes to their shifted counterparts; to be used when a shift-key is down.
* Alphabetic characters are handled in code, since they must also take CAPS_LOCK into consideration.
*/
Keyboard.SHIFTED_KEYCODES = {};
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['1']] = Keyboard.ASCII['!'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['2']] = Keyboard.ASCII['@'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['3']] = Keyboard.ASCII['#'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['4']] = Keyboard.ASCII['$'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['5']] = Keyboard.ASCII['%'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['6']] = Keyboard.ASCII['^'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['7']] = Keyboard.ASCII['&'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['8']] = Keyboard.ASCII['*'];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['9']] = Keyboard.ASCII['('];
Keyboard.SHIFTED_KEYCODES[Keyboard.ASCII['0']] = Keyboard.ASCII[')'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.SEMI] = Keyboard.ASCII[':'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.EQUALS] = Keyboard.ASCII['+'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.COMMA] = Keyboard.ASCII['<'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.DASH] = Keyboard.ASCII['_'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.PERIOD] = Keyboard.ASCII['>'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.SLASH] = Keyboard.ASCII['?'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.BQUOTE] = Keyboard.ASCII['~'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.LBRACK] = Keyboard.ASCII['{'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.BSLASH] = Keyboard.ASCII['|'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.RBRACK] = Keyboard.ASCII['}'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.QUOTE] = Keyboard.ASCII['"'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.FF_DASH] = Keyboard.ASCII['_'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.FF_EQUALS] = Keyboard.ASCII['+'];
Keyboard.SHIFTED_KEYCODES[Keyboard.KEYCODE.FF_SEMI] = Keyboard.ASCII[':'];
Keyboard.SIMCODE = {
BS: Keyboard.KEYCODE.BS + Keyboard.KEYCODE.ONDOWN,
TAB: Keyboard.KEYCODE.TAB + Keyboard.KEYCODE.ONDOWN,
SHIFT: Keyboard.KEYCODE.SHIFT + Keyboard.KEYCODE.ONDOWN,
RSHIFT: Keyboard.KEYCODE.SHIFT + Keyboard.KEYCODE.ONDOWN + Keyboard.KEYCODE.ONRIGHT,
CTRL: Keyboard.KEYCODE.CTRL + Keyboard.KEYCODE.ONDOWN,
ALT: Keyboard.KEYCODE.ALT + Keyboard.KEYCODE.ONDOWN,
CAPS_LOCK: Keyboard.KEYCODE.CAPS_LOCK + Keyboard.KEYCODE.ONDOWN,
ESC: Keyboard.KEYCODE.ESC + Keyboard.KEYCODE.ONDOWN,
/*
* It seems that a recent change to Safari on iOS (first noticed in iOS 9.1) treats SPACE
* differently now, at least with regard to <textarea> controls, and possibly only readonly
* or hidden controls, like the hidden <textarea> we overlay on the Video <canvas> element.
*
* Whatever the exact criteria are, Safari on iOS now performs SPACE's default behavior
* after the onkeydown event but before the onkeypress event. So we must now process SPACE
* as an ONDOWN key, so that we can call preventDefault() and properly simulate the key at
* the time the key goes down.
*/
SPACE: Keyboard.KEYCODE.SPACE + Keyboard.KEYCODE.ONDOWN,
F1: Keyboard.KEYCODE.F1 + Keyboard.KEYCODE.ONDOWN,
F2: Keyboard.KEYCODE.F2 + Keyboard.KEYCODE.ONDOWN,
F3: Keyboard.KEYCODE.F3 + Keyboard.KEYCODE.ONDOWN,
F4: Keyboard.KEYCODE.F4 + Keyboard.KEYCODE.ONDOWN,
F5: Keyboard.KEYCODE.F5 + Keyboard.KEYCODE.ONDOWN,
F6: Keyboard.KEYCODE.F6 + Keyboard.KEYCODE.ONDOWN,
F7: Keyboard.KEYCODE.F7 + Keyboard.KEYCODE.ONDOWN,
F8: Keyboard.KEYCODE.F8 + Keyboard.KEYCODE.ONDOWN,
F9: Keyboard.KEYCODE.F9 + Keyboard.KEYCODE.ONDOWN,
F10: Keyboard.KEYCODE.F10 + Keyboard.KEYCODE.ONDOWN,
F11: Keyboard.KEYCODE.F11 + Keyboard.KEYCODE.ONDOWN,
F12: Keyboard.KEYCODE.F12 + Keyboard.KEYCODE.ONDOWN,
NUM_LOCK: Keyboard.KEYCODE.NUM_LOCK + Keyboard.KEYCODE.ONDOWN,
SCROLL_LOCK: Keyboard.KEYCODE.SCROLL_LOCK + Keyboard.KEYCODE.ONDOWN,
PRTSC: Keyboard.KEYCODE.PRTSC + Keyboard.KEYCODE.ONDOWN,
HOME: Keyboard.KEYCODE.HOME + Keyboard.KEYCODE.ONDOWN,
UP: Keyboard.KEYCODE.UP + Keyboard.KEYCODE.ONDOWN,
PGUP: Keyboard.KEYCODE.PGUP + Keyboard.KEYCODE.ONDOWN,
NUM_SUB: Keyboard.KEYCODE.NUM_SUB + Keyboard.KEYCODE.ONDOWN,
LEFT: Keyboard.KEYCODE.LEFT + Keyboard.KEYCODE.ONDOWN,
NUM_CENTER: Keyboard.KEYCODE.NUM_CENTER + Keyboard.KEYCODE.ONDOWN,
RIGHT: Keyboard.KEYCODE.RIGHT + Keyboard.KEYCODE.ONDOWN,
NUM_ADD: Keyboard.KEYCODE.NUM_ADD + Keyboard.KEYCODE.ONDOWN,
END: Keyboard.KEYCODE.END + Keyboard.KEYCODE.ONDOWN,
DOWN: Keyboard.KEYCODE.DOWN + Keyboard.KEYCODE.ONDOWN,
PGDN: Keyboard.KEYCODE.PGDN + Keyboard.KEYCODE.ONDOWN,
INS: Keyboard.KEYCODE.INS + Keyboard.KEYCODE.ONDOWN,
DEL: Keyboard.KEYCODE.DEL + Keyboard.KEYCODE.ONDOWN,
CMD: Keyboard.KEYCODE.CMD + Keyboard.KEYCODE.ONDOWN,
RCMD: Keyboard.KEYCODE.RCMD + Keyboard.KEYCODE.ONDOWN,
FF_CMD: Keyboard.KEYCODE.FF_CMD + Keyboard.KEYCODE.ONDOWN,
CTRL_C: Keyboard.ASCII.CTRL_C + Keyboard.KEYCODE.FAKE,
CTRL_BREAK: Keyboard.KEYCODE.BS + Keyboard.KEYCODE.FAKE,
CTRL_ALT_DEL: Keyboard.KEYCODE.DEL + Keyboard.KEYCODE.FAKE,
SYSREQ: Keyboard.KEYCODE.ESC + Keyboard.KEYCODE.FAKE
};
/*
* Scan code constants
*/
Keyboard.SCANCODE = {
/* 0x01 */ ESC: 1,
/* 0x02 */ ONE: 2,
/* 0x03 */ TWO: 3,
/* 0x04 */ THREE: 4,
/* 0x05 */ FOUR: 5,
/* 0x06 */ FIVE: 6,
/* 0x07 */ SIX: 7,
/* 0x08 */ SEVEN: 8,
/* 0x09 */ EIGHT: 9,
/* 0x0A */ NINE: 10,
/* 0x0B */ ZERO: 11,
/* 0x0C */ DASH: 12,
/* 0x0D */ EQUALS: 13,
/* 0x0E */ BS: 14,
/* 0x0F */ TAB: 15,
/* 0x10 */ Q: 16,
/* 0x11 */ W: 17,
/* 0x12 */ E: 18,
/* 0x13 */ R: 19,
/* 0x14 */ T: 20,
/* 0x15 */ Y: 21,
/* 0x16 */ U: 22,
/* 0x17 */ I: 23,
/* 0x18 */ O: 24,
/* 0x19 */ P: 25,
/* 0x1A */ LBRACK: 26,
/* 0x1B */ RBRACK: 27,
/* 0x1C */ ENTER: 28,
/* 0x1D */ CTRL: 29,
/* 0x1E */ A: 30,
/* 0x1F */ S: 31,
/* 0x20 */ D: 32,
/* 0x21 */ F: 33,
/* 0x22 */ G: 34,
/* 0x23 */ H: 35,
/* 0x24 */ J: 36,
/* 0x25 */ K: 37,
/* 0x26 */ L: 38,
/* 0x27 */ SEMI: 39,
/* 0x28 */ QUOTE: 40,
/* 0x29 */ BQUOTE: 41,
/* 0x2A */ SHIFT: 42,
/* 0x2B */ BSLASH: 43,
/* 0x2C */ Z: 44,
/* 0x2D */ X: 45,
/* 0x2E */ C: 46,
/* 0x2F */ V: 47,
/* 0x30 */ B: 48,
/* 0x31 */ N: 49,
/* 0x32 */ M: 50,
/* 0x33 */ COMMA: 51,
/* 0x34 */ PERIOD: 52,
/* 0x35 */ SLASH: 53,
/* 0x36 */ RSHIFT: 54,
/* 0x37 */ PRTSC: 55, // unshifted '*'; becomes dedicated 'Print Screen' key on 101-key keyboards
/* 0x38 */ ALT: 56,
/* 0x39 */ SPACE: 57,
/* 0x3A */ CAPS_LOCK: 58,
/* 0x3B */ F1: 59,
/* 0x3C */ F2: 60,
/* 0x3D */ F3: 61,
/* 0x3E */ F4: 62,
/* 0x3F */ F5: 63,
/* 0x40 */ F6: 64,
/* 0x41 */ F7: 65,
/* 0x42 */ F8: 66,
/* 0x43 */ F9: 67,
/* 0x44 */ F10: 68,
/* 0x45 */ NUM_LOCK: 69,
/* 0x46 */ SCROLL_LOCK: 70,
/* 0x47 */ NUM_HOME: 71,
/* 0x48 */ NUM_UP: 72,
/* 0x49 */ NUM_PGUP: 73,
/* 0x4A */ NUM_SUB: 74,
/* 0x4B */ NUM_LEFT: 75,
/* 0x4C */ NUM_CENTER: 76,
/* 0x4D */ NUM_RIGHT: 77,
/* 0x4E */ NUM_ADD: 78,
/* 0x4F */ NUM_END: 79,
/* 0x50 */ NUM_DOWN: 80,
/* 0x51 */ NUM_PGDN: 81,
/* 0x52 */ NUM_INS: 82,
/* 0x53 */ NUM_DEL: 83,
/* 0x54 */ SYSREQ: 84, // 84-key keyboard only (simulated with 'alt'+'prtsc' on 101-key keyboards)
/* 0x54 */ PAUSE: 84, // 101-key keyboard only
/* 0x57 */ F11: 87,
/* 0x58 */ F12: 88,
/* 0x5B */ WIN: 91, // aka CMD
/* 0x5C */ RWIN: 92,
/* 0x5D */ MENU: 93, // aka CMD + ONRIGHT
/* 0x7F */ MAKE: 127,
/* 0x80 */ BREAK: 128,
/* 0xE0 */ EXTEND1: 224,
/* 0xE1 */ EXTEND2: 225
};
/**
* The set of values that a browser may store in the 'location' property of a keyboard event object
* which we also support.
*
* @enum {number}
*/
Keyboard.LOCATION = {
LEFT: 1,
RIGHT: 2,
NUMPAD: 3
};
/**
* These internal "shift key" states are used to indicate BOTH the physical shift-key states (in bitsState)
* and the simulated shift-key states (in bitsStateSim). The LOCK keys are problematic in both cases: the
* browsers give us no way to query the LOCK key states, so we can only infer them, and because they are "soft"
* locks, the machine's notion of their state is subject to change at any time as well. Granted, the IBM PC
* ROM BIOS will store its LOCK states in the ROM BIOS Data Area (@0040:0017), but that's just a BIOS convention.
*
* Also, because this is purely for internal use, don't make the mistake of thinking that these bits have any
* connection to the ROM BIOS bits @0040:0017 (they don't). We emulate hardware, not ROMs.
*
* TODO: Consider taking notice of the ROM BIOS Data Area state anyway, even though I'd rather remain ROM-agnostic;
* at the very least, it would help us keep our LOCK LEDs in sync with the machine's LOCK states. However, the LED
* issue will be largely moot (at least for MODEL_5170 machines) once we add support for PC AT keyboard LED commands.
*
* Note that right-hand state bits are equal to the left-hand bits shifted right 1 bit; makes sense, "right"? ;-)
*
* @enum {number}
*/
Keyboard.STATE = {
RSHIFT: 0x0001,
SHIFT: 0x0002,
RCTRL: 0x0004, // 101-key keyboard only
CTRL: 0x0008,
CTRLS: 0x000c,
RALT: 0x0010, // 101-key keyboard only
ALT: 0x0020,
ALTS: 0x0030,
RCMD: 0x0040, // 101-key keyboard only
CMD: 0x0080, // 101-key keyboard only
CMDS: 0x00c0,
ALL_RIGHT: 0x0055, // RSHIFT | RCTRL | RALT | RCMD
ALL_SHIFT: 0x00ff, // SHIFT | RSHIFT | CTRL | RCTRL | ALT | RALT | CMD | RCMD
INSERT: 0x0100, // TODO: Placeholder (we currently have no notion of any "insert" states)
CAPS_LOCK: 0x0200,
NUM_LOCK: 0x0400,
SCROLL_LOCK: 0x0800,
ALL_LOCKS: 0x0e00 // CAPS_LOCK | NUM_LOCK | SCROLL_LOCK
};
/**
* Maps KEYCODES of shift/modifier keys to their corresponding (default) STATES bit above.
*
* @enum {number}
*/
Keyboard.KEYSTATES = {};
Keyboard.KEYSTATES[Keyboard.SIMCODE.RSHIFT] = Keyboard.STATE.RSHIFT;
Keyboard.KEYSTATES[Keyboard.SIMCODE.SHIFT] = Keyboard.STATE.SHIFT;
Keyboard.KEYSTATES[Keyboard.SIMCODE.CTRL] = Keyboard.STATE.CTRL;
Keyboard.KEYSTATES[Keyboard.SIMCODE.ALT] = Keyboard.STATE.ALT;
Keyboard.KEYSTATES[Keyboard.SIMCODE.CMD] = Keyboard.STATE.CMD;
Keyboard.KEYSTATES[Keyboard.SIMCODE.RCMD] = Keyboard.STATE.RCMD;
Keyboard.KEYSTATES[Keyboard.SIMCODE.FF_CMD] = Keyboard.STATE.CMD;
Keyboard.KEYSTATES[Keyboard.SIMCODE.CAPS_LOCK] = Keyboard.STATE.CAPS_LOCK;
Keyboard.KEYSTATES[Keyboard.SIMCODE.NUM_LOCK] = Keyboard.STATE.NUM_LOCK;
Keyboard.KEYSTATES[Keyboard.SIMCODE.SCROLL_LOCK] = Keyboard.STATE.SCROLL_LOCK;
/**
* Maps CLICKCODE (string) to SIMCODE (number).
*
* @enum {number}
*/
Keyboard.CLICKCODES = {
'TAB': Keyboard.SIMCODE.TAB,
'ESC': Keyboard.SIMCODE.ESC,
'F1': Keyboard.SIMCODE.F1,
'F2': Keyboard.SIMCODE.F2,
'F3': Keyboard.SIMCODE.F3,
'F4': Keyboard.SIMCODE.F4,
'F5': Keyboard.SIMCODE.F5,
'F6': Keyboard.SIMCODE.F6,
'F7': Keyboard.SIMCODE.F7,
'F8': Keyboard.SIMCODE.F8,
'F9': Keyboard.SIMCODE.F9,
'F10': Keyboard.SIMCODE.F10,
'LEFT': Keyboard.SIMCODE.LEFT, // formerly "left-arrow"
'UP': Keyboard.SIMCODE.UP, // formerly "up-arrow"
'RIGHT': Keyboard.SIMCODE.RIGHT, // formerly "right-arrow"
'DOWN': Keyboard.SIMCODE.DOWN, // formerly "down-arrow"
'SYSREQ': Keyboard.SIMCODE.SYSREQ,
/*
* These bindings are for convenience (common key combinations that can be bound to a single control)
*/
'CTRL_C': Keyboard.SIMCODE.CTRL_C,
'CTRL_BREAK': Keyboard.SIMCODE.CTRL_BREAK,
'CTRL_ALT_DEL': Keyboard.SIMCODE.CTRL_ALT_DEL
};
/**
* Maps SOFTCODE (string) to KEYCODE or SIMCODE (number).
*
* We define identifiers for all possible keys, based on their primary (unshifted) character or function.
* This also serves as a definition of all supported keys, making it possible to create full-featured
* "soft keyboards".
*
* One exception to the (unshifted) rule above is 'prtsc': on the original IBM 83-key and 84-key keyboards,
* its primary (unshifted) character was '*', but on 101-key keyboards, it became a separate key ('prtsc',
* now labeled "Print Screen"), as did the num-pad '*' ('num-mul'), so 'prtsc' seems worthy of an exception
* to the rule.
*
* On 83-key and 84-key keyboards, 'ctrl'+'num-lock' triggered a "pause" operation and 'ctrl'+'scroll-lock'
* triggered a "break" operation.
*
* On 101-key keyboards, IBM decided to move both those special operations to a new 'pause' ("Pause/Break")
* key, near the new dedicated 'prtsc' ("Print Screen/SysRq") key -- and to drop the "e" from "SysReq".
* Those keys behave as follows:
*
* When 'pause' is pressed alone, it generates 0xe1 0x1d 0x45 0xe1 0x9d 0xc5 on make (nothing on break),
* which essentially simulates the make-and-break of the 'ctrl' and 'num-lock' keys (ignoring the 0xe1),
* triggering a "pause" operation.
*
* When 'pause' is pressed with 'ctrl', it generates 0xe0 0x46 0xe0 0xc6 on make (nothing on break) and
* does not repeat, which essentially simulates the make-and-break of 'scroll-lock', which, in conjunction
* with the separate make-and-break of 'ctrl', triggers a "break" operation.
*
* When 'prtsc' is pressed alone, it generates 0xe0 0x2a 0xe0 0x37, simulating the make of both 'shift'
* and 'prtsc'; when pressed with 'shift' or 'ctrl', it generates only 0xe0 0x37; and when pressed with
* 'alt', it generates only 0x54 (to simulate 'sysreq').
*
* TODO: Implement the above behaviors.
*
* All key identifiers must be quotable using single-quotes, because that's how components.xsl will encode them
* *inside* the "data-value" attribute of the corresponding HTML control. Which, in turn, is why the single-quote
* key is defined as 'quote' rather than "'". Similarly, if there was unshifted "double-quote" key, it could
* not be called '"', because components.xsl quotes the *entire* "data-value" attribute using double-quotes.
*
* In the (informal) numbering of keys below, two keys are deliberately numbered 84, reflecting the fact that
* the 'sysreq' key was added to the 84-key keyboard but then dropped from the 101-key keyboard as a stand-alone key.
*
* @enum {number}
*/
Keyboard.SOFTCODES = {
/* 1 */ 'esc': Keyboard.SIMCODE.ESC,
/* 2 */ '1': Keyboard.ASCII['1'],
/* 3 */ '2': Keyboard.ASCII['2'],
/* 4 */ '3': Keyboard.ASCII['3'],
/* 5 */ '4': Keyboard.ASCII['4'],
/* 6 */ '5': Keyboard.ASCII['5'],
/* 7 */ '6': Keyboard.ASCII['6'],
/* 8 */ '7': Keyboard.ASCII['7'],
/* 9 */ '8': Keyboard.ASCII['8'],
/* 10 */ '9': Keyboard.ASCII['9'],
/* 11 */ '0': Keyboard.ASCII['0'],
/* 12 */ '-': Keyboard.ASCII['-'],
/* 13 */ '=': Keyboard.ASCII['='],
/* 14 */ 'bs': Keyboard.SIMCODE.BS,
/* 15 */ 'tab': Keyboard.SIMCODE.TAB,
/* 16 */ 'q': Keyboard.ASCII.Q,
/* 17 */ 'w': Keyboard.ASCII.W,
/* 18 */ 'e': Keyboard.ASCII.E,
/* 19 */ 'r': Keyboard.ASCII.R,
/* 20 */ 't': Keyboard.ASCII.T,
/* 21 */ 'y': Keyboard.ASCII.Y,
/* 22 */ 'u': Keyboard.ASCII.U,
/* 23 */ 'i': Keyboard.ASCII.I,
/* 24 */ 'o': Keyboard.ASCII.O,
/* 25 */ 'p': Keyboard.ASCII.P,
/* 26 */ '[': Keyboard.ASCII['['],
/* 27 */ ']': Keyboard.ASCII[']'],
/* 28 */ 'enter': Keyboard.KEYCODE.CR,
/* 29 */ 'ctrl': Keyboard.SIMCODE.CTRL,
/* 30 */ 'a': Keyboard.ASCII.A,
/* 31 */ 's': Keyboard.ASCII.S,
/* 32 */ 'd': Keyboard.ASCII.D,
/* 33 */ 'f': Keyboard.ASCII.F,
/* 34 */ 'g': Keyboard.ASCII.G,
/* 35 */ 'h': Keyboard.ASCII.H,
/* 36 */ 'j': Keyboard.ASCII.J,
/* 37 */ 'k': Keyboard.ASCII.K,
/* 38 */ 'l': Keyboard.ASCII.L,
/* 39 */ ';': Keyboard.ASCII[';'],
/* 40 */ 'quote': Keyboard.ASCII["'"], // formerly "squote"
/* 41 */ '`': Keyboard.ASCII['`'], // formerly "bquote"
/* 42 */ 'shift': Keyboard.SIMCODE.SHIFT, // formerly "lshift"
/* 43 */ '\\': Keyboard.ASCII['\\'], // formerly "bslash"
/* 44 */ 'z': Keyboard.ASCII.Z,
/* 45 */ 'x': Keyboard.ASCII.X,
/* 46 */ 'c': Keyboard.ASCII.C,
/* 47 */ 'v': Keyboard.ASCII.V,
/* 48 */ 'b': Keyboard.ASCII.B,
/* 49 */ 'n': Keyboard.ASCII.N,
/* 50 */ 'm': Keyboard.ASCII.M,
/* 51 */ ',': Keyboard.ASCII[','],
/* 52 */ '.': Keyboard.ASCII['.'],
/* 53 */ '/': Keyboard.ASCII['/'],
/* 54 */ 'right-shift': Keyboard.SIMCODE.RSHIFT, // formerly "rshift"
/* 55 */ 'prtsc': Keyboard.SIMCODE.PRTSC, // unshifted '*'; becomes dedicated 'Print Screen' key on 101-key keyboards
/* 56 */ 'alt': Keyboard.SIMCODE.ALT,
/* 57 */ 'space': Keyboard.SIMCODE.SPACE,
/* 58 */ 'caps-lock': Keyboard.SIMCODE.CAPS_LOCK,
/* 59 */ 'f1': Keyboard.SIMCODE.F1,
/* 60 */ 'f2': Keyboard.SIMCODE.F2,
/* 61 */ 'f3': Keyboard.SIMCODE.F3,
/* 62 */ 'f4': Keyboard.SIMCODE.F4,
/* 63 */ 'f5': Keyboard.SIMCODE.F5,
/* 64 */ 'f6': Keyboard.SIMCODE.F6,
/* 65 */ 'f7': Keyboard.SIMCODE.F7,
/* 66 */ 'f8': Keyboard.SIMCODE.F8,
/* 67 */ 'f9': Keyboard.SIMCODE.F9,
/* 68 */ 'f10': Keyboard.SIMCODE.F10,
/* 69 */ 'num-lock': Keyboard.SIMCODE.NUM_LOCK,
/* 70 */ 'scroll-lock': Keyboard.SIMCODE.SCROLL_LOCK, // TODO: 0xe046 on 101-key keyboards?
/* 71 */ 'num-home': Keyboard.SIMCODE.HOME, // formerly "home"
/* 72 */ 'num-up': Keyboard.SIMCODE.UP, // formerly "up-arrow"
/* 73 */ 'num-pgup': Keyboard.SIMCODE.PGUP, // formerly "page-up"
/* 74 */ 'num-sub': Keyboard.SIMCODE.NUM_SUB, // formerly "num-minus"
/* 75 */ 'num-left': Keyboard.SIMCODE.LEFT, // formerly "left-arrow"
/* 76 */ 'num-center': Keyboard.SIMCODE.NUM_CENTER, // formerly "center"
/* 77 */ 'num-right': Keyboard.SIMCODE.RIGHT, // formerly "right-arrow"
/* 78 */ 'num-add': Keyboard.SIMCODE.NUM_ADD, // formerly "num-plus"
/* 79 */ 'num-end': Keyboard.SIMCODE.END, // formerly "end"
/* 80 */ 'num-down': Keyboard.SIMCODE.DOWN, // formerly "down-arrow"
/* 81 */ 'num-pgdn': Keyboard.SIMCODE.PGDN, // formerly "page-down"
/* 82 */ 'num-ins': Keyboard.SIMCODE.INS, // formerly "ins"
/* 83 */ 'num-del': Keyboard.SIMCODE.DEL, // formerly "del"
/* 84 */ 'sysreq': Keyboard.SCANCODE.SYSREQ // 84-key keyboard only (simulated with 'alt'+'prtsc' on 101-key keyboards)
// /* 84 */ 'pause': Keyboard.SCANCODE.PAUSE, // 101-key keyboard only
// /* 85 */ 'f11': Keyboard.SCANCODE.F11,
// /* 86 */ 'f12': Keyboard.SCANCODE.F12,
// /* 87 */ 'num-enter': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.ENTER << 8),
// /* 88 */ 'right-ctrl': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.CTRL << 8),
// /* 89 */ 'num-div': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.SLASH << 8),
// /* 90 */ 'num-mul': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.PRTSC << 8),
// /* 91 */ 'right-alt': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.ALT << 8),
// /* 92 */ 'home': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_HOME << 8),
// /* 93 */ 'up': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_UP << 8),
// /* 94 */ 'pgup': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_PGUP << 8),
// /* 95 */ 'left': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_LEFT << 8),
// /* 96 */ 'right': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_RIGHT << 8),
// /* 97 */ 'end': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_END << 8),
// /* 98 */ 'down': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_DOWN << 8),
// /* 99 */ 'pgdn': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_PGDN << 8),
// /*100 */ 'ins': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_INS << 8),
// /*101 */ 'del': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.NUM_DEL << 8),
// 'win': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.WIN << 8),
// 'right-win': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.RWIN << 8),
// 'menu': Keyboard.SCANCODE.EXTEND1 | (Keyboard.SCANCODE.MENU << 8)
};
/**
* Maps "soft-key" definitions (above) of shift/modifier keys to their corresponding (default) STATES bit.
*
* @enum {number}
*/
Keyboard.LEDSTATES = {
'caps-lock': Keyboard.STATE.CAPS_LOCK,
'num-lock': Keyboard.STATE.NUM_LOCK,
'scroll-lock': Keyboard.STATE.SCROLL_LOCK
};
/**
* Maps SIMCODE (number) to SCANCODE (number(s)).
*
* This array is used by keySimulate() to lookup a given SIMCODE and convert it to a SCANCODE
* (lower byte), plus any required shift key SCANCODES (upper bytes).
*
* Using keyCodes from keyPress events proved to be more robust than using keyCodes from keyDown and
* keyUp events, in part because of differences in the way browsers generate the keyDown and keyUp events.
* For example, Safari on iOS devices will not generate up/down events for shift keys, and for other keys,
* the up/down events are usually generated after the actual press is complete, and in rapid succession.
*
* The other problem (which is more of a problem with keyboards like the C1P than any IBM keyboards) is
* that the shift/modifier state for a character on the "source" keyboard may not match the shift/modifier
* state for the same character on the "target" keyboard. And since this code is inherited from C1Pjs,
* we've inherited the same solution: keySimulate() has the ability to "undo" any states in bitsState
* that conflict with the state(s) required for the character in question.
*
* @enum {number}
*/
Keyboard.SIMCODES = {};
Keyboard.SIMCODES[Keyboard.SIMCODE.ESC] = Keyboard.SCANCODE.ESC;
Keyboard.SIMCODES[Keyboard.ASCII['1']] = Keyboard.SCANCODE.ONE;
Keyboard.SIMCODES[Keyboard.ASCII['!']] = Keyboard.SCANCODE.ONE | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['2']] = Keyboard.SCANCODE.TWO;
Keyboard.SIMCODES[Keyboard.ASCII['@']] = Keyboard.SCANCODE.TWO | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['3']] = Keyboard.SCANCODE.THREE;
Keyboard.SIMCODES[Keyboard.ASCII['#']] = Keyboard.SCANCODE.THREE | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['4']] = Keyboard.SCANCODE.FOUR;
Keyboard.SIMCODES[Keyboard.ASCII['$']] = Keyboard.SCANCODE.FOUR | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['5']] = Keyboard.SCANCODE.FIVE;
Keyboard.SIMCODES[Keyboard.ASCII['%']] = Keyboard.SCANCODE.FIVE | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['6']] = Keyboard.SCANCODE.SIX;
Keyboard.SIMCODES[Keyboard.ASCII['^']] = Keyboard.SCANCODE.SIX | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['7']] = Keyboard.SCANCODE.SEVEN;
Keyboard.SIMCODES[Keyboard.ASCII['&']] = Keyboard.SCANCODE.SEVEN | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['8']] = Keyboard.SCANCODE.EIGHT;
Keyboard.SIMCODES[Keyboard.ASCII['*']] = Keyboard.SCANCODE.EIGHT | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['9']] = Keyboard.SCANCODE.NINE;
Keyboard.SIMCODES[Keyboard.ASCII['(']] = Keyboard.SCANCODE.NINE | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['0']] = Keyboard.SCANCODE.ZERO;
Keyboard.SIMCODES[Keyboard.ASCII[')']] = Keyboard.SCANCODE.ZERO | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['-']] = Keyboard.SCANCODE.DASH;
Keyboard.SIMCODES[Keyboard.ASCII['_']] = Keyboard.SCANCODE.DASH | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['=']] = Keyboard.SCANCODE.EQUALS;
Keyboard.SIMCODES[Keyboard.ASCII['+']] = Keyboard.SCANCODE.EQUALS | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.SIMCODE.BS] = Keyboard.SCANCODE.BS;
Keyboard.SIMCODES[Keyboard.SIMCODE.TAB] = Keyboard.SCANCODE.TAB;
Keyboard.SIMCODES[Keyboard.ASCII.q] = Keyboard.SCANCODE.Q;
Keyboard.SIMCODES[Keyboard.ASCII.Q] = Keyboard.SCANCODE.Q | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.w] = Keyboard.SCANCODE.W;
Keyboard.SIMCODES[Keyboard.ASCII.W] = Keyboard.SCANCODE.W | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.e] = Keyboard.SCANCODE.E;
Keyboard.SIMCODES[Keyboard.ASCII.E] = Keyboard.SCANCODE.E | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.r] = Keyboard.SCANCODE.R;
Keyboard.SIMCODES[Keyboard.ASCII.R] = Keyboard.SCANCODE.R | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.t] = Keyboard.SCANCODE.T;
Keyboard.SIMCODES[Keyboard.ASCII.T] = Keyboard.SCANCODE.T | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.y] = Keyboard.SCANCODE.Y;
Keyboard.SIMCODES[Keyboard.ASCII.Y] = Keyboard.SCANCODE.Y | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.u] = Keyboard.SCANCODE.U;
Keyboard.SIMCODES[Keyboard.ASCII.U] = Keyboard.SCANCODE.U | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.i] = Keyboard.SCANCODE.I;
Keyboard.SIMCODES[Keyboard.ASCII.I] = Keyboard.SCANCODE.I | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.o] = Keyboard.SCANCODE.O;
Keyboard.SIMCODES[Keyboard.ASCII.O] = Keyboard.SCANCODE.O | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.p] = Keyboard.SCANCODE.P;
Keyboard.SIMCODES[Keyboard.ASCII.P] = Keyboard.SCANCODE.P | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['[']] = Keyboard.SCANCODE.LBRACK;
Keyboard.SIMCODES[Keyboard.ASCII['{']] = Keyboard.SCANCODE.LBRACK | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII[']']] = Keyboard.SCANCODE.RBRACK;
Keyboard.SIMCODES[Keyboard.ASCII['}']] = Keyboard.SCANCODE.RBRACK | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.KEYCODE.CR] = Keyboard.SCANCODE.ENTER;
Keyboard.SIMCODES[Keyboard.SIMCODE.CTRL] = Keyboard.SCANCODE.CTRL;
Keyboard.SIMCODES[Keyboard.ASCII.a] = Keyboard.SCANCODE.A;
Keyboard.SIMCODES[Keyboard.ASCII.A] = Keyboard.SCANCODE.A | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.s] = Keyboard.SCANCODE.S;
Keyboard.SIMCODES[Keyboard.ASCII.S] = Keyboard.SCANCODE.S | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.d] = Keyboard.SCANCODE.D;
Keyboard.SIMCODES[Keyboard.ASCII.D] = Keyboard.SCANCODE.D | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.f] = Keyboard.SCANCODE.F;
Keyboard.SIMCODES[Keyboard.ASCII.F] = Keyboard.SCANCODE.F | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.g] = Keyboard.SCANCODE.G;
Keyboard.SIMCODES[Keyboard.ASCII.G] = Keyboard.SCANCODE.G | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.h] = Keyboard.SCANCODE.H;
Keyboard.SIMCODES[Keyboard.ASCII.H] = Keyboard.SCANCODE.H | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.j] = Keyboard.SCANCODE.J;
Keyboard.SIMCODES[Keyboard.ASCII.J] = Keyboard.SCANCODE.J | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.k] = Keyboard.SCANCODE.K;
Keyboard.SIMCODES[Keyboard.ASCII.K] = Keyboard.SCANCODE.K | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.l] = Keyboard.SCANCODE.L;
Keyboard.SIMCODES[Keyboard.ASCII.L] = Keyboard.SCANCODE.L | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII[';']] = Keyboard.SCANCODE.SEMI;
Keyboard.SIMCODES[Keyboard.ASCII[':']] = Keyboard.SCANCODE.SEMI | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII["'"]] = Keyboard.SCANCODE.QUOTE;
Keyboard.SIMCODES[Keyboard.ASCII['"']] = Keyboard.SCANCODE.QUOTE | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['`']] = Keyboard.SCANCODE.BQUOTE;
Keyboard.SIMCODES[Keyboard.ASCII['~']] = Keyboard.SCANCODE.BQUOTE | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.SIMCODE.SHIFT] = Keyboard.SCANCODE.SHIFT;
Keyboard.SIMCODES[Keyboard.ASCII['\\']] = Keyboard.SCANCODE.BSLASH;
Keyboard.SIMCODES[Keyboard.ASCII['|']] = Keyboard.SCANCODE.BSLASH | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.z] = Keyboard.SCANCODE.Z;
Keyboard.SIMCODES[Keyboard.ASCII.Z] = Keyboard.SCANCODE.Z | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.x] = Keyboard.SCANCODE.X;
Keyboard.SIMCODES[Keyboard.ASCII.X] = Keyboard.SCANCODE.X | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.c] = Keyboard.SCANCODE.C;
Keyboard.SIMCODES[Keyboard.ASCII.C] = Keyboard.SCANCODE.C | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.v] = Keyboard.SCANCODE.V;
Keyboard.SIMCODES[Keyboard.ASCII.V] = Keyboard.SCANCODE.V | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.b] = Keyboard.SCANCODE.B;
Keyboard.SIMCODES[Keyboard.ASCII.B] = Keyboard.SCANCODE.B | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.n] = Keyboard.SCANCODE.N;
Keyboard.SIMCODES[Keyboard.ASCII.N] = Keyboard.SCANCODE.N | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII.m] = Keyboard.SCANCODE.M;
Keyboard.SIMCODES[Keyboard.ASCII.M] = Keyboard.SCANCODE.M | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII[',']] = Keyboard.SCANCODE.COMMA;
Keyboard.SIMCODES[Keyboard.ASCII['<']] = Keyboard.SCANCODE.COMMA | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['.']] = Keyboard.SCANCODE.PERIOD;
Keyboard.SIMCODES[Keyboard.ASCII['>']] = Keyboard.SCANCODE.PERIOD | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.ASCII['/']] = Keyboard.SCANCODE.SLASH;
Keyboard.SIMCODES[Keyboard.ASCII['?']] = Keyboard.SCANCODE.SLASH | (Keyboard.SCANCODE.SHIFT << 8);
Keyboard.SIMCODES[Keyboard.SIMCODE.RSHIFT] = Keyboard.SCANCODE.RSHIFT;
Keyboard.SIMCODES[Keyboard.SIMCODE.PRTSC] = Keyboard.SCANCODE.PRTSC;
Keyboard.SIMCODES[Keyboard.SIMCODE.ALT] = Keyboard.SCANCODE.ALT;
Keyboard.SIMCODES[Keyboard.SIMCODE.SPACE] = Keyboard.SCANCODE.SPACE;
Keyboard.SIMCODES[Keyboard.SIMCODE.CAPS_LOCK] = Keyboard.SCANCODE.CAPS_LOCK;
Keyboard.SIMCODES[Keyboard.SIMCODE.F1] = Keyboard.SCANCODE.F1;
Keyboard.SIMCODES[Keyboard.SIMCODE.F2] = Keyboard.SCANCODE.F2;
Keyboard.SIMCODES[Keyboard.SIMCODE.F3] = Keyboard.SCANCODE.F3;
Keyboard.SIMCODES[Keyboard.SIMCODE.F4] = Keyboard.SCANCODE.F4;
Keyboard.SIMCODES[Keyboard.SIMCODE.F5] = Keyboard.SCANCODE.F5;
Keyboard.SIMCODES[Keyboard.SIMCODE.F6] = Keyboard.SCANCODE.F6;
Keyboard.SIMCODES[Keyboard.SIMCODE.F7] = Keyboard.SCANCODE.F7;
Keyboard.SIMCODES[Keyboard.SIMCODE.F8] = Keyboard.SCANCODE.F8;
Keyboard.SIMCODES[Keyboard.SIMCODE.F9] = Keyboard.SCANCODE.F9;
Keyboard.SIMCODES[Keyboard.SIMCODE.F10] = Keyboard.SCANCODE.F10;
Keyboard.SIMCODES[Keyboard.SIMCODE.NUM_LOCK] = Keyboard.SCANCODE.NUM_LOCK;
Keyboard.SIMCODES[Keyboard.SIMCODE.SCROLL_LOCK] = Keyboard.SCANCODE.SCROLL_LOCK;
Keyboard.SIMCODES[Keyboard.SIMCODE.HOME] = Keyboard.SCANCODE.NUM_HOME;
Keyboard.SIMCODES[Keyboard.SIMCODE.UP] = Keyboard.SCANCODE.NUM_UP;
Keyboard.SIMCODES[Keyboard.SIMCODE.PGUP] = Keyboard.SCANCODE.NUM_PGUP;
Keyboard.SIMCODES[Keyboard.SIMCODE.NUM_SUB] = Keyboard.SCANCODE.NUM_SUB;
Keyboard.SIMCODES[Keyboard.SIMCODE.LEFT] = Keyboard.SCANCODE.NUM_LEFT;
Keyboard.SIMCODES[Keyboard.SIMCODE.NUM_CENTER] = Keyboard.SCANCODE.NUM_CENTER;
Keyboard.SIMCODES[Keyboard.SIMCODE.RIGHT] = Keyboard.SCANCODE.NUM_RIGHT;
Keyboard.SIMCODES[Keyboard.SIMCODE.NUM_ADD] = Keyboard.SCANCODE.NUM_ADD;
Keyboard.SIMCODES[Keyboard.SIMCODE.END] = Keyboard.SCANCODE.NUM_END;
Keyboard.SIMCODES[Keyboard.SIMCODE.DOWN] = Keyboard.SCANCODE.NUM_DOWN;
Keyboard.SIMCODES[Keyboard.SIMCODE.PGDN] = Keyboard.SCANCODE.NUM_PGDN;
Keyboard.SIMCODES[Keyboard.SIMCODE.INS] = Keyboard.SCANCODE.NUM_INS;
Keyboard.SIMCODES[Keyboard.SIMCODE.DEL] = Keyboard.SCANCODE.NUM_DEL;
Keyboard.SIMCODES[Keyboard.SIMCODE.SYSREQ] = Keyboard.SCANCODE.SYSREQ;
/*
* Entries beyond this point are for keys that existed only on 101-key keyboards (well, except for 'sysreq',
* which also existed on the 84-key keyboard), which ALSO means that these keys essentially did not exist
* for a MODEL_5150 or MODEL_5160 machine, because those machines could use only 83-key keyboards. Remember
* that IBM machines and IBM keyboards are our reference point here, so while there were undoubtedly 5150/5160
* clones that could use newer keyboards, as well as 3rd-party keyboards that could work with older machines,
* support for non-IBM configurations is left for another day.
*
* TODO: The only relevance of newer keyboards to older machines is the fact that you're probably using a newer
* keyboard with your browser, which raises the question of what to do with newer keys that older machines
* wouldn't understand. I don't attempt to filter out any of the entries below based on machine model, but that
* would seem like a wise thing to do.
*
* TODO: Add entries for 'num-mul', 'num-div', 'num-enter', the stand-alone arrow keys, etc, AND at the same time,
* make sure that keys with multi-byte sequences (eg, 0xe0 0x1c) work properly.
*/
Keyboard.SIMCODES[Keyboard.SIMCODE.F11] = Keyboard.SCANCODE.F11;
Keyboard.SIMCODES[Keyboard.SIMCODE.F12] = Keyboard.SCANCODE.F12;
Keyboard.SIMCODES[Keyboard.SIMCODE.CMD] = Keyboard.SCANCODE.WIN;
Keyboard.SIMCODES[Keyboard.SIMCODE.RCMD] = Keyboard.SCANCODE.MENU;
Keyboard.SIMCODES[Keyboard.SIMCODE.FF_CMD] = Keyboard.SCANCODE.WIN;
Keyboard.SIMCODES[Keyboard.SIMCODE.CTRL_C] = Keyboard.SCANCODE.C | (Keyboard.SCANCODE.CTRL << 8);
Keyboard.SIMCODES[Keyboard.SIMCODE.CTRL_BREAK] = Keyboard.SCANCODE.SCROLL_LOCK | (Keyboard.SCANCODE.CTRL << 8);
Keyboard.SIMCODES[Keyboard.SIMCODE.CTRL_ALT_DEL] = Keyboard.SCANCODE.NUM_DEL | (Keyboard.SCANCODE.CTRL << 8) | (Keyboard.SCANCODE.ALT << 16);
/**
* Commands that can be sent to the Keyboard via the 8042; see sendCmd()
*
* @enum {number}
*/
Keyboard.CMD = {
RESET: 0xFF,
RESEND: 0xFE,
DEF_ON: 0xF6,
DEF_OFF: 0xF5,
ENABLE: 0xF4,
SET_RATE: 0xF3,
ECHO: 0xEE,
SET_LEDS: 0xED
};
/**
* Command responses returned to the Keyboard via the 8042; see sendCmd()
*
* @enum {number}
*/
Keyboard.CMDRES = {
OVERRUN: 0x00,
LOAD_TEST: 0x65, // undocumented "LOAD MANUFACTURING TEST REQUEST" response code
BAT_OK: 0xAA, // Basic Assurance Test (BAT) succeeded
ECHO: 0xEE,
BREAK_PREF: 0xF0, // break prefix
ACK: 0xFA,
BAT_FAIL: 0xFC, // Basic Assurance Test (BAT) failed
DIAG_FAIL: 0xFD,
RESEND: 0xFE,
BUFF_FULL: 0xFF // TODO: Verify this response code (is it just for older 83-key keyboards?)
};
Keyboard.LIMIT = {
MAX_SCANCODES: 20 // TODO: Verify this limit for newer keyboards (84-key and up)
};
/**
* setBinding(sHTMLType, sBinding, control, sValue)
*
* @this {Keyboard}
* @param {string|null} sHTMLType is the type of the HTML control (eg, "button", "list", "text", "submit", "textarea", "canvas")
* @param {string} sBinding is the value of the 'binding' parameter stored in the HTML control's "data-value" attribute (eg, "esc")
* @param {Object} control is the HTML control DOM object (eg, HTMLButtonElement)
* @param {string} [sValue] optional data value
* @return {boolean} true if binding was successful, false if unrecognized binding request
*/
Keyboard.prototype.setBinding = function(sHTMLType, sBinding, control, sValue)
{
/*
* There's a special binding that the Video component uses ("kbd") to effectively bind its
* screen to the entire keyboard, in Video.powerUp(); ie:
*
* video.kbd.setBinding("canvas", "kbd", video.canvasScreen);
* or:
* video.kbd.setBinding("textarea", "kbd", video.textareaScreen);
*
* However, it's also possible for the keyboard XML definition to define a control that serves
* a similar purpose; eg:
*
* <control type="text" binding="kbd" width="2em">Kbd</control>
*
* The latter is purely experimental, while we work on finding ways to trigger the soft keyboard on
* certain pesky devices (like the Kindle Fire). Note that even if you use the latter, the former will
* still be enabled (there's currently no way to configure the Video component to not bind its screen,
* but we could certainly add one if the need ever arose).