View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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.BrowserVersionFeatures.KEYBOARD_EVENT_OEM_KEY_CODES;
19  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
20  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.htmlunit.Page;
26  import org.htmlunit.corejs.javascript.Context;
27  import org.htmlunit.corejs.javascript.Function;
28  import org.htmlunit.corejs.javascript.Scriptable;
29  import org.htmlunit.corejs.javascript.ScriptableObject;
30  import org.htmlunit.corejs.javascript.VarScope;
31  import org.htmlunit.html.DomNode;
32  import org.htmlunit.javascript.JavaScriptEngine;
33  import org.htmlunit.javascript.configuration.JsxClass;
34  import org.htmlunit.javascript.configuration.JsxConstant;
35  import org.htmlunit.javascript.configuration.JsxConstructor;
36  import org.htmlunit.javascript.configuration.JsxFunction;
37  import org.htmlunit.javascript.configuration.JsxGetter;
38  
39  /**
40   * JavaScript host object for {@code KeyboardEvent}.
41   *
42   * @author Ahmed Ashour
43   * @author Frank Danek
44   * @author Ronald Brill
45   * @author Joerg Werner
46   *
47   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent">MDN Documentation</a>
48   */
49  @JsxClass
50  public class KeyboardEvent extends UIEvent {
51  
52      /** Constant for {@code DOM_KEY_LOCATION_STANDARD}. */
53      @JsxConstant
54      public static final int DOM_KEY_LOCATION_STANDARD = 0;
55  
56      /** Constant for {@code DOM_KEY_LOCATION_LEFT}. */
57      @JsxConstant
58      public static final int DOM_KEY_LOCATION_LEFT = 1;
59  
60      /** Constant for {@code DOM_KEY_LOCATION_RIGHT}. */
61      @JsxConstant
62      public static final int DOM_KEY_LOCATION_RIGHT = 2;
63  
64      /** Constant for {@code DOM_KEY_LOCATION_NUMPAD}. */
65      @JsxConstant
66      public static final int DOM_KEY_LOCATION_NUMPAD = 3;
67  
68      /** Constant for {@code DOM_VK_CANCEL}. */
69      @JsxConstant({FF, FF_ESR})
70      public static final int DOM_VK_CANCEL = 3;
71  
72      /** Constant for {@code DOM_VK_HELP}. */
73      @JsxConstant({FF, FF_ESR})
74      public static final int DOM_VK_HELP = 6;
75  
76      /** Constant for {@code DOM_VK_BACK_SPACE}. */
77      @JsxConstant({FF, FF_ESR})
78      public static final int DOM_VK_BACK_SPACE = 8;
79  
80      /** Constant for {@code DOM_VK_TAB}. */
81      @JsxConstant({FF, FF_ESR})
82      public static final int DOM_VK_TAB = 9;
83  
84      /** Constant for {@code DOM_VK_CLEAR}. */
85      @JsxConstant({FF, FF_ESR})
86      public static final int DOM_VK_CLEAR = 12;
87  
88      /** Constant for {@code DOM_VK_RETURN}. */
89      @JsxConstant({FF, FF_ESR})
90      public static final int DOM_VK_RETURN = 13;
91  
92      /** Constant for {@code DOM_VK_SHIFT}. */
93      @JsxConstant({FF, FF_ESR})
94      public static final int DOM_VK_SHIFT = 16;
95  
96      /** Constant for {@code DOM_VK_CONTROL}. */
97      @JsxConstant({FF, FF_ESR})
98      public static final int DOM_VK_CONTROL = 17;
99  
100     /** Constant for {@code DOM_VK_ALT}. */
101     @JsxConstant({FF, FF_ESR})
102     public static final int DOM_VK_ALT = 18;
103 
104     /** Constant for {@code DOM_VK_PAUSE}. */
105     @JsxConstant({FF, FF_ESR})
106     public static final int DOM_VK_PAUSE = 19;
107 
108     /** Constant for {@code DOM_VK_CAPS_LOCK}. */
109     @JsxConstant({FF, FF_ESR})
110     public static final int DOM_VK_CAPS_LOCK = 20;
111 
112     /** Constant for {@code DOM_VK_HANGUL}. */
113     @JsxConstant({FF, FF_ESR})
114     public static final int DOM_VK_HANGUL = 21;
115 
116     /** Constant for {@code DOM_VK_KANA}. */
117     @JsxConstant({FF, FF_ESR})
118     public static final int DOM_VK_KANA = 21;
119 
120     /** Constant for {@code DOM_VK_EISU}. */
121     @JsxConstant({FF, FF_ESR})
122     public static final int DOM_VK_EISU = 22;
123 
124     /** Constant for {@code DOM_VK_FINAL}. */
125     @JsxConstant({FF, FF_ESR})
126     public static final int DOM_VK_FINAL = 24;
127 
128     /** Constant for {@code DOM_VK_JUNJA}. */
129     @JsxConstant({FF, FF_ESR})
130     public static final int DOM_VK_JUNJA = 23;
131 
132     /** Constant for {@code DOM_VK_HANJA}. */
133     @JsxConstant({FF, FF_ESR})
134     public static final int DOM_VK_HANJA = 25;
135 
136     /** Constant for {@code DOM_VK_KANJI}. */
137     @JsxConstant({FF, FF_ESR})
138     public static final int DOM_VK_KANJI = 25;
139 
140     /** Constant for {@code DOM_VK_ESCAPE}. */
141     @JsxConstant({FF, FF_ESR})
142     public static final int DOM_VK_ESCAPE = 27;
143 
144     /** Constant for {@code DOM_VK_CONVERT}. */
145     @JsxConstant({FF, FF_ESR})
146     public static final int DOM_VK_CONVERT = 28;
147 
148     /** Constant for {@code DOM_VK_NONCONVERT}. */
149     @JsxConstant({FF, FF_ESR})
150     public static final int DOM_VK_NONCONVERT = 29;
151 
152     /** Constant for {@code DOM_VK_ACCEPT}. */
153     @JsxConstant({FF, FF_ESR})
154     public static final int DOM_VK_ACCEPT = 30;
155 
156     /** Constant for {@code DOM_VK_MODECHANGE}. */
157     @JsxConstant({FF, FF_ESR})
158     public static final int DOM_VK_MODECHANGE = 31;
159 
160     /** Constant for {@code DOM_VK_SPACE}. */
161     @JsxConstant({FF, FF_ESR})
162     public static final int DOM_VK_SPACE = 32;
163 
164     /** Constant for {@code DOM_VK_PAGE_UP}. */
165     @JsxConstant({FF, FF_ESR})
166     public static final int DOM_VK_PAGE_UP = 33;
167 
168     /** Constant for {@code DOM_VK_PAGE_DOWN}. */
169     @JsxConstant({FF, FF_ESR})
170     public static final int DOM_VK_PAGE_DOWN = 34;
171 
172     /** Constant for {@code DOM_VK_END}. */
173     @JsxConstant({FF, FF_ESR})
174     public static final int DOM_VK_END = 35;
175 
176     /** Constant for {@code DOM_VK_HOME}. */
177     @JsxConstant({FF, FF_ESR})
178     public static final int DOM_VK_HOME = 36;
179 
180     /** Constant for {@code DOM_VK_LEFT}. */
181     @JsxConstant({FF, FF_ESR})
182     public static final int DOM_VK_LEFT = 37;
183 
184     /** Constant for {@code DOM_VK_UP}. */
185     @JsxConstant({FF, FF_ESR})
186     public static final int DOM_VK_UP = 38;
187 
188     /** Constant for {@code DOM_VK_RIGHT}. */
189     @JsxConstant({FF, FF_ESR})
190     public static final int DOM_VK_RIGHT = 39;
191 
192     /** Constant for {@code DOM_VK_SELECT}. */
193     @JsxConstant({FF, FF_ESR})
194     public static final int DOM_VK_SELECT = 41;
195 
196     /** Constant for {@code DOM_VK_DOWN}. */
197     @JsxConstant({FF, FF_ESR})
198     public static final int DOM_VK_DOWN = 40;
199 
200     /** Constant for {@code DOM_VK_PRINT}. */
201     @JsxConstant({FF, FF_ESR})
202     public static final int DOM_VK_PRINT = 42;
203 
204     /** Constant for {@code DOM_VK_EXECUTE}. */
205     @JsxConstant({FF, FF_ESR})
206     public static final int DOM_VK_EXECUTE = 43;
207 
208     /** Constant for {@code DOM_VK_PRINTSCREEN}. */
209     @JsxConstant({FF, FF_ESR})
210     public static final int DOM_VK_PRINTSCREEN = 44;
211 
212     /** Constant for {@code DOM_VK_INSERT}. */
213     @JsxConstant({FF, FF_ESR})
214     public static final int DOM_VK_INSERT = 45;
215 
216     /** Constant for {@code DOM_VK_DELETE}. */
217     @JsxConstant({FF, FF_ESR})
218     public static final int DOM_VK_DELETE = 46;
219 
220     /** Constant for {@code DOM_VK_0}. */
221     @JsxConstant({FF, FF_ESR})
222     public static final int DOM_VK_0 = 48;
223 
224     /** Constant for {@code DOM_VK_1}. */
225     @JsxConstant({FF, FF_ESR})
226     public static final int DOM_VK_1 = 49;
227 
228     /** Constant for {@code DOM_VK_2}. */
229     @JsxConstant({FF, FF_ESR})
230     public static final int DOM_VK_2 = 50;
231 
232     /** Constant for {@code DOM_VK_3}. */
233     @JsxConstant({FF, FF_ESR})
234     public static final int DOM_VK_3 = 51;
235 
236     /** Constant for {@code DOM_VK_4}. */
237     @JsxConstant({FF, FF_ESR})
238     public static final int DOM_VK_4 = 52;
239 
240     /** Constant for {@code DOM_VK_5}. */
241     @JsxConstant({FF, FF_ESR})
242     public static final int DOM_VK_5 = 53;
243 
244     /** Constant for {@code DOM_VK_6}. */
245     @JsxConstant({FF, FF_ESR})
246     public static final int DOM_VK_6 = 54;
247 
248     /** Constant for {@code DOM_VK_7}. */
249     @JsxConstant({FF, FF_ESR})
250     public static final int DOM_VK_7 = 55;
251 
252     /** Constant for {@code DOM_VK_8}. */
253     @JsxConstant({FF, FF_ESR})
254     public static final int DOM_VK_8 = 56;
255 
256     /** Constant for {@code DOM_VK_9}. */
257     @JsxConstant({FF, FF_ESR})
258     public static final int DOM_VK_9 = 57;
259 
260     /** Constant for {@code DOM_VK_COLON}. */
261     @JsxConstant({FF, FF_ESR})
262     public static final int DOM_VK_COLON = 58;
263 
264     /** Constant for {@code DOM_VK_SEMICOLON}. */
265     @JsxConstant({FF, FF_ESR})
266     public static final int DOM_VK_SEMICOLON = 59;
267 
268     /** Constant for {@code DOM_VK_LESS_THAN}. */
269     @JsxConstant({FF, FF_ESR})
270     public static final int DOM_VK_LESS_THAN = 60;
271 
272     /** Constant for {@code DOM_VK_EQUALS}. */
273     @JsxConstant({FF, FF_ESR})
274     public static final int DOM_VK_EQUALS = 61;
275 
276     /** Constant for {@code DOM_VK_GREATER_THAN}. */
277     @JsxConstant({FF, FF_ESR})
278     public static final int DOM_VK_GREATER_THAN = 62;
279 
280     /** Constant for {@code DOM_VK_QUESTION_MARK}. */
281     @JsxConstant({FF, FF_ESR})
282     public static final int DOM_VK_QUESTION_MARK = 63;
283 
284     /** Constant for {@code DOM_VK_AT}. */
285     @JsxConstant({FF, FF_ESR})
286     public static final int DOM_VK_AT = 64;
287 
288     /** Constant for {@code DOM_VK_A}. */
289     @JsxConstant({FF, FF_ESR})
290     public static final int DOM_VK_A = 65;
291 
292     /** Constant for {@code DOM_VK_B}. */
293     @JsxConstant({FF, FF_ESR})
294     public static final int DOM_VK_B = 66;
295 
296     /** Constant for {@code DOM_VK_C}. */
297     @JsxConstant({FF, FF_ESR})
298     public static final int DOM_VK_C = 67;
299 
300     /** Constant for {@code DOM_VK_D}. */
301     @JsxConstant({FF, FF_ESR})
302     public static final int DOM_VK_D = 68;
303 
304     /** Constant for {@code DOM_VK_E}. */
305     @JsxConstant({FF, FF_ESR})
306     public static final int DOM_VK_E = 69;
307 
308     /** Constant for {@code DOM_VK_F}. */
309     @JsxConstant({FF, FF_ESR})
310     public static final int DOM_VK_F = 70;
311 
312     /** Constant for {@code DOM_VK_G}. */
313     @JsxConstant({FF, FF_ESR})
314     public static final int DOM_VK_G = 71;
315 
316     /** Constant for {@code DOM_VK_H}. */
317     @JsxConstant({FF, FF_ESR})
318     public static final int DOM_VK_H = 72;
319 
320     /** Constant for {@code DOM_VK_I}. */
321     @JsxConstant({FF, FF_ESR})
322     public static final int DOM_VK_I = 73;
323 
324     /** Constant for {@code DOM_VK_J}. */
325     @JsxConstant({FF, FF_ESR})
326     public static final int DOM_VK_J = 74;
327 
328     /** Constant for {@code DOM_VK_K}. */
329     @JsxConstant({FF, FF_ESR})
330     public static final int DOM_VK_K = 75;
331 
332     /** Constant for {@code DOM_VK_L}. */
333     @JsxConstant({FF, FF_ESR})
334     public static final int DOM_VK_L = 76;
335 
336     /** Constant for {@code DOM_VK_M}. */
337     @JsxConstant({FF, FF_ESR})
338     public static final int DOM_VK_M = 77;
339 
340     /** Constant for {@code DOM_VK_N}. */
341     @JsxConstant({FF, FF_ESR})
342     public static final int DOM_VK_N = 78;
343 
344     /** Constant for {@code DOM_VK_O}. */
345     @JsxConstant({FF, FF_ESR})
346     public static final int DOM_VK_O = 79;
347 
348     /** Constant for {@code DOM_VK_P}. */
349     @JsxConstant({FF, FF_ESR})
350     public static final int DOM_VK_P = 80;
351 
352     /** Constant for {@code DOM_VK_Q}. */
353     @JsxConstant({FF, FF_ESR})
354     public static final int DOM_VK_Q = 81;
355 
356     /** Constant for {@code DOM_VK_R}. */
357     @JsxConstant({FF, FF_ESR})
358     public static final int DOM_VK_R = 82;
359 
360     /** Constant for {@code DOM_VK_S}. */
361     @JsxConstant({FF, FF_ESR})
362     public static final int DOM_VK_S = 83;
363 
364     /** Constant for {@code DOM_VK_T}. */
365     @JsxConstant({FF, FF_ESR})
366     public static final int DOM_VK_T = 84;
367 
368     /** Constant for {@code DOM_VK_U}. */
369     @JsxConstant({FF, FF_ESR})
370     public static final int DOM_VK_U = 85;
371 
372     /** Constant for {@code DOM_VK_V}. */
373     @JsxConstant({FF, FF_ESR})
374     public static final int DOM_VK_V = 86;
375 
376     /** Constant for {@code DOM_VK_W}. */
377     @JsxConstant({FF, FF_ESR})
378     public static final int DOM_VK_W = 87;
379 
380     /** Constant for {@code DOM_VK_X}. */
381     @JsxConstant({FF, FF_ESR})
382     public static final int DOM_VK_X = 88;
383 
384     /** Constant for {@code DOM_VK_Y}. */
385     @JsxConstant({FF, FF_ESR})
386     public static final int DOM_VK_Y = 89;
387 
388     /** Constant for {@code DOM_VK_Z}. */
389     @JsxConstant({FF, FF_ESR})
390     public static final int DOM_VK_Z = 90;
391 
392     /** Constant for {@code DOM_VK_WIN}. */
393     @JsxConstant({FF, FF_ESR})
394     public static final int DOM_VK_WIN = 91;
395 
396     /** Constant for {@code DOM_VK_CONTEXT_MENU}. */
397     @JsxConstant({FF, FF_ESR})
398     public static final int DOM_VK_CONTEXT_MENU = 93;
399 
400     /** Constant for {@code DOM_VK_SLEEP}. */
401     @JsxConstant({FF, FF_ESR})
402     public static final int DOM_VK_SLEEP = 95;
403 
404     /** Constant for {@code DOM_VK_NUMPAD0}. */
405     @JsxConstant({FF, FF_ESR})
406     public static final int DOM_VK_NUMPAD0 = 96;
407 
408     /** Constant for {@code DOM_VK_NUMPAD1}. */
409     @JsxConstant({FF, FF_ESR})
410     public static final int DOM_VK_NUMPAD1 = 97;
411 
412     /** Constant for {@code DOM_VK_NUMPAD2}. */
413     @JsxConstant({FF, FF_ESR})
414     public static final int DOM_VK_NUMPAD2 = 98;
415 
416     /** Constant for {@code DOM_VK_NUMPAD3}. */
417     @JsxConstant({FF, FF_ESR})
418     public static final int DOM_VK_NUMPAD3 = 99;
419 
420     /** Constant for {@code DOM_VK_NUMPAD4}. */
421     @JsxConstant({FF, FF_ESR})
422     public static final int DOM_VK_NUMPAD4 = 100;
423 
424     /** Constant for {@code DOM_VK_NUMPAD5}. */
425     @JsxConstant({FF, FF_ESR})
426     public static final int DOM_VK_NUMPAD5 = 101;
427 
428     /** Constant for {@code DOM_VK_NUMPAD6}. */
429     @JsxConstant({FF, FF_ESR})
430     public static final int DOM_VK_NUMPAD6 = 102;
431 
432     /** Constant for {@code DOM_VK_NUMPAD7}. */
433     @JsxConstant({FF, FF_ESR})
434     public static final int DOM_VK_NUMPAD7 = 103;
435 
436     /** Constant for {@code DOM_VK_NUMPAD8}. */
437     @JsxConstant({FF, FF_ESR})
438     public static final int DOM_VK_NUMPAD8 = 104;
439 
440     /** Constant for {@code DOM_VK_NUMPAD9}. */
441     @JsxConstant({FF, FF_ESR})
442     public static final int DOM_VK_NUMPAD9 = 105;
443 
444     /** Constant for {@code DOM_VK_MULTIPLY}. */
445     @JsxConstant({FF, FF_ESR})
446     public static final int DOM_VK_MULTIPLY = 106;
447 
448     /** Constant for {@code DOM_VK_ADD}. */
449     @JsxConstant({FF, FF_ESR})
450     public static final int DOM_VK_ADD = 107;
451 
452     /** Constant for {@code DOM_VK_SEPARATOR}. */
453     @JsxConstant({FF, FF_ESR})
454     public static final int DOM_VK_SEPARATOR = 108;
455 
456     /** Constant for {@code DOM_VK_SUBTRACT}. */
457     @JsxConstant({FF, FF_ESR})
458     public static final int DOM_VK_SUBTRACT = 109;
459 
460     /** Constant for {@code DOM_VK_DECIMAL}. */
461     @JsxConstant({FF, FF_ESR})
462     public static final int DOM_VK_DECIMAL = 110;
463 
464     /** Constant for {@code DOM_VK_DIVIDE}. */
465     @JsxConstant({FF, FF_ESR})
466     public static final int DOM_VK_DIVIDE = 111;
467 
468     /** Constant for {@code DOM_VK_F1}. */
469     @JsxConstant({FF, FF_ESR})
470     public static final int DOM_VK_F1 = 112;
471 
472     /** Constant for {@code DOM_VK_F2}. */
473     @JsxConstant({FF, FF_ESR})
474     public static final int DOM_VK_F2 = 113;
475 
476     /** Constant for {@code DOM_VK_F3}. */
477     @JsxConstant({FF, FF_ESR})
478     public static final int DOM_VK_F3 = 114;
479 
480     /** Constant for {@code DOM_VK_F4}. */
481     @JsxConstant({FF, FF_ESR})
482     public static final int DOM_VK_F4 = 115;
483 
484     /** Constant for {@code DOM_VK_F5}. */
485     @JsxConstant({FF, FF_ESR})
486     public static final int DOM_VK_F5 = 116;
487 
488     /** Constant for {@code DOM_VK_F6}. */
489     @JsxConstant({FF, FF_ESR})
490     public static final int DOM_VK_F6 = 117;
491 
492     /** Constant for {@code DOM_VK_F7}. */
493     @JsxConstant({FF, FF_ESR})
494     public static final int DOM_VK_F7 = 118;
495 
496     /** Constant for {@code DOM_VK_F8}. */
497     @JsxConstant({FF, FF_ESR})
498     public static final int DOM_VK_F8 = 119;
499 
500     /** Constant for {@code DOM_VK_F9}. */
501     @JsxConstant({FF, FF_ESR})
502     public static final int DOM_VK_F9 = 120;
503 
504     /** Constant for {@code DOM_VK_F10}. */
505     @JsxConstant({FF, FF_ESR})
506     public static final int DOM_VK_F10 = 121;
507 
508     /** Constant for {@code DOM_VK_F11}. */
509     @JsxConstant({FF, FF_ESR})
510     public static final int DOM_VK_F11 = 122;
511 
512     /** Constant for {@code DOM_VK_F12}. */
513     @JsxConstant({FF, FF_ESR})
514     public static final int DOM_VK_F12 = 123;
515 
516     /** Constant for {@code DOM_VK_F13}. */
517     @JsxConstant({FF, FF_ESR})
518     public static final int DOM_VK_F13 = 124;
519 
520     /** Constant for {@code DOM_VK_F14}. */
521     @JsxConstant({FF, FF_ESR})
522     public static final int DOM_VK_F14 = 125;
523 
524     /** Constant for {@code DOM_VK_F15}. */
525     @JsxConstant({FF, FF_ESR})
526     public static final int DOM_VK_F15 = 126;
527 
528     /** Constant for {@code DOM_VK_F16}. */
529     @JsxConstant({FF, FF_ESR})
530     public static final int DOM_VK_F16 = 127;
531 
532     /** Constant for {@code DOM_VK_F17}. */
533     @JsxConstant({FF, FF_ESR})
534     public static final int DOM_VK_F17 = 128;
535 
536     /** Constant for {@code DOM_VK_F18}. */
537     @JsxConstant({FF, FF_ESR})
538     public static final int DOM_VK_F18 = 129;
539 
540     /** Constant for {@code DOM_VK_F19}. */
541     @JsxConstant({FF, FF_ESR})
542     public static final int DOM_VK_F19 = 130;
543 
544     /** Constant for {@code DOM_VK_F20}. */
545     @JsxConstant({FF, FF_ESR})
546     public static final int DOM_VK_F20 = 131;
547 
548     /** Constant for {@code DOM_VK_F21}. */
549     @JsxConstant({FF, FF_ESR})
550     public static final int DOM_VK_F21 = 132;
551 
552     /** Constant for {@code DOM_VK_F22}. */
553     @JsxConstant({FF, FF_ESR})
554     public static final int DOM_VK_F22 = 133;
555 
556     /** Constant for {@code DOM_VK_F23}. */
557     @JsxConstant({FF, FF_ESR})
558     public static final int DOM_VK_F23 = 134;
559 
560     /** Constant for {@code DOM_VK_F24}. */
561     @JsxConstant({FF, FF_ESR})
562     public static final int DOM_VK_F24 = 135;
563 
564     /** Constant for {@code DOM_VK_NUM_LOCK}. */
565     @JsxConstant({FF, FF_ESR})
566     public static final int DOM_VK_NUM_LOCK = 144;
567 
568     /** Constant for {@code DOM_VK_SCROLL_LOCK}. */
569     @JsxConstant({FF, FF_ESR})
570     public static final int DOM_VK_SCROLL_LOCK = 145;
571 
572     /** Constant for {@code DOM_VK_WIN_OEM_FJ_JISHO}. */
573     @JsxConstant({FF, FF_ESR})
574     public static final int DOM_VK_WIN_OEM_FJ_JISHO = 146;
575 
576     /** Constant for {@code DOM_VK_WIN_OEM_FJ_MASSHOU}. */
577     @JsxConstant({FF, FF_ESR})
578     public static final int DOM_VK_WIN_OEM_FJ_MASSHOU = 147;
579 
580     /** Constant for {@code DOM_VK_WIN_OEM_FJ_TOUROKU}. */
581     @JsxConstant({FF, FF_ESR})
582     public static final int DOM_VK_WIN_OEM_FJ_TOUROKU = 148;
583 
584     /** Constant for {@code DOM_VK_WIN_OEM_FJ_LOYA}. */
585     @JsxConstant({FF, FF_ESR})
586     public static final int DOM_VK_WIN_OEM_FJ_LOYA = 149;
587 
588     /** Constant for {@code DOM_VK_WIN_OEM_FJ_ROYA}. */
589     @JsxConstant({FF, FF_ESR})
590     public static final int DOM_VK_WIN_OEM_FJ_ROYA = 150;
591 
592     /** Constant for {@code DOM_VK_CIRCUMFLEX}. */
593     @JsxConstant({FF, FF_ESR})
594     public static final int DOM_VK_CIRCUMFLEX = 160;
595 
596     /** Constant for {@code DOM_VK_EXCLAMATION}. */
597     @JsxConstant({FF, FF_ESR})
598     public static final int DOM_VK_EXCLAMATION = 161;
599 
600     /** Constant for {@code DOM_VK_DOUBLE_QUOTE}. */
601     @JsxConstant({FF, FF_ESR})
602     public static final int DOM_VK_DOUBLE_QUOTE = 162;
603 
604     /** Constant for {@code DOM_VK_HASH}. */
605     @JsxConstant({FF, FF_ESR})
606     public static final int DOM_VK_HASH = 163;
607 
608     /** Constant for {@code DOM_VK_DOLLAR}. */
609     @JsxConstant({FF, FF_ESR})
610     public static final int DOM_VK_DOLLAR = 164;
611 
612     /** Constant for {@code DOM_VK_PERCENT}. */
613     @JsxConstant({FF, FF_ESR})
614     public static final int DOM_VK_PERCENT = 165;
615 
616     /** Constant for {@code DOM_VK_AMPERSAND}. */
617     @JsxConstant({FF, FF_ESR})
618     public static final int DOM_VK_AMPERSAND = 166;
619 
620     /** Constant for {@code DOM_VK_UNDERSCORE}. */
621     @JsxConstant({FF, FF_ESR})
622     public static final int DOM_VK_UNDERSCORE = 167;
623 
624     /** Constant for {@code DOM_VK_OPEN_PAREN}. */
625     @JsxConstant({FF, FF_ESR})
626     public static final int DOM_VK_OPEN_PAREN = 168;
627 
628     /** Constant for {@code DOM_VK_CLOSE_PAREN}. */
629     @JsxConstant({FF, FF_ESR})
630     public static final int DOM_VK_CLOSE_PAREN = 169;
631 
632     /** Constant for {@code DOM_VK_ASTERISK}. */
633     @JsxConstant({FF, FF_ESR})
634     public static final int DOM_VK_ASTERISK = 170;
635 
636     /** Constant for {@code DOM_VK_PLUS}. */
637     @JsxConstant({FF, FF_ESR})
638     public static final int DOM_VK_PLUS = 171;
639 
640     /** Constant for {@code DOM_VK_PIPE}. */
641     @JsxConstant({FF, FF_ESR})
642     public static final int DOM_VK_PIPE = 172;
643 
644     /** Constant for {@code DOM_VK_HYPHEN_MINUS}. */
645     @JsxConstant({FF, FF_ESR})
646     public static final int DOM_VK_HYPHEN_MINUS = 173;
647 
648     /** Constant for {@code DOM_VK_OPEN_CURLY_BRACKET}. */
649     @JsxConstant({FF, FF_ESR})
650     public static final int DOM_VK_OPEN_CURLY_BRACKET = 174;
651 
652     /** Constant for {@code DOM_VK_CLOSE_CURLY_BRACKET}. */
653     @JsxConstant({FF, FF_ESR})
654     public static final int DOM_VK_CLOSE_CURLY_BRACKET = 175;
655 
656     /** Constant for {@code DOM_VK_TILDE}. */
657     @JsxConstant({FF, FF_ESR})
658     public static final int DOM_VK_TILDE = 176;
659 
660     /** Constant for {@code DOM_VK_VOLUME_MUTE}. */
661     @JsxConstant({FF, FF_ESR})
662     public static final int DOM_VK_VOLUME_MUTE = 181;
663 
664     /** Constant for {@code DOM_VK_VOLUME_DOWN}. */
665     @JsxConstant({FF, FF_ESR})
666     public static final int DOM_VK_VOLUME_DOWN = 182;
667 
668     /** Constant for {@code DOM_VK_VOLUME_UP}. */
669     @JsxConstant({FF, FF_ESR})
670     public static final int DOM_VK_VOLUME_UP = 183;
671 
672     /** Constant for {@code DOM_VK_COMMA}. */
673     @JsxConstant({FF, FF_ESR})
674     public static final int DOM_VK_COMMA = 188;
675 
676     /** Constant for {@code DOM_VK_PERIOD}. */
677     @JsxConstant({FF, FF_ESR})
678     public static final int DOM_VK_PERIOD = 190;
679 
680     /** Constant for {@code DOM_VK_SLASH}. */
681     @JsxConstant({FF, FF_ESR})
682     public static final int DOM_VK_SLASH = 191;
683 
684     /** Constant for {@code DOM_VK_BACK_QUOTE}. */
685     @JsxConstant({FF, FF_ESR})
686     public static final int DOM_VK_BACK_QUOTE = 192;
687 
688     /** Constant for {@code DOM_VK_OPEN_BRACKET}. */
689     @JsxConstant({FF, FF_ESR})
690     public static final int DOM_VK_OPEN_BRACKET = 219;
691 
692     /** Constant for {@code DOM_VK_BACK_SLASH}. */
693     @JsxConstant({FF, FF_ESR})
694     public static final int DOM_VK_BACK_SLASH = 220;
695 
696     /** Constant for {@code DOM_VK_CLOSE_BRACKET}. */
697     @JsxConstant({FF, FF_ESR})
698     public static final int DOM_VK_CLOSE_BRACKET = 221;
699 
700     /** Constant for {@code DOM_VK_QUOTE}. */
701     @JsxConstant({FF, FF_ESR})
702     public static final int DOM_VK_QUOTE = 222;
703 
704     /** Constant for {@code DOM_VK_META}. */
705     @JsxConstant({FF, FF_ESR})
706     public static final int DOM_VK_META = 224;
707 
708     /** Constant for {@code DOM_VK_ALTGR}. */
709     @JsxConstant({FF, FF_ESR})
710     public static final int DOM_VK_ALTGR = 225;
711 
712     /** Constant for {@code DOM_VK_WIN_ICO_HELP}. */
713     @JsxConstant({FF, FF_ESR})
714     public static final int DOM_VK_WIN_ICO_HELP = 227;
715 
716     /** Constant for {@code DOM_VK_WIN_ICO_00}. */
717     @JsxConstant({FF, FF_ESR})
718     public static final int DOM_VK_WIN_ICO_00 = 228;
719 
720     /** Constant for {@code DOM_VK_PROCESSKEY}. */
721     @JsxConstant({FF, FF_ESR})
722     public static final int DOM_VK_PROCESSKEY = 229;
723 
724     /** Constant for {@code DOM_VK_WIN_ICO_CLEAR}. */
725     @JsxConstant({FF, FF_ESR})
726     public static final int DOM_VK_WIN_ICO_CLEAR = 230;
727 
728     /** Constant for {@code DOM_VK_WIN_OEM_RESET}. */
729     @JsxConstant({FF, FF_ESR})
730     public static final int DOM_VK_WIN_OEM_RESET = 233;
731 
732     /** Constant for {@code DOM_VK_WIN_OEM_JUMP}. */
733     @JsxConstant({FF, FF_ESR})
734     public static final int DOM_VK_WIN_OEM_JUMP = 234;
735 
736     /** Constant for {@code DOM_VK_WIN_OEM_PA1}. */
737     @JsxConstant({FF, FF_ESR})
738     public static final int DOM_VK_WIN_OEM_PA1 = 235;
739 
740     /** Constant for {@code DOM_VK_WIN_OEM_PA2}. */
741     @JsxConstant({FF, FF_ESR})
742     public static final int DOM_VK_WIN_OEM_PA2 = 236;
743 
744     /** Constant for {@code DOM_VK_WIN_OEM_PA3}. */
745     @JsxConstant({FF, FF_ESR})
746     public static final int DOM_VK_WIN_OEM_PA3 = 237;
747 
748     /** Constant for {@code DOM_VK_WIN_OEM_WSCTRL}. */
749     @JsxConstant({FF, FF_ESR})
750     public static final int DOM_VK_WIN_OEM_WSCTRL = 238;
751 
752     /** Constant for {@code DOM_VK_WIN_OEM_CUSEL}. */
753     @JsxConstant({FF, FF_ESR})
754     public static final int DOM_VK_WIN_OEM_CUSEL = 239;
755 
756     /** Constant for {@code DOM_VK_WIN_OEM_ATTN}. */
757     @JsxConstant({FF, FF_ESR})
758     public static final int DOM_VK_WIN_OEM_ATTN = 240;
759 
760     /** Constant for {@code DOM_VK_WIN_OEM_FINISH}. */
761     @JsxConstant({FF, FF_ESR})
762     public static final int DOM_VK_WIN_OEM_FINISH = 241;
763 
764     /** Constant for {@code DOM_VK_WIN_OEM_COPY}. */
765     @JsxConstant({FF, FF_ESR})
766     public static final int DOM_VK_WIN_OEM_COPY = 242;
767 
768     /** Constant for {@code DOM_VK_WIN_OEM_AUTO}. */
769     @JsxConstant({FF, FF_ESR})
770     public static final int DOM_VK_WIN_OEM_AUTO = 243;
771 
772     /** Constant for {@code DOM_VK_WIN_OEM_ENLW}. */
773     @JsxConstant({FF, FF_ESR})
774     public static final int DOM_VK_WIN_OEM_ENLW = 244;
775 
776     /** Constant for {@code DOM_VK_WIN_OEM_BACKTAB}. */
777     @JsxConstant({FF, FF_ESR})
778     public static final int DOM_VK_WIN_OEM_BACKTAB = 245;
779 
780     /** Constant for {@code DOM_VK_ATTN}. */
781     @JsxConstant({FF, FF_ESR})
782     public static final int DOM_VK_ATTN = 246;
783 
784     /** Constant for {@code DOM_VK_CRSEL}. */
785     @JsxConstant({FF, FF_ESR})
786     public static final int DOM_VK_CRSEL = 247;
787 
788     /** Constant for {@code DOM_VK_EXSEL}. */
789     @JsxConstant({FF, FF_ESR})
790     public static final int DOM_VK_EXSEL = 248;
791 
792     /** Constant for {@code DOM_VK_EREOF}. */
793     @JsxConstant({FF, FF_ESR})
794     public static final int DOM_VK_EREOF = 249;
795 
796     /** Constant for {@code DOM_VK_PLAY}. */
797     @JsxConstant({FF, FF_ESR})
798     public static final int DOM_VK_PLAY = 250;
799 
800     /** Constant for {@code DOM_VK_ZOOM}. */
801     @JsxConstant({FF, FF_ESR})
802     public static final int DOM_VK_ZOOM = 251;
803 
804     /** Constant for {@code DOM_VK_PA1}. */
805     @JsxConstant({FF, FF_ESR})
806     public static final int DOM_VK_PA1 = 253;
807 
808     /** Constant for {@code DOM_VK_WIN_OEM_CLEAR}. */
809     @JsxConstant({FF, FF_ESR})
810     public static final int DOM_VK_WIN_OEM_CLEAR = 254;
811 
812     /** The key code of the ';' key used by Chrome and Edge (Windows VK_OEM_1). */
813     private static final int OEM_KEY_CODE_SEMICOLON = 186;
814 
815     /** The key code of the '=' key used by Chrome and Edge (Windows VK_OEM_PLUS). */
816     private static final int OEM_KEY_CODE_EQUALS = 187;
817 
818     /** The key code of the '-' key used by Chrome and Edge (Windows VK_OEM_MINUS). */
819     private static final int OEM_KEY_CODE_HYPHEN_MINUS = 189;
820 
821     /**
822      * Maps characters to key codes for {@link #TYPE_KEY_DOWN} and {@link #TYPE_KEY_UP} events
823      * where the key code differs from the character code.
824      *
825      * @see <a href="https://www.asquare.net/javascript/tests/KeyCode.html">Key Code Reference</a>
826      */
827     private static final Map<Character, Integer> KEX_CODE_MAP = new HashMap<>();
828     static {
829         KEX_CODE_MAP.put('`', DOM_VK_BACK_QUOTE);
830         KEX_CODE_MAP.put('~', DOM_VK_BACK_QUOTE);
831         KEX_CODE_MAP.put('!', DOM_VK_1);
832         KEX_CODE_MAP.put('@', DOM_VK_2);
833         KEX_CODE_MAP.put('#', DOM_VK_3);
834         KEX_CODE_MAP.put('$', DOM_VK_4);
835         KEX_CODE_MAP.put('%', DOM_VK_5);
836         KEX_CODE_MAP.put('^', DOM_VK_6);
837         KEX_CODE_MAP.put('&', DOM_VK_7);
838         KEX_CODE_MAP.put('*', DOM_VK_8);
839         KEX_CODE_MAP.put('(', DOM_VK_9);
840         KEX_CODE_MAP.put(')', DOM_VK_0);
841         //Chrome/IE 189
842         KEX_CODE_MAP.put('-', DOM_VK_HYPHEN_MINUS);
843         KEX_CODE_MAP.put('_', DOM_VK_HYPHEN_MINUS);
844         //Chrome/IE 187
845         KEX_CODE_MAP.put('+', DOM_VK_EQUALS);
846         KEX_CODE_MAP.put('[', DOM_VK_OPEN_BRACKET);
847         KEX_CODE_MAP.put('{', DOM_VK_OPEN_BRACKET);
848         KEX_CODE_MAP.put(']', DOM_VK_CLOSE_BRACKET);
849         KEX_CODE_MAP.put('}', DOM_VK_CLOSE_BRACKET);
850         //Chrome/IE 186
851         KEX_CODE_MAP.put(':', DOM_VK_SEMICOLON);
852         KEX_CODE_MAP.put('\'', DOM_VK_QUOTE);
853         KEX_CODE_MAP.put('"', DOM_VK_QUOTE);
854         KEX_CODE_MAP.put(',', DOM_VK_COMMA);
855         KEX_CODE_MAP.put('<', DOM_VK_COMMA);
856         KEX_CODE_MAP.put('.', DOM_VK_PERIOD);
857         KEX_CODE_MAP.put('>', DOM_VK_PERIOD);
858         KEX_CODE_MAP.put('/', DOM_VK_SLASH);
859         KEX_CODE_MAP.put('?', DOM_VK_SLASH);
860         KEX_CODE_MAP.put('\\', DOM_VK_BACK_SLASH);
861         KEX_CODE_MAP.put('|', DOM_VK_BACK_SLASH);
862     }
863 
864     /**
865      * The values of the {@code key}, {@code code} and {@code location} properties
866      * of the events for a key code.
867      *
868      * @param key the key value
869      * @param shiftedKey the key value if the shift key is pressed
870      * @param code the code value (the physical key on a US keyboard)
871      * @param location the location (see DOM_KEY_LOCATION_* constants)
872      */
873     private record KeyInfo(String key, String shiftedKey, String code, int location) {
874     }
875 
876     /**
877      * Maps the key codes of the special keys (the keys not typed as character) to the
878      * values of the {@code key}, {@code code} and {@code location} properties.
879      *
880      * @see <a href="https://www.w3.org/TR/uievents-key/">UI Events KeyboardEvent key Values</a>
881      * @see <a href="https://www.w3.org/TR/uievents-code/">UI Events KeyboardEvent code Values</a>
882      */
883     private static final Map<Integer, KeyInfo> KEY_CODE_INFO_MAP = new HashMap<>();
884 
885     /**
886      * Maps the characters to the value of the {@code code} property
887      * (the physical key on a US keyboard); letters and digits are calculated.
888      */
889     private static final Map<Character, String> CHAR_CODE_MAP = new HashMap<>();
890 
891     static {
892         addKey(DOM_VK_CANCEL, "Cancel", "");
893         addKey(DOM_VK_HELP, "Help", "Help");
894         addKey(DOM_VK_BACK_SPACE, "Backspace", "Backspace");
895         addKey(DOM_VK_TAB, "Tab", "Tab");
896         addKey(DOM_VK_CLEAR, "Clear", "NumpadClear");
897         addKey(DOM_VK_RETURN, "Enter", "Enter");
898         addKey(DOM_VK_SHIFT, "Shift", "ShiftLeft", DOM_KEY_LOCATION_LEFT);
899         addKey(DOM_VK_CONTROL, "Control", "ControlLeft", DOM_KEY_LOCATION_LEFT);
900         addKey(DOM_VK_ALT, "Alt", "AltLeft", DOM_KEY_LOCATION_LEFT);
901         addKey(DOM_VK_PAUSE, "Pause", "Pause");
902         addKey(DOM_VK_CAPS_LOCK, "CapsLock", "CapsLock");
903         addKey(DOM_VK_ESCAPE, "Escape", "Escape");
904         addKey(DOM_VK_CONVERT, "Convert", "Convert");
905         addKey(DOM_VK_NONCONVERT, "NonConvert", "NonConvert");
906         addKey(DOM_VK_SPACE, " ", "Space");
907         addKey(DOM_VK_PAGE_UP, "PageUp", "PageUp");
908         addKey(DOM_VK_PAGE_DOWN, "PageDown", "PageDown");
909         addKey(DOM_VK_END, "End", "End");
910         addKey(DOM_VK_HOME, "Home", "Home");
911         addKey(DOM_VK_LEFT, "ArrowLeft", "ArrowLeft");
912         addKey(DOM_VK_UP, "ArrowUp", "ArrowUp");
913         addKey(DOM_VK_RIGHT, "ArrowRight", "ArrowRight");
914         addKey(DOM_VK_DOWN, "ArrowDown", "ArrowDown");
915         addKey(DOM_VK_SELECT, "Select", "Select");
916         addKey(DOM_VK_PRINT, "Print", "");
917         addKey(DOM_VK_EXECUTE, "Execute", "");
918         addKey(DOM_VK_PRINTSCREEN, "PrintScreen", "PrintScreen");
919         addKey(DOM_VK_INSERT, "Insert", "Insert");
920         addKey(DOM_VK_DELETE, "Delete", "Delete");
921         addKey(DOM_VK_WIN, "Meta", "MetaLeft", DOM_KEY_LOCATION_LEFT);
922         addKey(DOM_VK_CONTEXT_MENU, "ContextMenu", "ContextMenu");
923         addKey(DOM_VK_SLEEP, "Standby", "Sleep");
924 
925         for (int i = 0; i <= 9; i++) {
926             addKey(DOM_VK_NUMPAD0 + i, Integer.toString(i), "Numpad" + i, DOM_KEY_LOCATION_NUMPAD);
927         }
928         addKey(DOM_VK_MULTIPLY, "*", "NumpadMultiply", DOM_KEY_LOCATION_NUMPAD);
929         addKey(DOM_VK_ADD, "+", "NumpadAdd", DOM_KEY_LOCATION_NUMPAD);
930         addKey(DOM_VK_SEPARATOR, ",", "NumpadComma", DOM_KEY_LOCATION_NUMPAD);
931         addKey(DOM_VK_SUBTRACT, "-", "NumpadSubtract", DOM_KEY_LOCATION_NUMPAD);
932         addKey(DOM_VK_DECIMAL, ".", "NumpadDecimal", DOM_KEY_LOCATION_NUMPAD);
933         addKey(DOM_VK_DIVIDE, "/", "NumpadDivide", DOM_KEY_LOCATION_NUMPAD);
934 
935         for (int i = 1; i <= 24; i++) {
936             addKey(DOM_VK_F1 + i - 1, "F" + i, "F" + i);
937         }
938 
939         addKey(DOM_VK_NUM_LOCK, "NumLock", "NumLock");
940         addKey(DOM_VK_SCROLL_LOCK, "ScrollLock", "ScrollLock");
941         addKey(DOM_VK_VOLUME_MUTE, "AudioVolumeMute", "AudioVolumeMute");
942         addKey(DOM_VK_VOLUME_DOWN, "AudioVolumeDown", "AudioVolumeDown");
943         addKey(DOM_VK_VOLUME_UP, "AudioVolumeUp", "AudioVolumeUp");
944         addKey(DOM_VK_META, "Meta", "MetaLeft", DOM_KEY_LOCATION_LEFT);
945         addKey(DOM_VK_ALTGR, "AltGraph", "AltRight", DOM_KEY_LOCATION_RIGHT);
946         addKey(DOM_VK_PROCESSKEY, "Process", "");
947         addKey(DOM_VK_ATTN, "Attn", "");
948         addKey(DOM_VK_CRSEL, "CrSel", "");
949         addKey(DOM_VK_EXSEL, "ExSel", "");
950         addKey(DOM_VK_EREOF, "EraseEof", "");
951         addKey(DOM_VK_PLAY, "Play", "");
952         addKey(DOM_VK_ZOOM, "ZoomToggle", "");
953 
954         // the punctuation keys
955         addPunctuation(DOM_VK_SEMICOLON, ';', ':', "Semicolon");
956         addPunctuation(DOM_VK_EQUALS, '=', '+', "Equal");
957         addPunctuation(DOM_VK_HYPHEN_MINUS, '-', '_', "Minus");
958         addPunctuation(OEM_KEY_CODE_SEMICOLON, ';', ':', "Semicolon");
959         addPunctuation(OEM_KEY_CODE_EQUALS, '=', '+', "Equal");
960         addPunctuation(OEM_KEY_CODE_HYPHEN_MINUS, '-', '_', "Minus");
961         addPunctuation(DOM_VK_COMMA, ',', '<', "Comma");
962         addPunctuation(DOM_VK_PERIOD, '.', '>', "Period");
963         addPunctuation(DOM_VK_SLASH, '/', '?', "Slash");
964         addPunctuation(DOM_VK_BACK_QUOTE, '`', '~', "Backquote");
965         addPunctuation(DOM_VK_OPEN_BRACKET, '[', '{', "BracketLeft");
966         addPunctuation(DOM_VK_BACK_SLASH, '\\', '|', "Backslash");
967         addPunctuation(DOM_VK_CLOSE_BRACKET, ']', '}', "BracketRight");
968         addPunctuation(DOM_VK_QUOTE, '\'', '"', "Quote");
969 
970         final String shiftedDigits = ")!@#$%^&*(";
971         for (int i = 0; i <= 9; i++) {
972             CHAR_CODE_MAP.put((char) ('0' + i), "Digit" + i);
973             CHAR_CODE_MAP.put(shiftedDigits.charAt(i), "Digit" + i);
974         }
975         CHAR_CODE_MAP.put(' ', "Space");
976         CHAR_CODE_MAP.put('\n', "Enter");
977         CHAR_CODE_MAP.put('\r', "Enter");
978         CHAR_CODE_MAP.put('\t', "Tab");
979         CHAR_CODE_MAP.put('\b', "Backspace");
980     }
981 
982     private static void addKey(final int keyCode, final String key, final String code) {
983         addKey(keyCode, key, code, DOM_KEY_LOCATION_STANDARD);
984     }
985 
986     private static void addKey(final int keyCode, final String key, final String code, final int location) {
987         KEY_CODE_INFO_MAP.put(keyCode, new KeyInfo(key, key, code, location));
988     }
989 
990     private static void addPunctuation(final int keyCode, final char key, final char shiftedKey, final String code) {
991         KEY_CODE_INFO_MAP.put(keyCode,
992                 new KeyInfo(String.valueOf(key), String.valueOf(shiftedKey), code, DOM_KEY_LOCATION_STANDARD));
993         CHAR_CODE_MAP.put(key, code);
994         CHAR_CODE_MAP.put(shiftedKey, code);
995     }
996 
997     /*
998      * Standard properties
999      */
1000 
1001     /** The key value of the key represented by the event. */
1002     private String key_ = "";
1003 
1004     /** The code value of the physical key represented by the event. */
1005     private String code_ = "";
1006 
1007     /** The location of the key on the keyboard or other input device. See DOM_KEY_LOCATION_* constants. */
1008     private int location_;
1009 
1010     /** Whether or not the "meta" key was pressed during the firing of the event. */
1011     private boolean metaKey_;
1012 
1013     /** Whether the key is being held down such that it is automatically repeating. */
1014     private boolean repeat_;
1015 
1016     /** Whether the event is fired after the compositionstart and before the compositionend events. */
1017     private boolean isComposing_;
1018 
1019     /*
1020      * Deprecated properties
1021      */
1022 
1023     /** The Unicode reference number of the key. */
1024     private int charCode_;
1025 
1026     /** The unmodified value of the pressed key. This is usually the same as keyCode. */
1027     private int which_;
1028 
1029     /**
1030      * Creates a new keyboard event instance.
1031      */
1032     public KeyboardEvent() {
1033         super();
1034     }
1035 
1036     /**
1037      * Creates a new keyboard event instance for a character key.
1038      *
1039      * @param domNode the DOM node that triggered the event
1040      * @param type the event type
1041      * @param character the character associated with the event
1042      * @param shiftKey {@code true} if SHIFT is pressed
1043      * @param ctrlKey {@code true} if CTRL is pressed
1044      * @param altKey {@code true} if ALT is pressed
1045      */
1046     public KeyboardEvent(final DomNode domNode, final String type, final char character,
1047             final boolean shiftKey, final boolean ctrlKey, final boolean altKey) {
1048         this(domNode, type, character, shiftKey, ctrlKey, altKey, false);
1049     }
1050 
1051     /**
1052      * Creates a new keyboard event instance for a character key.
1053      *
1054      * @param domNode the DOM node that triggered the event
1055      * @param type the event type
1056      * @param character the character associated with the event
1057      * @param shiftKey {@code true} if SHIFT is pressed
1058      * @param ctrlKey {@code true} if CTRL is pressed
1059      * @param altKey {@code true} if ALT is pressed
1060      * @param metaKey {@code true} if META is pressed
1061      */
1062     public KeyboardEvent(final DomNode domNode, final String type, final char character,
1063             final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final boolean metaKey) {
1064         super(domNode, type);
1065 
1066         setShiftKey(shiftKey);
1067         setCtrlKey(ctrlKey);
1068         setAltKey(altKey);
1069         setMetaKey(metaKey);
1070 
1071         if ('\n' == character) {
1072             setKeyCode(DOM_VK_RETURN);
1073             charCode_ = DOM_VK_RETURN;
1074             which_ = DOM_VK_RETURN;
1075             key_ = "Enter";
1076             code_ = "Enter";
1077             return;
1078         }
1079 
1080         final int keyCode;
1081         if (Event.TYPE_KEY_PRESS.equals(getType())) {
1082             keyCode = Integer.valueOf(character);
1083         }
1084         else {
1085             keyCode = Integer.valueOf(charToKeyCode(character, hasOemKeyCodes(domNode)));
1086         }
1087         setKeyCode(keyCode);
1088         if (Event.TYPE_KEY_PRESS.equals(getType())) {
1089             charCode_ = character;
1090         }
1091         which_ = charCode_ == 0 ? keyCode : Integer.valueOf(charCode_);
1092 
1093         key_ = determineKey(character, shiftKey);
1094         code_ = determineCode(character);
1095     }
1096 
1097     /**
1098      * Creates a new keyboard event instance for a key code.
1099      *
1100      * @param domNode the DOM node that triggered the event
1101      * @param type the event type
1102      * @param keyCode the key code associated with the event
1103      * @param shiftKey {@code true} if SHIFT is pressed
1104      * @param ctrlKey {@code true} if CTRL is pressed
1105      * @param altKey {@code true} if ALT is pressed
1106      */
1107     public KeyboardEvent(final DomNode domNode, final String type, final int keyCode,
1108             final boolean shiftKey, final boolean ctrlKey, final boolean altKey) {
1109         this(domNode, type, keyCode, -1, shiftKey, ctrlKey, altKey, false);
1110     }
1111 
1112     /**
1113      * Creates a new keyboard event instance for a key code.
1114      *
1115      * @param domNode the DOM node that triggered the event
1116      * @param type the event type
1117      * @param keyCode the key code associated with the event
1118      * @param location the location of the key (e.g. {@link #DOM_KEY_LOCATION_RIGHT} for the right
1119      *        shift key) or a negative value for the default location of the key code
1120      * @param shiftKey {@code true} if SHIFT is pressed
1121      * @param ctrlKey {@code true} if CTRL is pressed
1122      * @param altKey {@code true} if ALT is pressed
1123      * @param metaKey {@code true} if META is pressed
1124      */
1125     public KeyboardEvent(final DomNode domNode, final String type, final int keyCode, final int location,
1126             final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final boolean metaKey) {
1127         super(domNode, type);
1128 
1129         if (isAmbiguousKeyCode(keyCode)) {
1130             throw new IllegalArgumentException("Please use the 'char' constructor instead of int");
1131         }
1132 
1133         // all supported browsers report the windows key code for the meta key
1134         final int browserKeyCode = keyCode == DOM_VK_META ? DOM_VK_WIN : keyCode;
1135         final int charCode = getCharCode(keyCode);
1136         if (Event.TYPE_KEY_PRESS.equals(getType())) {
1137             // the keypress event of a character key reports the character code
1138             setKeyCode(charCode == 0 ? browserKeyCode : charCode);
1139             charCode_ = charCode;
1140             which_ = charCode;
1141         }
1142         else {
1143             setKeyCode(browserKeyCode);
1144             which_ = browserKeyCode;
1145         }
1146         setShiftKey(shiftKey);
1147         setCtrlKey(ctrlKey);
1148         setAltKey(altKey);
1149 
1150         final KeyInfo info = KEY_CODE_INFO_MAP.get(keyCode);
1151         if (info == null) {
1152             key_ = "Unidentified";
1153             code_ = "";
1154         }
1155         else {
1156             key_ = shiftKey ? info.shiftedKey() : info.key();
1157             code_ = info.code();
1158             location_ = info.location();
1159         }
1160         setMetaKey(metaKey);
1161 
1162         if (location >= 0 && location != location_) {
1163             location_ = location;
1164             // e.g. ShiftLeft -> ShiftRight
1165             if (location == DOM_KEY_LOCATION_RIGHT && code_.endsWith("Left")) {
1166                 code_ = code_.substring(0, code_.length() - "Left".length()) + "Right";
1167             }
1168             else if (location == DOM_KEY_LOCATION_LEFT && code_.endsWith("Right")) {
1169                 code_ = code_.substring(0, code_.length() - "Right".length()) + "Left";
1170             }
1171         }
1172     }
1173 
1174     /**
1175      * Returns whether the specified character requires the {@code SHIFT} key to be pressed.
1176      *
1177      * @param ch the character to check
1178      * @param shiftKey whether the shift key is currently pressed
1179      * @return {@code true} if the character requires the {@code SHIFT} key
1180      */
1181     public static boolean isShiftNeeded(final char ch, final boolean shiftKey) {
1182         return "~!@#$%^&*()_+{}:\"<>?|".indexOf(ch) != -1
1183                 || (!shiftKey && ch >= 'A' && ch <= 'Z');
1184     }
1185 
1186     /**
1187      * Returns whether the given key code is ambiguous (i.e. could represent either upper or lower case).
1188      * Key codes in the ranges {@code DOM_VK_0}–{@code DOM_VK_9} and {@code DOM_VK_A}–{@code DOM_VK_Z}
1189      * are considered ambiguous; the character constructor should be used instead.
1190      */
1191     private static boolean isAmbiguousKeyCode(final int keyCode) {
1192         return (keyCode >= DOM_VK_0 && keyCode <= DOM_VK_9) || (keyCode >= DOM_VK_A && keyCode <= DOM_VK_Z);
1193     }
1194 
1195     /**
1196      * Converts a Java character to its corresponding key code.
1197      *
1198      * @param c the character to convert
1199      * @param oemKeyCodes whether to use the key codes of Chrome/Edge for the ';', '=' and '-' keys
1200      * @return the corresponding key code
1201      * @see <a href="https://www.w3.org/TR/DOM-Level-3-Events/#dom-keyboard-event-key-algorithm">DOM 3 Events</a>
1202      */
1203     private static int charToKeyCode(final char c, final boolean oemKeyCodes) {
1204         if (c >= 'a' && c <= 'z') {
1205             return 'A' + c - 'a';
1206         }
1207 
1208         if (oemKeyCodes) {
1209             final int oemKeyCode = switch (c) {
1210                 case ';', ':' -> OEM_KEY_CODE_SEMICOLON;
1211                 case '=', '+' -> OEM_KEY_CODE_EQUALS;
1212                 case '-', '_' -> OEM_KEY_CODE_HYPHEN_MINUS;
1213                 default -> 0;
1214             };
1215             if (oemKeyCode != 0) {
1216                 return oemKeyCode;
1217             }
1218         }
1219 
1220         final Integer i = KEX_CODE_MAP.get(c);
1221         if (i != null) {
1222             return i;
1223         }
1224         return c;
1225     }
1226 
1227     /**
1228      * Returns whether the browser of the page of the given node uses the key codes
1229      * 186, 187 and 189 for the ';', '=' and '-' keys.
1230      *
1231      * @param domNode the node the event is fired for
1232      * @return whether to use these key codes
1233      */
1234     private static boolean hasOemKeyCodes(final DomNode domNode) {
1235         if (domNode == null) {
1236             return false;
1237         }
1238         final Page page = domNode.getPage();
1239         return page != null
1240                 && page.getEnclosingWindow().getWebClient().getBrowserVersion()
1241                         .hasFeature(KEYBOARD_EVENT_OEM_KEY_CODES);
1242     }
1243 
1244     /**
1245      * Returns the code of the character produced by the key with the given key code
1246      * (e.g. the numpad keys); this is the {@code charCode} of the keypress event.
1247      *
1248      * @param keyCode the key code
1249      * @return the character code or {@code 0} if the key does not produce a character
1250      */
1251     public static int getCharCode(final int keyCode) {
1252         if (keyCode == DOM_VK_RETURN) {
1253             return DOM_VK_RETURN;
1254         }
1255         final KeyInfo info = KEY_CODE_INFO_MAP.get(keyCode);
1256         if (info != null && info.key().length() == 1) {
1257             return info.key().charAt(0);
1258         }
1259         return 0;
1260     }
1261 
1262     /**
1263      * Returns whether the key with the given key code produces a character; only
1264      * these keys fire a {@code keypress} event.
1265      *
1266      * @param keyCode the key code
1267      * @return {@code true} if the key produces a character
1268      */
1269     public static boolean isCharacterKey(final int keyCode) {
1270         return getCharCode(keyCode) != 0;
1271     }
1272 
1273     /**
1274      * Determines the value of the {@code key} property for a typed character.
1275      *
1276      * @param character the typed character
1277      * @param shiftKey whether the shift key is pressed
1278      * @return the key value
1279      */
1280     private static String determineKey(final char character, final boolean shiftKey) {
1281         return switch (character) {
1282             case '\n', '\r' -> "Enter";
1283             case '\t' -> "Tab";
1284             case '\b' -> "Backspace";
1285             default -> {
1286                 if (shiftKey && character >= 'a' && character <= 'z') {
1287                     yield String.valueOf(Character.toUpperCase(character));
1288                 }
1289                 yield String.valueOf(character);
1290             }
1291         };
1292     }
1293 
1294     /**
1295      * Determines the value of the {@code code} property (the physical key on a US keyboard)
1296      * for a typed character.
1297      *
1298      * @param character the typed character
1299      * @return the code value or an empty string if there is no key for this character
1300      */
1301     private static String determineCode(final char character) {
1302         if (character >= 'a' && character <= 'z') {
1303             return "Key" + Character.toUpperCase(character);
1304         }
1305         if (character >= 'A' && character <= 'Z') {
1306             return "Key" + character;
1307         }
1308 
1309         final String code = CHAR_CODE_MAP.get(character);
1310         if (code == null) {
1311             return "";
1312         }
1313         return code;
1314     }
1315 
1316     /**
1317      * Creates an instance of this event.
1318      *
1319      * @param type the event type
1320      * @param details the event details (optional)
1321      */
1322     @JsxConstructor
1323     @Override
1324     public void jsConstructor(final String type, final ScriptableObject details) {
1325         super.jsConstructor(type, details);
1326 
1327         if (details != null && !JavaScriptEngine.isUndefined(details)) {
1328 
1329             final Object key = details.get("key", details);
1330             if (!isMissingOrUndefined(key)) {
1331                 setKey(JavaScriptEngine.toString(key));
1332             }
1333 
1334             final Object code = details.get("code", details);
1335             if (!isMissingOrUndefined(code)) {
1336                 setCode(JavaScriptEngine.toString(code));
1337             }
1338 
1339             final Object location = details.get("location", details);
1340             if (!isMissingOrUndefined(location)) {
1341                 setLocation(JavaScriptEngine.toInt32(location));
1342             }
1343 
1344             final Object ctrlKey = details.get("ctrlKey", details);
1345             if (!isMissingOrUndefined(ctrlKey)) {
1346                 setCtrlKey(JavaScriptEngine.toBoolean(ctrlKey));
1347             }
1348 
1349             final Object shiftKey = details.get("shiftKey", details);
1350             if (!isMissingOrUndefined(shiftKey)) {
1351                 setShiftKey(JavaScriptEngine.toBoolean(shiftKey));
1352             }
1353 
1354             final Object altKey = details.get("altKey", details);
1355             if (!isMissingOrUndefined(altKey)) {
1356                 setAltKey(JavaScriptEngine.toBoolean(altKey));
1357             }
1358 
1359             final Object metaKey = details.get("metaKey", details);
1360             if (!isMissingOrUndefined(metaKey)) {
1361                 setMetaKey(JavaScriptEngine.toBoolean(metaKey));
1362             }
1363 
1364             final Object repeat = details.get("repeat", details);
1365             if (!isMissingOrUndefined(repeat)) {
1366                 setRepeat(JavaScriptEngine.toBoolean(repeat));
1367             }
1368 
1369             final Object isComposing = details.get("isComposing", details);
1370             if (!isMissingOrUndefined(isComposing)) {
1371                 setIsComposing(JavaScriptEngine.toBoolean(isComposing));
1372             }
1373 
1374             final Object charCode = details.get("charCode", details);
1375             if (!isMissingOrUndefined(charCode)) {
1376                 setCharCode(JavaScriptEngine.toInt32(charCode));
1377             }
1378 
1379             final Object keyCode = details.get("keyCode", details);
1380             if (!isMissingOrUndefined(keyCode)) {
1381                 setKeyCode(JavaScriptEngine.toInt32(keyCode));
1382             }
1383 
1384             if (getBrowserVersion().hasFeature(JS_EVENT_KEYBOARD_CTOR_WHICH)) {
1385                 final Object which = details.get("which", details);
1386                 if (!isMissingOrUndefined(which)) {
1387                     setWhich(JavaScriptEngine.toInt32(which));
1388                 }
1389             }
1390         }
1391     }
1392 
1393     /**
1394      * Initializes the keyboard event using the DOM Level 3 Event specification.
1395      *
1396      * @param type the event type
1397      * @param bubbles whether the event can bubble
1398      * @param cancelable whether the event can be canceled
1399      * @param view the view to use for this event
1400      * @param key the value of the {@code key} attribute; defaults to {@code ""}
1401      * @param location the value of the {@code location} attribute; defaults to {@code 0}
1402      * @param ctrlKey whether the Control key is pressed
1403      * @param altKey whether the Alt key is pressed
1404      * @param shiftKey whether the Shift key is pressed
1405      * @param metaKey whether the Meta key is pressed
1406      */
1407     @JsxFunction
1408     public void initKeyboardEvent(
1409             final String type,
1410             final boolean bubbles,
1411             final boolean cancelable,
1412             final Object view,
1413             final String key,
1414             final int location,
1415             final boolean ctrlKey,
1416             final boolean altKey,
1417             final boolean shiftKey,
1418             final boolean metaKey) {
1419 
1420         initUIEvent(type, bubbles, cancelable, view, 0);
1421         setKey(key);
1422         setLocation(location);
1423         setCtrlKey(ctrlKey);
1424         setAltKey(altKey);
1425         setShiftKey(shiftKey);
1426         setKeyCode(0);
1427         setMetaKey(metaKey);
1428         charCode_ = 0;
1429     }
1430 
1431     /**
1432      * Returns the char code associated with the event.
1433      *
1434      * @return the char code associated with the event
1435      */
1436     @JsxGetter
1437     public int getCharCode() {
1438         return charCode_;
1439     }
1440 
1441     /**
1442      * Sets the char code associated with the event.
1443      *
1444      * @param charCode the char code associated with the event
1445      */
1446     protected void setCharCode(final int charCode) {
1447         charCode_ = charCode;
1448     }
1449 
1450     /**
1451      * Returns the numeric key code of the key pressed, or the char code for an alphanumeric key pressed.
1452      *
1453      * @return the numeric key code or char code
1454      */
1455     @Override
1456     public int getWhich() {
1457         return which_;
1458     }
1459 
1460     /**
1461      * Sets the numeric key code of the key pressed, or the char code for an alphanumeric key pressed.
1462      *
1463      * @param which the key code or char code
1464      */
1465     protected void setWhich(final int which) {
1466         which_ = which;
1467     }
1468 
1469     /**
1470      * {@inheritDoc} Overridden to modify browser configurations.
1471      */
1472     @Override
1473     @JsxGetter
1474     public int getKeyCode() {
1475         return super.getKeyCode();
1476     }
1477 
1478     /**
1479      * {@inheritDoc}
1480      */
1481     @Override
1482     @JsxGetter
1483     public boolean isShiftKey() {
1484         return super.isShiftKey();
1485     }
1486 
1487     /**
1488      * {@inheritDoc}
1489      */
1490     @Override
1491     @JsxGetter
1492     public boolean isCtrlKey() {
1493         return super.isCtrlKey();
1494     }
1495 
1496     /**
1497      * {@inheritDoc}
1498      */
1499     @Override
1500     @JsxGetter
1501     public boolean isAltKey() {
1502         return super.isAltKey();
1503     }
1504 
1505     /**
1506      * Returns the value of the key pressed by the user.
1507      *
1508      * @return the value of the key pressed
1509      */
1510     @JsxGetter
1511     public String getKey() {
1512         return key_;
1513     }
1514 
1515     /**
1516      * Sets the value of the key pressed by the user.
1517      *
1518      * @param key the key value
1519      */
1520     protected void setKey(final String key) {
1521         key_ = key;
1522     }
1523 
1524     /**
1525      * Returns the physical key identifier on the keyboard.
1526      *
1527      * @return the physical key identifier
1528      */
1529     @JsxGetter
1530     public String getCode() {
1531         return code_;
1532     }
1533 
1534     /**
1535      * Sets the physical key identifier on the keyboard.
1536      *
1537      * @param code the physical key identifier
1538      */
1539     protected void setCode(final String code) {
1540         code_ = code;
1541     }
1542 
1543     /**
1544      * Returns whether the {@code meta} key was pressed during the event.
1545      *
1546      * @return {@code true} if the {@code meta} key was pressed
1547      */
1548     @JsxGetter
1549     public boolean isMetaKey() {
1550         return metaKey_;
1551     }
1552 
1553     /**
1554      * Sets whether the {@code meta} key was pressed during the event.
1555      *
1556      * @param metaKey {@code true} if the {@code meta} key was pressed
1557      */
1558     protected void setMetaKey(final boolean metaKey) {
1559         metaKey_ = metaKey;
1560     }
1561 
1562     /**
1563      * Returns the current state of the specified modifier key.
1564      *
1565      * @param context the JavaScript context
1566      * @param scope the scope
1567      * @param thisObj the scriptable
1568      * @param args the arguments passed into the method; the key value of the modifier
1569      *        (e.g. {@code Shift} or {@code Control})
1570      * @param function the function
1571      * @return {@code true} if the modifier is active
1572      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState">MDN</a>
1573      */
1574     @JsxFunction
1575     public static boolean getModifierState(final Context context, final VarScope scope,
1576             final Scriptable thisObj, final Object[] args, final Function function) {
1577         if (!(thisObj instanceof KeyboardEvent event)) {
1578             throw JavaScriptEngine.typeError("Illegal invocation");
1579         }
1580         return getModifierState("KeyboardEvent", args,
1581                 event.isShiftKey(), event.isCtrlKey(), event.isAltKey(), event.isMetaKey());
1582     }
1583 
1584     /**
1585      * Returns the location of the key on the keyboard.
1586      *
1587      * @return the key location
1588      */
1589     @JsxGetter
1590     public int getLocation() {
1591         return location_;
1592     }
1593 
1594     /**
1595      * Sets the location of the key on the keyboard.
1596      *
1597      * @param location the key location
1598      */
1599     protected void setLocation(final int location) {
1600         location_ = location;
1601     }
1602 
1603     /**
1604      * Returns whether the key is being held down causing auto-repeat.
1605      *
1606      * @return {@code true} if the key is being held down
1607      */
1608     @JsxGetter
1609     public boolean isRepeat() {
1610         return repeat_;
1611     }
1612 
1613     /**
1614      * Sets whether the key is being held down causing auto-repeat.
1615      *
1616      * @param repeat {@code true} if the key is being held down
1617      */
1618     protected void setRepeat(final boolean repeat) {
1619         repeat_ = repeat;
1620     }
1621 
1622     /**
1623      * Returns whether this event is fired after the {@code compositionstart} and before the {@code compositionend} events.
1624      *
1625      * @return {@code true} if the event is fired while composing
1626      */
1627     @JsxGetter
1628     public boolean getIsComposing() {
1629         return isComposing_;
1630     }
1631 
1632     /**
1633      * Sets whether this event is fired after the {@code compositionstart} and before the {@code compositionend} events.
1634      *
1635      * @param isComposing {@code true} if the event is fired while composing
1636      */
1637     protected void setIsComposing(final boolean isComposing) {
1638         isComposing_ = isComposing;
1639     }
1640 }