2 * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "KeyEventCocoa.h"
30 #import "WindowsKeyboardCodes.h"
31 #import <wtf/ASCIICType.h>
32 #import <wtf/text/WTFString.h>
35 #import "KeyEventCodesIOS.h"
42 // https://w3c.github.io/uievents-key/
43 String keyForCharCode(unichar charCode)
46 case NSUpArrowFunctionKey:
48 case NSDownArrowFunctionKey:
50 case NSLeftArrowFunctionKey:
52 case NSRightArrowFunctionKey:
53 return "ArrowRight"_s;
72 case NSF10FunctionKey:
74 case NSF11FunctionKey:
76 case NSF12FunctionKey:
78 case NSF13FunctionKey:
80 case NSF14FunctionKey:
82 case NSF15FunctionKey:
84 case NSF16FunctionKey:
86 case NSF17FunctionKey:
88 case NSF18FunctionKey:
90 case NSF19FunctionKey:
92 case NSF20FunctionKey:
94 case NSF21FunctionKey:
96 case NSF22FunctionKey:
98 case NSF23FunctionKey:
100 case NSF24FunctionKey:
102 case NSF25FunctionKey:
104 case NSF26FunctionKey:
106 case NSF27FunctionKey:
108 case NSF28FunctionKey:
110 case NSF29FunctionKey:
112 case NSF30FunctionKey:
114 case NSF31FunctionKey:
116 case NSF32FunctionKey:
118 case NSF33FunctionKey:
120 case NSF34FunctionKey:
122 case NSF35FunctionKey:
124 case NSInsertFunctionKey:
126 case NSDeleteFunctionKey:
128 case NSHomeFunctionKey:
130 case NSEndFunctionKey:
132 case NSPageUpFunctionKey:
134 case NSPageDownFunctionKey:
136 case NSPrintScreenFunctionKey:
137 return "PrintScreen"_s;
138 case NSScrollLockFunctionKey:
139 return "ScrollLock"_s;
140 case NSPauseFunctionKey:
142 case NSMenuFunctionKey:
143 return "ContextMenu"_s;
144 case NSPrintFunctionKey:
146 case NSClearLineFunctionKey:
148 case NSSelectFunctionKey:
150 case NSExecuteFunctionKey:
152 case NSUndoFunctionKey:
154 case NSRedoFunctionKey:
156 case NSFindFunctionKey:
158 case NSHelpFunctionKey:
160 case NSModeSwitchFunctionKey:
161 return "ModeChange"_s;
162 case NSEnterCharacter:
163 case NSNewlineCharacter:
164 case NSCarriageReturnCharacter:
166 case NSDeleteCharacter:
167 case NSBackspaceCharacter:
168 return "Backspace"_s;
169 case NSBackTabCharacter:
174 case NSFormFeedCharacter:
175 case NSParagraphSeparatorCharacter:
176 case NSLineSeparatorCharacter:
177 case NSBeginFunctionKey:
178 case NSSysReqFunctionKey:
179 case NSBreakFunctionKey:
180 case NSResetFunctionKey:
181 case NSStopFunctionKey:
182 case NSUserFunctionKey:
183 case NSSystemFunctionKey:
184 case NSClearDisplayFunctionKey:
185 case NSInsertLineFunctionKey:
186 case NSDeleteLineFunctionKey:
187 case NSInsertCharFunctionKey:
188 case NSDeleteCharFunctionKey:
189 case NSPrevFunctionKey:
190 case NSNextFunctionKey:
191 return "Unidentified"_s;
193 return String(&charCode, 1);
197 String keyIdentifierForCharCode(unichar charCode)
200 // Each identifier listed in the DOM spec is listed here.
201 // Many are simply commented out since they do not appear on standard Macintosh keyboards
202 // or are on a key that doesn't have a corresponding character.
208 case NSMenuFunctionKey:
221 case NSClearLineFunctionKey:
233 case NSDownArrowFunctionKey:
236 case NSEndFunctionKey:
239 case 0x3: case 0xA: case 0xD: // Macintosh calls the one on the main keyboard Return, but Windows calls it Enter, so we'll do the same for the DOM
245 case NSExecuteFunctionKey:
251 case NSF1FunctionKey:
254 case NSF2FunctionKey:
257 case NSF3FunctionKey:
260 case NSF4FunctionKey:
263 case NSF5FunctionKey:
266 case NSF6FunctionKey:
269 case NSF7FunctionKey:
272 case NSF8FunctionKey:
275 case NSF9FunctionKey:
278 case NSF10FunctionKey:
281 case NSF11FunctionKey:
284 case NSF12FunctionKey:
287 case NSF13FunctionKey:
290 case NSF14FunctionKey:
293 case NSF15FunctionKey:
296 case NSF16FunctionKey:
299 case NSF17FunctionKey:
302 case NSF18FunctionKey:
305 case NSF19FunctionKey:
308 case NSF20FunctionKey:
311 case NSF21FunctionKey:
314 case NSF22FunctionKey:
317 case NSF23FunctionKey:
320 case NSF24FunctionKey:
326 case NSFindFunctionKey:
335 case NSHelpFunctionKey:
341 case NSHomeFunctionKey:
344 case NSInsertFunctionKey:
347 // "JapaneseHiragana"
348 // "JapaneseKatakana"
354 // "LaunchApplication1"
355 // "LaunchApplication2"
359 case NSLeftArrowFunctionKey:
365 // "MediaPreviousTrack"
369 case NSModeSwitchFunctionKey:
376 case NSPageDownFunctionKey:
379 case NSPageUpFunctionKey:
385 case NSPauseFunctionKey:
389 // "PreviousCandidate"
392 case NSPrintScreenFunctionKey:
393 return "PrintScreen";
399 case NSRightArrowFunctionKey:
405 case NSScrollLockFunctionKey:
408 case NSSelectFunctionKey:
415 case NSStopFunctionKey:
418 case NSUpArrowFunctionKey:
421 case NSUndoFunctionKey:
430 // More function keys, not in the key identifier specification.
431 case NSF25FunctionKey:
433 case NSF26FunctionKey:
435 case NSF27FunctionKey:
437 case NSF28FunctionKey:
439 case NSF29FunctionKey:
441 case NSF30FunctionKey:
443 case NSF31FunctionKey:
445 case NSF32FunctionKey:
447 case NSF33FunctionKey:
449 case NSF34FunctionKey:
451 case NSF35FunctionKey:
454 // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
457 // Standard says that DEL becomes U+007F.
458 case NSDeleteFunctionKey:
461 // Always use 0x09 for tab instead of AppKit's backtab character.
462 case NSBackTabCharacter:
465 case NSBeginFunctionKey:
466 case NSBreakFunctionKey:
467 case NSClearDisplayFunctionKey:
468 case NSDeleteCharFunctionKey:
469 case NSDeleteLineFunctionKey:
470 case NSInsertCharFunctionKey:
471 case NSInsertLineFunctionKey:
472 case NSNextFunctionKey:
473 case NSPrevFunctionKey:
474 case NSPrintFunctionKey:
475 case NSRedoFunctionKey:
476 case NSResetFunctionKey:
477 case NSSysReqFunctionKey:
478 case NSSystemFunctionKey:
479 case NSUserFunctionKey:
480 // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
481 // For now, just fall through to the default.
483 return String::format("U+%04X", toASCIIUpper(charCode));
487 int windowsKeyCodeForCharCode(unichar charCode)
491 case 8: case 0x7F: return VK_BACK;
492 case 9: return VK_TAB;
493 case 0xD: case 3: return VK_RETURN;
494 case 0x1B: return VK_ESCAPE;
495 case ' ': return VK_SPACE;
496 case NSHomeFunctionKey: return VK_HOME;
497 case NSEndFunctionKey: return VK_END;
498 case NSPageUpFunctionKey: return VK_PRIOR;
499 case NSPageDownFunctionKey: return VK_NEXT;
500 case NSUpArrowFunctionKey: return VK_UP;
501 case NSDownArrowFunctionKey: return VK_DOWN;
502 case NSLeftArrowFunctionKey: return VK_LEFT;
503 case NSRightArrowFunctionKey: return VK_RIGHT;
504 case NSDeleteFunctionKey: return VK_DELETE;
506 case '0': case ')': return VK_0;
507 case '1': case '!': return VK_1;
508 case '2': case '@': return VK_2;
509 case '3': case '#': return VK_3;
510 case '4': case '$': return VK_4;
511 case '5': case '%': return VK_5;
512 case '6': case '^': return VK_6;
513 case '7': case '&': return VK_7;
514 case '8': case '*': return VK_8;
515 case '9': case '(': return VK_9;
517 case 'a': case 'A': return VK_A;
518 case 'b': case 'B': return VK_B;
519 case 'c': case 'C': return VK_C;
520 case 'd': case 'D': return VK_D;
521 case 'e': case 'E': return VK_E;
522 case 'f': case 'F': return VK_F;
523 case 'g': case 'G': return VK_G;
524 case 'h': case 'H': return VK_H;
525 case 'i': case 'I': return VK_I;
526 case 'j': case 'J': return VK_J;
527 case 'k': case 'K': return VK_K;
528 case 'l': case 'L': return VK_L;
529 case 'm': case 'M': return VK_M;
530 case 'n': case 'N': return VK_N;
531 case 'o': case 'O': return VK_O;
532 case 'p': case 'P': return VK_P;
533 case 'q': case 'Q': return VK_Q;
534 case 'r': case 'R': return VK_R;
535 case 's': case 'S': return VK_S;
536 case 't': case 'T': return VK_T;
537 case 'u': case 'U': return VK_U;
538 case 'v': case 'V': return VK_V;
539 case 'w': case 'W': return VK_W;
540 case 'x': case 'X': return VK_X;
541 case 'y': case 'Y': return VK_Y;
542 case 'z': case 'Z': return VK_Z;
544 // AppKit generates Unicode PUA character codes for some function keys; using these when key code is not known.
545 case NSPauseFunctionKey: return VK_PAUSE;
546 case NSSelectFunctionKey: return VK_SELECT;
547 case NSPrintFunctionKey: return VK_PRINT;
548 case NSExecuteFunctionKey: return VK_EXECUTE;
549 case NSPrintScreenFunctionKey: return VK_SNAPSHOT;
551 case NSInsertFunctionKey: case NSHelpFunctionKey: return VK_INSERT;
553 case NSF1FunctionKey: return VK_F1;
554 case NSF2FunctionKey: return VK_F2;
555 case NSF3FunctionKey: return VK_F3;
556 case NSF4FunctionKey: return VK_F4;
557 case NSF5FunctionKey: return VK_F5;
558 case NSF6FunctionKey: return VK_F6;
559 case NSF7FunctionKey: return VK_F7;
560 case NSF8FunctionKey: return VK_F8;
561 case NSF9FunctionKey: return VK_F9;
562 case NSF10FunctionKey: return VK_F10;
563 case NSF11FunctionKey: return VK_F11;
564 case NSF12FunctionKey: return VK_F12;
565 case NSF13FunctionKey: return VK_F13;
566 case NSF14FunctionKey: return VK_F14;
567 case NSF15FunctionKey: return VK_F15;
568 case NSF16FunctionKey: return VK_F16;
569 case NSF17FunctionKey: return VK_F17;
570 case NSF18FunctionKey: return VK_F18;
571 case NSF19FunctionKey: return VK_F19;
572 case NSF20FunctionKey: return VK_F20;
574 case NSInsertFunctionKey: return VK_INSERT;
576 case NSF21FunctionKey: return VK_F21;
577 case NSF22FunctionKey: return VK_F22;
578 case NSF23FunctionKey: return VK_F23;
579 case NSF24FunctionKey: return VK_F24;
580 case NSScrollLockFunctionKey: return VK_SCROLL;
582 // This is for U.S. keyboard mapping, and doesn't necessarily make sense for different keyboard layouts.
583 // For example, '"' on Windows Russian layout is VK_2, not VK_OEM_7.
584 case ';': case ':': return VK_OEM_1;
585 case '=': case '+': return VK_OEM_PLUS;
586 case ',': case '<': return VK_OEM_COMMA;
587 case '-': case '_': return VK_OEM_MINUS;
588 case '.': case '>': return VK_OEM_PERIOD;
589 case '/': case '?': return VK_OEM_2;
590 case '`': case '~': return VK_OEM_3;
591 case '[': case '{': return VK_OEM_4;
592 case '\\': case '|': return VK_OEM_5;
593 case ']': case '}': return VK_OEM_6;
594 case '\'': case '"': return VK_OEM_7;