1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.event;
16
17 import static org.htmlunit.BrowserVersionFeatures.JS_EVENT_KEYBOARD_CTOR_WHICH;
18 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
19 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.htmlunit.corejs.javascript.ScriptableObject;
25 import org.htmlunit.html.DomNode;
26 import org.htmlunit.javascript.JavaScriptEngine;
27 import org.htmlunit.javascript.configuration.JsxClass;
28 import org.htmlunit.javascript.configuration.JsxConstant;
29 import org.htmlunit.javascript.configuration.JsxConstructor;
30 import org.htmlunit.javascript.configuration.JsxFunction;
31 import org.htmlunit.javascript.configuration.JsxGetter;
32
33
34
35
36
37
38
39
40
41
42
43
44 @JsxClass
45 public class KeyboardEvent extends UIEvent {
46
47
48 @JsxConstant
49 public static final int DOM_KEY_LOCATION_STANDARD = 0;
50
51
52 @JsxConstant
53 public static final int DOM_KEY_LOCATION_LEFT = 1;
54
55
56 @JsxConstant
57 public static final int DOM_KEY_LOCATION_RIGHT = 2;
58
59
60 @JsxConstant
61 public static final int DOM_KEY_LOCATION_NUMPAD = 3;
62
63
64 @JsxConstant({FF, FF_ESR})
65 public static final int DOM_VK_CANCEL = 3;
66
67
68 @JsxConstant({FF, FF_ESR})
69 public static final int DOM_VK_HELP = 6;
70
71
72 @JsxConstant({FF, FF_ESR})
73 public static final int DOM_VK_TAB = 9;
74
75
76 @JsxConstant({FF, FF_ESR})
77 public static final int DOM_VK_CLEAR = 12;
78
79
80 @JsxConstant({FF, FF_ESR})
81 public static final int DOM_VK_RETURN = 13;
82
83
84 @JsxConstant({FF, FF_ESR})
85 public static final int DOM_VK_SHIFT = 16;
86
87
88 @JsxConstant({FF, FF_ESR})
89 public static final int DOM_VK_CONTROL = 17;
90
91
92 @JsxConstant({FF, FF_ESR})
93 public static final int DOM_VK_ALT = 18;
94
95
96 @JsxConstant({FF, FF_ESR})
97 public static final int DOM_VK_PAUSE = 19;
98
99
100 @JsxConstant({FF, FF_ESR})
101 public static final int DOM_VK_CAPS_LOCK = 20;
102
103
104 @JsxConstant({FF, FF_ESR})
105 public static final int DOM_VK_HANGUL = 21;
106
107
108 @JsxConstant({FF, FF_ESR})
109 public static final int DOM_VK_KANA = 21;
110
111
112 @JsxConstant({FF, FF_ESR})
113 public static final int DOM_VK_EISU = 22;
114
115
116 @JsxConstant({FF, FF_ESR})
117 public static final int DOM_VK_FINAL = 24;
118
119
120 @JsxConstant({FF, FF_ESR})
121 public static final int DOM_VK_JUNJA = 23;
122
123
124 @JsxConstant({FF, FF_ESR})
125 public static final int DOM_VK_HANJA = 25;
126
127
128 @JsxConstant({FF, FF_ESR})
129 public static final int DOM_VK_KANJI = 25;
130
131
132 @JsxConstant({FF, FF_ESR})
133 public static final int DOM_VK_ESCAPE = 27;
134
135
136 @JsxConstant({FF, FF_ESR})
137 public static final int DOM_VK_CONVERT = 28;
138
139
140 @JsxConstant({FF, FF_ESR})
141 public static final int DOM_VK_NONCONVERT = 29;
142
143
144 @JsxConstant({FF, FF_ESR})
145 public static final int DOM_VK_ACCEPT = 30;
146
147
148 @JsxConstant({FF, FF_ESR})
149 public static final int DOM_VK_MODECHANGE = 31;
150
151
152 @JsxConstant({FF, FF_ESR})
153 public static final int DOM_VK_SPACE = 32;
154
155
156 @JsxConstant({FF, FF_ESR})
157 public static final int DOM_VK_PAGE_UP = 33;
158
159
160 @JsxConstant({FF, FF_ESR})
161 public static final int DOM_VK_PAGE_DOWN = 34;
162
163
164 @JsxConstant({FF, FF_ESR})
165 public static final int DOM_VK_END = 35;
166
167
168 @JsxConstant({FF, FF_ESR})
169 public static final int DOM_VK_HOME = 36;
170
171
172 @JsxConstant({FF, FF_ESR})
173 public static final int DOM_VK_LEFT = 37;
174
175
176 @JsxConstant({FF, FF_ESR})
177 public static final int DOM_VK_UP = 38;
178
179
180 @JsxConstant({FF, FF_ESR})
181 public static final int DOM_VK_RIGHT = 39;
182
183
184 @JsxConstant({FF, FF_ESR})
185 public static final int DOM_VK_SELECT = 41;
186
187
188 @JsxConstant({FF, FF_ESR})
189 public static final int DOM_VK_DOWN = 40;
190
191
192 @JsxConstant({FF, FF_ESR})
193 public static final int DOM_VK_PRINT = 42;
194
195
196 @JsxConstant({FF, FF_ESR})
197 public static final int DOM_VK_EXECUTE = 43;
198
199
200 @JsxConstant({FF, FF_ESR})
201 public static final int DOM_VK_PRINTSCREEN = 44;
202
203
204 @JsxConstant({FF, FF_ESR})
205 public static final int DOM_VK_INSERT = 45;
206
207
208 @JsxConstant({FF, FF_ESR})
209 public static final int DOM_VK_DELETE = 46;
210
211
212 @JsxConstant({FF, FF_ESR})
213 public static final int DOM_VK_0 = 48;
214
215
216 @JsxConstant({FF, FF_ESR})
217 public static final int DOM_VK_1 = 49;
218
219
220 @JsxConstant({FF, FF_ESR})
221 public static final int DOM_VK_2 = 50;
222
223
224 @JsxConstant({FF, FF_ESR})
225 public static final int DOM_VK_3 = 51;
226
227
228 @JsxConstant({FF, FF_ESR})
229 public static final int DOM_VK_4 = 52;
230
231
232 @JsxConstant({FF, FF_ESR})
233 public static final int DOM_VK_5 = 53;
234
235
236 @JsxConstant({FF, FF_ESR})
237 public static final int DOM_VK_6 = 54;
238
239
240 @JsxConstant({FF, FF_ESR})
241 public static final int DOM_VK_7 = 55;
242
243
244 @JsxConstant({FF, FF_ESR})
245 public static final int DOM_VK_8 = 56;
246
247
248 @JsxConstant({FF, FF_ESR})
249 public static final int DOM_VK_9 = 57;
250
251
252 @JsxConstant({FF, FF_ESR})
253 public static final int DOM_VK_COLON = 58;
254
255
256 @JsxConstant({FF, FF_ESR})
257 public static final int DOM_VK_SEMICOLON = 59;
258
259
260 @JsxConstant({FF, FF_ESR})
261 public static final int DOM_VK_LESS_THAN = 60;
262
263
264 @JsxConstant({FF, FF_ESR})
265 public static final int DOM_VK_EQUALS = 61;
266
267
268 @JsxConstant({FF, FF_ESR})
269 public static final int DOM_VK_GREATER_THAN = 62;
270
271
272 @JsxConstant({FF, FF_ESR})
273 public static final int DOM_VK_QUESTION_MARK = 63;
274
275
276 @JsxConstant({FF, FF_ESR})
277 public static final int DOM_VK_AT = 64;
278
279
280 @JsxConstant({FF, FF_ESR})
281 public static final int DOM_VK_A = 65;
282
283
284 @JsxConstant({FF, FF_ESR})
285 public static final int DOM_VK_B = 66;
286
287
288 @JsxConstant({FF, FF_ESR})
289 public static final int DOM_VK_C = 67;
290
291
292 @JsxConstant({FF, FF_ESR})
293 public static final int DOM_VK_D = 68;
294
295
296 @JsxConstant({FF, FF_ESR})
297 public static final int DOM_VK_E = 69;
298
299
300 @JsxConstant({FF, FF_ESR})
301 public static final int DOM_VK_F = 70;
302
303
304 @JsxConstant({FF, FF_ESR})
305 public static final int DOM_VK_G = 71;
306
307
308 @JsxConstant({FF, FF_ESR})
309 public static final int DOM_VK_H = 72;
310
311
312 @JsxConstant({FF, FF_ESR})
313 public static final int DOM_VK_I = 73;
314
315
316 @JsxConstant({FF, FF_ESR})
317 public static final int DOM_VK_J = 74;
318
319
320 @JsxConstant({FF, FF_ESR})
321 public static final int DOM_VK_K = 75;
322
323
324 @JsxConstant({FF, FF_ESR})
325 public static final int DOM_VK_L = 76;
326
327
328 @JsxConstant({FF, FF_ESR})
329 public static final int DOM_VK_M = 77;
330
331
332 @JsxConstant({FF, FF_ESR})
333 public static final int DOM_VK_N = 78;
334
335
336 @JsxConstant({FF, FF_ESR})
337 public static final int DOM_VK_O = 79;
338
339
340 @JsxConstant({FF, FF_ESR})
341 public static final int DOM_VK_BACK_SPACE = 8;
342
343
344 @JsxConstant({FF, FF_ESR})
345 public static final int DOM_VK_P = 80;
346
347
348 @JsxConstant({FF, FF_ESR})
349 public static final int DOM_VK_Q = 81;
350
351
352 @JsxConstant({FF, FF_ESR})
353 public static final int DOM_VK_R = 82;
354
355
356 @JsxConstant({FF, FF_ESR})
357 public static final int DOM_VK_S = 83;
358
359
360 @JsxConstant({FF, FF_ESR})
361 public static final int DOM_VK_T = 84;
362
363
364 @JsxConstant({FF, FF_ESR})
365 public static final int DOM_VK_U = 85;
366
367
368 @JsxConstant({FF, FF_ESR})
369 public static final int DOM_VK_V = 86;
370
371
372 @JsxConstant({FF, FF_ESR})
373 public static final int DOM_VK_W = 87;
374
375
376 @JsxConstant({FF, FF_ESR})
377 public static final int DOM_VK_X = 88;
378
379
380 @JsxConstant({FF, FF_ESR})
381 public static final int DOM_VK_Y = 89;
382
383
384 @JsxConstant({FF, FF_ESR})
385 public static final int DOM_VK_Z = 90;
386
387
388 @JsxConstant({FF, FF_ESR})
389 public static final int DOM_VK_WIN = 91;
390
391
392 @JsxConstant({FF, FF_ESR})
393 public static final int DOM_VK_CONTEXT_MENU = 93;
394
395
396 @JsxConstant({FF, FF_ESR})
397 public static final int DOM_VK_SLEEP = 95;
398
399
400 @JsxConstant({FF, FF_ESR})
401 public static final int DOM_VK_NUMPAD0 = 96;
402
403
404 @JsxConstant({FF, FF_ESR})
405 public static final int DOM_VK_NUMPAD1 = 97;
406
407
408 @JsxConstant({FF, FF_ESR})
409 public static final int DOM_VK_NUMPAD2 = 98;
410
411
412 @JsxConstant({FF, FF_ESR})
413 public static final int DOM_VK_NUMPAD3 = 99;
414
415
416 @JsxConstant({FF, FF_ESR})
417 public static final int DOM_VK_NUMPAD4 = 100;
418
419
420 @JsxConstant({FF, FF_ESR})
421 public static final int DOM_VK_NUMPAD5 = 101;
422
423
424 @JsxConstant({FF, FF_ESR})
425 public static final int DOM_VK_NUMPAD6 = 102;
426
427
428 @JsxConstant({FF, FF_ESR})
429 public static final int DOM_VK_NUMPAD7 = 103;
430
431
432 @JsxConstant({FF, FF_ESR})
433 public static final int DOM_VK_NUMPAD8 = 104;
434
435
436 @JsxConstant({FF, FF_ESR})
437 public static final int DOM_VK_NUMPAD9 = 105;
438
439
440 @JsxConstant({FF, FF_ESR})
441 public static final int DOM_VK_MULTIPLY = 106;
442
443
444 @JsxConstant({FF, FF_ESR})
445 public static final int DOM_VK_ADD = 107;
446
447
448 @JsxConstant({FF, FF_ESR})
449 public static final int DOM_VK_SEPARATOR = 108;
450
451
452 @JsxConstant({FF, FF_ESR})
453 public static final int DOM_VK_SUBTRACT = 109;
454
455
456 @JsxConstant({FF, FF_ESR})
457 public static final int DOM_VK_DECIMAL = 110;
458
459
460 @JsxConstant({FF, FF_ESR})
461 public static final int DOM_VK_DIVIDE = 111;
462
463
464 @JsxConstant({FF, FF_ESR})
465 public static final int DOM_VK_F1 = 112;
466
467
468 @JsxConstant({FF, FF_ESR})
469 public static final int DOM_VK_F2 = 113;
470
471
472 @JsxConstant({FF, FF_ESR})
473 public static final int DOM_VK_F3 = 114;
474
475
476 @JsxConstant({FF, FF_ESR})
477 public static final int DOM_VK_F4 = 115;
478
479
480 @JsxConstant({FF, FF_ESR})
481 public static final int DOM_VK_F5 = 116;
482
483
484 @JsxConstant({FF, FF_ESR})
485 public static final int DOM_VK_F6 = 117;
486
487
488 @JsxConstant({FF, FF_ESR})
489 public static final int DOM_VK_F7 = 118;
490
491
492 @JsxConstant({FF, FF_ESR})
493 public static final int DOM_VK_F8 = 119;
494
495
496 @JsxConstant({FF, FF_ESR})
497 public static final int DOM_VK_F9 = 120;
498
499
500 @JsxConstant({FF, FF_ESR})
501 public static final int DOM_VK_F10 = 121;
502
503
504 @JsxConstant({FF, FF_ESR})
505 public static final int DOM_VK_F11 = 122;
506
507
508 @JsxConstant({FF, FF_ESR})
509 public static final int DOM_VK_F12 = 123;
510
511
512 @JsxConstant({FF, FF_ESR})
513 public static final int DOM_VK_F13 = 124;
514
515
516 @JsxConstant({FF, FF_ESR})
517 public static final int DOM_VK_F14 = 125;
518
519
520 @JsxConstant({FF, FF_ESR})
521 public static final int DOM_VK_F15 = 126;
522
523
524 @JsxConstant({FF, FF_ESR})
525 public static final int DOM_VK_F16 = 127;
526
527
528 @JsxConstant({FF, FF_ESR})
529 public static final int DOM_VK_F17 = 128;
530
531
532 @JsxConstant({FF, FF_ESR})
533 public static final int DOM_VK_F18 = 129;
534
535
536 @JsxConstant({FF, FF_ESR})
537 public static final int DOM_VK_F19 = 130;
538
539
540 @JsxConstant({FF, FF_ESR})
541 public static final int DOM_VK_F20 = 131;
542
543
544 @JsxConstant({FF, FF_ESR})
545 public static final int DOM_VK_F21 = 132;
546
547
548 @JsxConstant({FF, FF_ESR})
549 public static final int DOM_VK_F22 = 133;
550
551
552 @JsxConstant({FF, FF_ESR})
553 public static final int DOM_VK_F23 = 134;
554
555
556 @JsxConstant({FF, FF_ESR})
557 public static final int DOM_VK_F24 = 135;
558
559
560 @JsxConstant({FF, FF_ESR})
561 public static final int DOM_VK_NUM_LOCK = 144;
562
563
564 @JsxConstant({FF, FF_ESR})
565 public static final int DOM_VK_SCROLL_LOCK = 145;
566
567
568 @JsxConstant({FF, FF_ESR})
569 public static final int DOM_VK_WIN_OEM_FJ_JISHO = 146;
570
571
572 @JsxConstant({FF, FF_ESR})
573 public static final int DOM_VK_WIN_OEM_FJ_MASSHOU = 147;
574
575
576 @JsxConstant({FF, FF_ESR})
577 public static final int DOM_VK_WIN_OEM_FJ_TOUROKU = 148;
578
579
580 @JsxConstant({FF, FF_ESR})
581 public static final int DOM_VK_WIN_OEM_FJ_LOYA = 149;
582
583
584 @JsxConstant({FF, FF_ESR})
585 public static final int DOM_VK_WIN_OEM_FJ_ROYA = 150;
586
587
588 @JsxConstant({FF, FF_ESR})
589 public static final int DOM_VK_CIRCUMFLEX = 160;
590
591
592 @JsxConstant({FF, FF_ESR})
593 public static final int DOM_VK_EXCLAMATION = 161;
594
595
596 @JsxConstant({FF, FF_ESR})
597 public static final int DOM_VK_DOUBLE_QUOTE = 162;
598
599
600 @JsxConstant({FF, FF_ESR})
601 public static final int DOM_VK_HASH = 163;
602
603
604 @JsxConstant({FF, FF_ESR})
605 public static final int DOM_VK_DOLLAR = 164;
606
607
608 @JsxConstant({FF, FF_ESR})
609 public static final int DOM_VK_PERCENT = 165;
610
611
612 @JsxConstant({FF, FF_ESR})
613 public static final int DOM_VK_AMPERSAND = 166;
614
615
616 @JsxConstant({FF, FF_ESR})
617 public static final int DOM_VK_UNDERSCORE = 167;
618
619
620 @JsxConstant({FF, FF_ESR})
621 public static final int DOM_VK_OPEN_PAREN = 168;
622
623
624 @JsxConstant({FF, FF_ESR})
625 public static final int DOM_VK_CLOSE_PAREN = 169;
626
627
628 @JsxConstant({FF, FF_ESR})
629 public static final int DOM_VK_ASTERISK = 170;
630
631
632 @JsxConstant({FF, FF_ESR})
633 public static final int DOM_VK_PLUS = 171;
634
635
636 @JsxConstant({FF, FF_ESR})
637 public static final int DOM_VK_PIPE = 172;
638
639
640 @JsxConstant({FF, FF_ESR})
641 public static final int DOM_VK_HYPHEN_MINUS = 173;
642
643
644 @JsxConstant({FF, FF_ESR})
645 public static final int DOM_VK_OPEN_CURLY_BRACKET = 174;
646
647
648 @JsxConstant({FF, FF_ESR})
649 public static final int DOM_VK_CLOSE_CURLY_BRACKET = 175;
650
651
652 @JsxConstant({FF, FF_ESR})
653 public static final int DOM_VK_TILDE = 176;
654
655
656 @JsxConstant({FF, FF_ESR})
657 public static final int DOM_VK_VOLUME_MUTE = 181;
658
659
660 @JsxConstant({FF, FF_ESR})
661 public static final int DOM_VK_VOLUME_DOWN = 182;
662
663
664 @JsxConstant({FF, FF_ESR})
665 public static final int DOM_VK_VOLUME_UP = 183;
666
667
668 @JsxConstant({FF, FF_ESR})
669 public static final int DOM_VK_COMMA = 188;
670
671
672 @JsxConstant({FF, FF_ESR})
673 public static final int DOM_VK_PERIOD = 190;
674
675
676 @JsxConstant({FF, FF_ESR})
677 public static final int DOM_VK_SLASH = 191;
678
679
680 @JsxConstant({FF, FF_ESR})
681 public static final int DOM_VK_BACK_QUOTE = 192;
682
683
684 @JsxConstant({FF, FF_ESR})
685 public static final int DOM_VK_OPEN_BRACKET = 219;
686
687
688 @JsxConstant({FF, FF_ESR})
689 public static final int DOM_VK_BACK_SLASH = 220;
690
691
692 @JsxConstant({FF, FF_ESR})
693 public static final int DOM_VK_CLOSE_BRACKET = 221;
694
695
696 @JsxConstant({FF, FF_ESR})
697 public static final int DOM_VK_QUOTE = 222;
698
699
700 @JsxConstant({FF, FF_ESR})
701 public static final int DOM_VK_META = 224;
702
703
704 @JsxConstant({FF, FF_ESR})
705 public static final int DOM_VK_ALTGR = 225;
706
707
708 @JsxConstant({FF, FF_ESR})
709 public static final int DOM_VK_WIN_ICO_HELP = 227;
710
711
712 @JsxConstant({FF, FF_ESR})
713 public static final int DOM_VK_WIN_ICO_00 = 228;
714
715
716 @JsxConstant({FF, FF_ESR})
717 public static final int DOM_VK_PROCESSKEY = 229;
718
719
720 @JsxConstant({FF, FF_ESR})
721 public static final int DOM_VK_WIN_ICO_CLEAR = 230;
722
723
724 @JsxConstant({FF, FF_ESR})
725 public static final int DOM_VK_WIN_OEM_RESET = 233;
726
727
728 @JsxConstant({FF, FF_ESR})
729 public static final int DOM_VK_WIN_OEM_JUMP = 234;
730
731
732 @JsxConstant({FF, FF_ESR})
733 public static final int DOM_VK_WIN_OEM_PA1 = 235;
734
735
736 @JsxConstant({FF, FF_ESR})
737 public static final int DOM_VK_WIN_OEM_PA2 = 236;
738
739
740 @JsxConstant({FF, FF_ESR})
741 public static final int DOM_VK_WIN_OEM_PA3 = 237;
742
743
744 @JsxConstant({FF, FF_ESR})
745 public static final int DOM_VK_WIN_OEM_WSCTRL = 238;
746
747
748 @JsxConstant({FF, FF_ESR})
749 public static final int DOM_VK_WIN_OEM_CUSEL = 239;
750
751
752 @JsxConstant({FF, FF_ESR})
753 public static final int DOM_VK_WIN_OEM_ATTN = 240;
754
755
756 @JsxConstant({FF, FF_ESR})
757 public static final int DOM_VK_WIN_OEM_FINISH = 241;
758
759
760 @JsxConstant({FF, FF_ESR})
761 public static final int DOM_VK_WIN_OEM_COPY = 242;
762
763
764 @JsxConstant({FF, FF_ESR})
765 public static final int DOM_VK_WIN_OEM_AUTO = 243;
766
767
768 @JsxConstant({FF, FF_ESR})
769 public static final int DOM_VK_WIN_OEM_ENLW = 244;
770
771
772 @JsxConstant({FF, FF_ESR})
773 public static final int DOM_VK_WIN_OEM_BACKTAB = 245;
774
775
776 @JsxConstant({FF, FF_ESR})
777 public static final int DOM_VK_ATTN = 246;
778
779
780 @JsxConstant({FF, FF_ESR})
781 public static final int DOM_VK_CRSEL = 247;
782
783
784 @JsxConstant({FF, FF_ESR})
785 public static final int DOM_VK_EXSEL = 248;
786
787
788 @JsxConstant({FF, FF_ESR})
789 public static final int DOM_VK_EREOF = 249;
790
791
792 @JsxConstant({FF, FF_ESR})
793 public static final int DOM_VK_PLAY = 250;
794
795
796 @JsxConstant({FF, FF_ESR})
797 public static final int DOM_VK_ZOOM = 251;
798
799
800 @JsxConstant({FF, FF_ESR})
801 public static final int DOM_VK_PA1 = 253;
802
803
804 @JsxConstant({FF, FF_ESR})
805 public static final int DOM_VK_WIN_OEM_CLEAR = 254;
806
807
808
809
810
811
812 private static final Map<Character, Integer> KEX_CODE_MAP = new HashMap<>();
813 static {
814 KEX_CODE_MAP.put('`', DOM_VK_BACK_QUOTE);
815 KEX_CODE_MAP.put('~', DOM_VK_BACK_QUOTE);
816 KEX_CODE_MAP.put('!', DOM_VK_1);
817 KEX_CODE_MAP.put('@', DOM_VK_2);
818 KEX_CODE_MAP.put('#', DOM_VK_3);
819 KEX_CODE_MAP.put('$', DOM_VK_4);
820 KEX_CODE_MAP.put('%', DOM_VK_5);
821 KEX_CODE_MAP.put('^', DOM_VK_6);
822 KEX_CODE_MAP.put('&', DOM_VK_7);
823 KEX_CODE_MAP.put('*', DOM_VK_8);
824 KEX_CODE_MAP.put('(', DOM_VK_9);
825 KEX_CODE_MAP.put(')', DOM_VK_0);
826
827 KEX_CODE_MAP.put('-', DOM_VK_HYPHEN_MINUS);
828 KEX_CODE_MAP.put('_', DOM_VK_HYPHEN_MINUS);
829
830 KEX_CODE_MAP.put('+', DOM_VK_EQUALS);
831 KEX_CODE_MAP.put('[', DOM_VK_OPEN_BRACKET);
832 KEX_CODE_MAP.put('{', DOM_VK_OPEN_BRACKET);
833 KEX_CODE_MAP.put(']', DOM_VK_CLOSE_BRACKET);
834 KEX_CODE_MAP.put('}', DOM_VK_CLOSE_BRACKET);
835
836 KEX_CODE_MAP.put(':', DOM_VK_SEMICOLON);
837 KEX_CODE_MAP.put('\'', DOM_VK_QUOTE);
838 KEX_CODE_MAP.put('"', DOM_VK_QUOTE);
839 KEX_CODE_MAP.put(',', DOM_VK_COMMA);
840 KEX_CODE_MAP.put('<', DOM_VK_COMMA);
841 KEX_CODE_MAP.put('.', DOM_VK_PERIOD);
842 KEX_CODE_MAP.put('>', DOM_VK_PERIOD);
843 KEX_CODE_MAP.put('/', DOM_VK_SLASH);
844 KEX_CODE_MAP.put('?', DOM_VK_SLASH);
845 KEX_CODE_MAP.put('\\', DOM_VK_BACK_SLASH);
846 KEX_CODE_MAP.put('|', DOM_VK_BACK_SLASH);
847 }
848
849
850
851
852
853
854 private String key_ = "";
855
856
857 private String code_ = "";
858
859
860 private int location_;
861
862
863 private boolean metaKey_;
864
865
866 private boolean repeat_;
867
868
869 private boolean isComposing_;
870
871
872
873
874
875
876 private int charCode_;
877
878
879 private int which_;
880
881
882
883
884 public KeyboardEvent() {
885 super();
886 }
887
888
889
890
891
892
893
894
895
896
897
898 public KeyboardEvent(final DomNode domNode, final String type, final char character,
899 final boolean shiftKey, final boolean ctrlKey, final boolean altKey) {
900 super(domNode, type);
901
902 setShiftKey(shiftKey);
903 setCtrlKey(ctrlKey);
904 setAltKey(altKey);
905
906 if ('\n' == character) {
907 setKeyCode(DOM_VK_RETURN);
908 charCode_ = DOM_VK_RETURN;
909 which_ = DOM_VK_RETURN;
910 return;
911 }
912
913 final int keyCode;
914 if (Event.TYPE_KEY_PRESS.equals(getType())) {
915 keyCode = Integer.valueOf(character);
916 }
917 else {
918 keyCode = Integer.valueOf(charToKeyCode(character));
919 }
920 setKeyCode(keyCode);
921 if (Event.TYPE_KEY_PRESS.equals(getType())) {
922 charCode_ = character;
923 }
924 which_ = charCode_ == 0 ? keyCode : Integer.valueOf(charCode_);
925
926 key_ = determineKey();
927 code_ = determineCode();
928 }
929
930
931
932
933
934
935
936
937
938
939
940 public KeyboardEvent(final DomNode domNode, final String type, final int keyCode,
941 final boolean shiftKey, final boolean ctrlKey, final boolean altKey) {
942 super(domNode, type);
943
944 if (isAmbiguousKeyCode(keyCode)) {
945 throw new IllegalArgumentException("Please use the 'char' constructor instead of int");
946 }
947 setKeyCode(keyCode);
948 if (Event.TYPE_KEY_PRESS.equals(getType())) {
949 which_ = 0;
950 }
951 else {
952 which_ = keyCode;
953 }
954 setShiftKey(shiftKey);
955 setCtrlKey(ctrlKey);
956 setAltKey(altKey);
957
958 key_ = determineKey();
959 code_ = determineCode();
960 }
961
962
963
964
965
966
967
968 public static boolean isShiftNeeded(final char ch, final boolean shiftKey) {
969 return "~!@#$%^&*()_+{}:\"<>?|".indexOf(ch) != -1
970 || (!shiftKey && ch >= 'A' && ch <= 'Z');
971 }
972
973
974 private static boolean isAmbiguousKeyCode(final int keyCode) {
975 return (keyCode >= DOM_VK_0 && keyCode <= DOM_VK_9) || (keyCode >= DOM_VK_A && keyCode <= DOM_VK_Z);
976 }
977
978
979
980
981
982
983
984 private static int charToKeyCode(final char c) {
985 if (c >= 'a' && c <= 'z') {
986 return 'A' + c - 'a';
987 }
988
989 final Integer i = KEX_CODE_MAP.get(c);
990 if (i != null) {
991 return i;
992 }
993 return c;
994 }
995
996
997
998
999
1000 private String determineKey() {
1001 int code = getKeyCode();
1002 if (code == 0) {
1003 code = getCharCode();
1004 }
1005 return switch (code) {
1006 case DOM_VK_SHIFT -> "Shift";
1007 case DOM_VK_PERIOD -> ".";
1008 case DOM_VK_RETURN -> "Enter";
1009 default -> String.valueOf(isShiftKey() ? (char) which_ : Character.toLowerCase((char) which_));
1010 };
1011 }
1012
1013
1014
1015
1016
1017 private String determineCode() {
1018 int code = getKeyCode();
1019 if (code == 0) {
1020 code = getCharCode();
1021 }
1022 return switch (code) {
1023 case DOM_VK_SHIFT -> "ShiftLeft";
1024 case DOM_VK_PERIOD, '.' -> "Period";
1025 case DOM_VK_RETURN -> "Enter";
1026 default -> "Key" + Character.toUpperCase((char) which_);
1027 };
1028 }
1029
1030
1031
1032
1033
1034
1035
1036 @JsxConstructor
1037 @Override
1038 public void jsConstructor(final String type, final ScriptableObject details) {
1039 super.jsConstructor(type, details);
1040
1041 if (details != null && !JavaScriptEngine.isUndefined(details)) {
1042
1043 final Object key = details.get("key", details);
1044 if (!isMissingOrUndefined(key)) {
1045 setKey(JavaScriptEngine.toString(key));
1046 }
1047
1048 final Object code = details.get("code", details);
1049 if (!isMissingOrUndefined(code)) {
1050 setCode(JavaScriptEngine.toString(code));
1051 }
1052
1053 final Object location = details.get("location", details);
1054 if (!isMissingOrUndefined(location)) {
1055 setLocation(JavaScriptEngine.toInt32(location));
1056 }
1057
1058 final Object ctrlKey = details.get("ctrlKey", details);
1059 if (!isMissingOrUndefined(ctrlKey)) {
1060 setCtrlKey(JavaScriptEngine.toBoolean(ctrlKey));
1061 }
1062
1063 final Object shiftKey = details.get("shiftKey", details);
1064 if (!isMissingOrUndefined(shiftKey)) {
1065 setShiftKey(JavaScriptEngine.toBoolean(shiftKey));
1066 }
1067
1068 final Object altKey = details.get("altKey", details);
1069 if (!isMissingOrUndefined(altKey)) {
1070 setAltKey(JavaScriptEngine.toBoolean(altKey));
1071 }
1072
1073 final Object metaKey = details.get("metaKey", details);
1074 if (!isMissingOrUndefined(metaKey)) {
1075 setMetaKey(JavaScriptEngine.toBoolean(metaKey));
1076 }
1077
1078 final Object repeat = details.get("repeat", details);
1079 if (!isMissingOrUndefined(repeat)) {
1080 setRepeat(JavaScriptEngine.toBoolean(repeat));
1081 }
1082
1083 final Object isComposing = details.get("isComposing", details);
1084 if (!isMissingOrUndefined(isComposing)) {
1085 setIsComposing(JavaScriptEngine.toBoolean(isComposing));
1086 }
1087
1088 final Object charCode = details.get("charCode", details);
1089 if (!isMissingOrUndefined(charCode)) {
1090 setCharCode(JavaScriptEngine.toInt32(charCode));
1091 }
1092
1093 final Object keyCode = details.get("keyCode", details);
1094 if (!isMissingOrUndefined(keyCode)) {
1095 setKeyCode(JavaScriptEngine.toInt32(keyCode));
1096 }
1097
1098 if (getBrowserVersion().hasFeature(JS_EVENT_KEYBOARD_CTOR_WHICH)) {
1099 final Object which = details.get("which", details);
1100 if (!isMissingOrUndefined(which)) {
1101 setWhich(JavaScriptEngine.toInt32(which));
1102 }
1103 }
1104 }
1105 }
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121 @JsxFunction
1122 public void initKeyboardEvent(
1123 final String type,
1124 final boolean bubbles,
1125 final boolean cancelable,
1126 final Object view,
1127 final String key,
1128 final int location,
1129 final boolean ctrlKey,
1130 final boolean altKey,
1131 final boolean shiftKey,
1132 final boolean metaKey) {
1133
1134 initUIEvent(type, bubbles, cancelable, view, 0);
1135 setKey(key);
1136 setLocation(location);
1137 setCtrlKey(ctrlKey);
1138 setAltKey(altKey);
1139 setShiftKey(shiftKey);
1140 setKeyCode(0);
1141 setMetaKey(metaKey);
1142 charCode_ = 0;
1143 }
1144
1145
1146
1147
1148
1149 @JsxGetter
1150 public int getCharCode() {
1151 return charCode_;
1152 }
1153
1154
1155
1156
1157
1158 protected void setCharCode(final int charCode) {
1159 charCode_ = charCode;
1160 }
1161
1162
1163
1164
1165
1166 @Override
1167 public int getWhich() {
1168 return which_;
1169 }
1170
1171
1172
1173
1174
1175 protected void setWhich(final int which) {
1176 which_ = which;
1177 }
1178
1179
1180
1181
1182 @Override
1183 @JsxGetter
1184 public int getKeyCode() {
1185 return super.getKeyCode();
1186 }
1187
1188
1189
1190
1191 @Override
1192 @JsxGetter
1193 public boolean isShiftKey() {
1194 return super.isShiftKey();
1195 }
1196
1197
1198
1199
1200 @Override
1201 @JsxGetter
1202 public boolean isCtrlKey() {
1203 return super.isCtrlKey();
1204 }
1205
1206
1207
1208
1209 @Override
1210 @JsxGetter
1211 public boolean isAltKey() {
1212 return super.isAltKey();
1213 }
1214
1215
1216
1217
1218
1219 @JsxGetter
1220 public String getKey() {
1221 return key_;
1222 }
1223
1224
1225
1226
1227
1228 protected void setKey(final String key) {
1229 key_ = key;
1230 }
1231
1232
1233
1234
1235
1236 @JsxGetter
1237 public String getCode() {
1238 return code_;
1239 }
1240
1241
1242
1243
1244
1245 protected void setCode(final String code) {
1246 code_ = code;
1247 }
1248
1249
1250
1251
1252
1253 @JsxGetter
1254 public boolean isMetaKey() {
1255 return metaKey_;
1256 }
1257
1258
1259
1260
1261
1262 protected void setMetaKey(final boolean metaKey) {
1263 metaKey_ = metaKey;
1264 }
1265
1266
1267
1268
1269
1270 @JsxGetter
1271 public int getLocation() {
1272 return location_;
1273 }
1274
1275
1276
1277
1278
1279 protected void setLocation(final int location) {
1280 location_ = location;
1281 }
1282
1283
1284
1285
1286
1287 @JsxGetter
1288 public boolean isRepeat() {
1289 return repeat_;
1290 }
1291
1292
1293
1294
1295
1296 protected void setRepeat(final boolean repeat) {
1297 repeat_ = repeat;
1298 }
1299
1300
1301
1302
1303
1304 @JsxGetter
1305 public boolean getIsComposing() {
1306 return isComposing_;
1307 }
1308
1309
1310
1311
1312
1313 protected void setIsComposing(final boolean isComposing) {
1314 isComposing_ = isComposing;
1315 }
1316 }