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 COMPUTER, 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 COMPUTER, 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 "PlatformString.h"
31 #import "WindowsKeyboardCodes.h"
32 #import <wtf/ASCIICType.h>
35 #import "KeyEventCodesIPhone.h"
42 String keyIdentifierForCharCode(unichar charCode)
45 // Each identifier listed in the DOM spec is listed here.
46 // Many are simply commented out since they do not appear on standard Macintosh keyboards
47 // or are on a key that doesn't have a corresponding character.
53 case NSMenuFunctionKey:
66 case NSClearLineFunctionKey:
78 case NSDownArrowFunctionKey:
81 case NSEndFunctionKey:
84 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
90 case NSExecuteFunctionKey:
102 case NSF3FunctionKey:
105 case NSF4FunctionKey:
108 case NSF5FunctionKey:
111 case NSF6FunctionKey:
114 case NSF7FunctionKey:
117 case NSF8FunctionKey:
120 case NSF9FunctionKey:
123 case NSF10FunctionKey:
126 case NSF11FunctionKey:
129 case NSF12FunctionKey:
132 case NSF13FunctionKey:
135 case NSF14FunctionKey:
138 case NSF15FunctionKey:
141 case NSF16FunctionKey:
144 case NSF17FunctionKey:
147 case NSF18FunctionKey:
150 case NSF19FunctionKey:
153 case NSF20FunctionKey:
156 case NSF21FunctionKey:
159 case NSF22FunctionKey:
162 case NSF23FunctionKey:
165 case NSF24FunctionKey:
171 case NSFindFunctionKey:
180 case NSHelpFunctionKey:
186 case NSHomeFunctionKey:
189 case NSInsertFunctionKey:
192 // "JapaneseHiragana"
193 // "JapaneseKatakana"
199 // "LaunchApplication1"
200 // "LaunchApplication2"
204 case NSLeftArrowFunctionKey:
210 // "MediaPreviousTrack"
214 case NSModeSwitchFunctionKey:
221 case NSPageDownFunctionKey:
224 case NSPageUpFunctionKey:
230 case NSPauseFunctionKey:
234 // "PreviousCandidate"
237 case NSPrintScreenFunctionKey:
238 return "PrintScreen";
244 case NSRightArrowFunctionKey:
250 case NSScrollLockFunctionKey:
253 case NSSelectFunctionKey:
260 case NSStopFunctionKey:
263 case NSUpArrowFunctionKey:
266 case NSUndoFunctionKey:
275 // More function keys, not in the key identifier specification.
276 case NSF25FunctionKey:
278 case NSF26FunctionKey:
280 case NSF27FunctionKey:
282 case NSF28FunctionKey:
284 case NSF29FunctionKey:
286 case NSF30FunctionKey:
288 case NSF31FunctionKey:
290 case NSF32FunctionKey:
292 case NSF33FunctionKey:
294 case NSF34FunctionKey:
296 case NSF35FunctionKey:
299 // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
302 // Standard says that DEL becomes U+007F.
303 case NSDeleteFunctionKey:
306 // Always use 0x09 for tab instead of AppKit's backtab character.
307 case NSBackTabCharacter:
310 case NSBeginFunctionKey:
311 case NSBreakFunctionKey:
312 case NSClearDisplayFunctionKey:
313 case NSDeleteCharFunctionKey:
314 case NSDeleteLineFunctionKey:
315 case NSInsertCharFunctionKey:
316 case NSInsertLineFunctionKey:
317 case NSNextFunctionKey:
318 case NSPrevFunctionKey:
319 case NSPrintFunctionKey:
320 case NSRedoFunctionKey:
321 case NSResetFunctionKey:
322 case NSSysReqFunctionKey:
323 case NSSystemFunctionKey:
324 case NSUserFunctionKey:
325 // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
326 // For now, just fall through to the default.
328 return String::format("U+%04X", toASCIIUpper(charCode));
332 int windowsKeyCodeForKeyCode(uint16_t keyCode)
334 static const int windowsKeyCode[] = {
345 /* 0x0A */ VK_OEM_3, // "Section" - key to the left from 1 (ISO Keyboard Only)
359 /* 0x18 */ VK_OEM_PLUS, // =+
362 /* 0x1B */ VK_OEM_MINUS, // -_
365 /* 0x1E */ VK_OEM_6, // ]}
368 /* 0x21 */ VK_OEM_4, // {[
371 /* 0x24 */ VK_RETURN, // Return
374 /* 0x27 */ VK_OEM_7, // '"
376 /* 0x29 */ VK_OEM_1, // ;:
377 /* 0x2A */ VK_OEM_5, // \|
378 /* 0x2B */ VK_OEM_COMMA, // ,<
379 /* 0x2C */ VK_OEM_2, // /?
382 /* 0x2F */ VK_OEM_PERIOD, // .>
385 /* 0x32 */ VK_OEM_3, // `~
386 /* 0x33 */ VK_BACK, // Backspace
388 /* 0x35 */ VK_ESCAPE,
389 /* 0x36 */ VK_APPS, // Right Command
390 /* 0x37 */ VK_LWIN, // Left Command
391 /* 0x38 */ VK_SHIFT, // Left Shift
392 /* 0x39 */ VK_CAPITAL, // Caps Lock
393 /* 0x3A */ VK_MENU, // Left Option
394 /* 0x3B */ VK_CONTROL, // Left Ctrl
395 /* 0x3C */ VK_SHIFT, // Right Shift
396 /* 0x3D */ VK_MENU, // Right Option
397 /* 0x3E */ VK_CONTROL, // Right Ctrl
400 /* 0x41 */ VK_DECIMAL, // Num Pad .
402 /* 0x43 */ VK_MULTIPLY, // Num Pad *
404 /* 0x45 */ VK_ADD, // Num Pad +
406 /* 0x47 */ VK_CLEAR, // Num Pad Clear
407 /* 0x48 */ VK_VOLUME_UP,
408 /* 0x49 */ VK_VOLUME_DOWN,
409 /* 0x4A */ VK_VOLUME_MUTE,
410 /* 0x4B */ VK_DIVIDE, // Num Pad /
411 /* 0x4C */ VK_RETURN, // Num Pad Enter
413 /* 0x4E */ VK_SUBTRACT, // Num Pad -
416 /* 0x51 */ VK_OEM_PLUS, // Num Pad =. There is no such key on common PC keyboards, mapping to normal "+=".
417 /* 0x52 */ VK_NUMPAD0,
418 /* 0x53 */ VK_NUMPAD1,
419 /* 0x54 */ VK_NUMPAD2,
420 /* 0x55 */ VK_NUMPAD3,
421 /* 0x56 */ VK_NUMPAD4,
422 /* 0x57 */ VK_NUMPAD5,
423 /* 0x58 */ VK_NUMPAD6,
424 /* 0x59 */ VK_NUMPAD7,
426 /* 0x5B */ VK_NUMPAD8,
427 /* 0x5C */ VK_NUMPAD9,
428 /* 0x5D */ 0, // Yen (JIS Keyboard Only)
429 /* 0x5E */ 0, // Underscore (JIS Keyboard Only)
430 /* 0x5F */ 0, // KeypadComma (JIS Keyboard Only)
437 /* 0x66 */ 0, // Eisu (JIS Keyboard Only)
439 /* 0x68 */ 0, // Kana (JIS Keyboard Only)
445 /* 0x6E */ 0, // n/a (Windows95 key?)
449 /* 0x72 */ VK_INSERT, // Help
450 /* 0x73 */ VK_HOME, // Home
451 /* 0x74 */ VK_PRIOR, // Page Up
452 /* 0x75 */ VK_DELETE, // Forward Delete
454 /* 0x77 */ VK_END, // End
456 /* 0x79 */ VK_NEXT, // Page Down
458 /* 0x7B */ VK_LEFT, // Left Arrow
459 /* 0x7C */ VK_RIGHT, // Right Arrow
460 /* 0x7D */ VK_DOWN, // Down Arrow
461 /* 0x7E */ VK_UP, // Up Arrow
468 return windowsKeyCode[keyCode];
471 int windowsKeyCodeForCharCode(unichar charCode)
475 case 8: case 0x7F: return VK_BACK;
476 case 9: return VK_TAB;
477 case 0xD: case 3: return VK_RETURN;
478 case 0x1B: return VK_ESCAPE;
479 case ' ': return VK_SPACE;
480 case NSHomeFunctionKey: return VK_HOME;
481 case NSEndFunctionKey: return VK_END;
482 case NSPageUpFunctionKey: return VK_PRIOR;
483 case NSPageDownFunctionKey: return VK_NEXT;
484 case NSUpArrowFunctionKey: return VK_UP;
485 case NSDownArrowFunctionKey: return VK_DOWN;
486 case NSLeftArrowFunctionKey: return VK_LEFT;
487 case NSRightArrowFunctionKey: return VK_RIGHT;
488 case NSDeleteFunctionKey: return VK_DELETE;
490 case '0': case ')': return VK_0;
491 case '1': case '!': return VK_1;
492 case '2': case '@': return VK_2;
493 case '3': case '#': return VK_3;
494 case '4': case '$': return VK_4;
495 case '5': case '%': return VK_5;
496 case '6': case '^': return VK_6;
497 case '7': case '&': return VK_7;
498 case '8': case '*': return VK_8;
499 case '9': case '(': return VK_9;
501 case 'a': case 'A': return VK_A;
502 case 'b': case 'B': return VK_B;
503 case 'c': case 'C': return VK_C;
504 case 'd': case 'D': return VK_D;
505 case 'e': case 'E': return VK_E;
506 case 'f': case 'F': return VK_F;
507 case 'g': case 'G': return VK_G;
508 case 'h': case 'H': return VK_H;
509 case 'i': case 'I': return VK_I;
510 case 'j': case 'J': return VK_J;
511 case 'k': case 'K': return VK_K;
512 case 'l': case 'L': return VK_L;
513 case 'm': case 'M': return VK_M;
514 case 'n': case 'N': return VK_N;
515 case 'o': case 'O': return VK_O;
516 case 'p': case 'P': return VK_P;
517 case 'q': case 'Q': return VK_Q;
518 case 'r': case 'R': return VK_R;
519 case 's': case 'S': return VK_S;
520 case 't': case 'T': return VK_T;
521 case 'u': case 'U': return VK_U;
522 case 'v': case 'V': return VK_V;
523 case 'w': case 'W': return VK_W;
524 case 'x': case 'X': return VK_X;
525 case 'y': case 'Y': return VK_Y;
526 case 'z': case 'Z': return VK_Z;
528 // AppKit generates Unicode PUA character codes for some function keys; using these when key code is not known.
529 case NSPauseFunctionKey: return VK_PAUSE;
530 case NSSelectFunctionKey: return VK_SELECT;
531 case NSPrintFunctionKey: return VK_PRINT;
532 case NSExecuteFunctionKey: return VK_EXECUTE;
533 case NSPrintScreenFunctionKey: return VK_SNAPSHOT;
534 case NSInsertFunctionKey: return VK_INSERT;
536 case NSHelpFunctionKey: return VK_INSERT;
538 case NSF1FunctionKey: return VK_F1;
539 case NSF2FunctionKey: return VK_F2;
540 case NSF3FunctionKey: return VK_F3;
541 case NSF4FunctionKey: return VK_F4;
542 case NSF5FunctionKey: return VK_F5;
543 case NSF6FunctionKey: return VK_F6;
544 case NSF7FunctionKey: return VK_F7;
545 case NSF8FunctionKey: return VK_F8;
546 case NSF9FunctionKey: return VK_F9;
547 case NSF10FunctionKey: return VK_F10;
548 case NSF11FunctionKey: return VK_F11;
549 case NSF12FunctionKey: return VK_F12;
550 case NSF13FunctionKey: return VK_F13;
551 case NSF14FunctionKey: return VK_F14;
552 case NSF15FunctionKey: return VK_F15;
553 case NSF16FunctionKey: return VK_F16;
554 case NSF17FunctionKey: return VK_F17;
555 case NSF18FunctionKey: return VK_F18;
556 case NSF19FunctionKey: return VK_F19;
557 case NSF20FunctionKey: return VK_F20;
559 case NSF21FunctionKey: return VK_F21;
560 case NSF22FunctionKey: return VK_F22;
561 case NSF23FunctionKey: return VK_F23;
562 case NSF24FunctionKey: return VK_F24;
563 case NSScrollLockFunctionKey: return VK_SCROLL;
565 // This is for U.S. keyboard mapping, and doesn't necessarily make sense for different keyboard layouts.
566 // For example, '"' on Windows Russian layout is VK_2, not VK_OEM_7.
567 case ';': case ':': return VK_OEM_1;
568 case '=': case '+': return VK_OEM_PLUS;
569 case ',': case '<': return VK_OEM_COMMA;
570 case '-': case '_': return VK_OEM_MINUS;
571 case '.': case '>': return VK_OEM_PERIOD;
572 case '/': case '?': return VK_OEM_2;
573 case '`': case '~': return VK_OEM_3;
574 case '[': case '{': return VK_OEM_4;
575 case '\\': case '|': return VK_OEM_5;
576 case ']': case '}': return VK_OEM_6;
577 case '\'': case '"': return VK_OEM_7;