2 * Copyright (C) 2004, 2006 Apple Computer, 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 "PlatformKeyboardEvent.h"
33 static String keyIdentifierForKeyEvent(NSEvent* event)
35 NSString *s = [event charactersIgnoringModifiers];
36 if ([s length] != 1) {
37 LOG(Events, "received an unexpected number of characters in key event: %u", [s length]);
38 return "Unidentified";
40 unichar c = [s characterAtIndex:0];
42 // Each identifier listed in the DOM spec is listed here.
43 // Many are simply commented out since they do not appear on standard Macintosh keyboards
44 // or are on a key that doesn't have a corresponding character.
50 case NSMenuFunctionKey:
63 case NSClearLineFunctionKey:
75 case NSDownArrowFunctionKey:
78 case NSEndFunctionKey:
81 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
87 case NSExecuteFunctionKey:
102 case NSF4FunctionKey:
105 case NSF5FunctionKey:
108 case NSF6FunctionKey:
111 case NSF7FunctionKey:
114 case NSF8FunctionKey:
117 case NSF9FunctionKey:
120 case NSF10FunctionKey:
123 case NSF11FunctionKey:
126 case NSF12FunctionKey:
129 case NSF13FunctionKey:
132 case NSF14FunctionKey:
135 case NSF15FunctionKey:
138 case NSF16FunctionKey:
141 case NSF17FunctionKey:
144 case NSF18FunctionKey:
147 case NSF19FunctionKey:
150 case NSF20FunctionKey:
153 case NSF21FunctionKey:
156 case NSF22FunctionKey:
159 case NSF23FunctionKey:
162 case NSF24FunctionKey:
168 case NSFindFunctionKey:
177 case NSHelpFunctionKey:
183 case NSHomeFunctionKey:
186 case NSInsertFunctionKey:
189 // "JapaneseHiragana"
190 // "JapaneseKatakana"
196 // "LaunchApplication1"
197 // "LaunchApplication2"
201 case NSLeftArrowFunctionKey:
207 // "MediaPreviousTrack"
211 case NSModeSwitchFunctionKey:
218 case NSPageDownFunctionKey:
221 case NSPageUpFunctionKey:
227 case NSPauseFunctionKey:
231 // "PreviousCandidate"
234 case NSPrintScreenFunctionKey:
235 return "PrintScreen";
241 case NSRightArrowFunctionKey:
247 case NSScrollLockFunctionKey:
250 case NSSelectFunctionKey:
257 case NSStopFunctionKey:
260 case NSUpArrowFunctionKey:
263 case NSUndoFunctionKey:
272 // More function keys, not in the key identifier specification.
273 case NSF25FunctionKey:
275 case NSF26FunctionKey:
277 case NSF27FunctionKey:
279 case NSF28FunctionKey:
281 case NSF29FunctionKey:
283 case NSF30FunctionKey:
285 case NSF31FunctionKey:
287 case NSF32FunctionKey:
289 case NSF33FunctionKey:
291 case NSF34FunctionKey:
293 case NSF35FunctionKey:
296 // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
299 // Standard says that DEL becomes U+00007F.
300 case NSDeleteFunctionKey:
303 case NSBeginFunctionKey:
304 case NSBreakFunctionKey:
305 case NSClearDisplayFunctionKey:
306 case NSDeleteCharFunctionKey:
307 case NSDeleteLineFunctionKey:
308 case NSInsertCharFunctionKey:
309 case NSInsertLineFunctionKey:
310 case NSNextFunctionKey:
311 case NSPrevFunctionKey:
312 case NSPrintFunctionKey:
313 case NSRedoFunctionKey:
314 case NSResetFunctionKey:
315 case NSSysReqFunctionKey:
316 case NSSystemFunctionKey:
317 case NSUserFunctionKey:
318 // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
319 // For now, just fall through to the default.
321 return String::sprintf("U+%06X", toupper(c));
325 static bool isKeypadEvent(NSEvent* event)
327 // Check that this is the type of event that has a keyCode.
328 switch ([event type]) {
337 switch ([event keyCode]) {
362 static int WindowsKeyCodeForKeyEvent(NSEvent* event)
364 switch ([event keyCode]) {
365 // VK_TAB (09) TAB key
366 case 48: return 0x09;
368 // VK_CLEAR (0C) CLEAR key
369 case 71: return 0x0C;
371 // VK_NUMPAD0 (60) Numeric keypad 0 key
372 case 82: return 0x60;
373 // VK_NUMPAD1 (61) Numeric keypad 1 key
374 case 83: return 0x61;
375 // VK_NUMPAD2 (62) Numeric keypad 2 key
376 case 84: return 0x62;
377 // VK_NUMPAD3 (63) Numeric keypad 3 key
378 case 85: return 0x63;
379 // VK_NUMPAD4 (64) Numeric keypad 4 key
380 case 86: return 0x64;
381 // VK_NUMPAD5 (65) Numeric keypad 5 key
382 case 87: return 0x65;
383 // VK_NUMPAD6 (66) Numeric keypad 6 key
384 case 88: return 0x66;
385 // VK_NUMPAD7 (67) Numeric keypad 7 key
386 case 89: return 0x67;
387 // VK_NUMPAD8 (68) Numeric keypad 8 key
388 case 91: return 0x68;
389 // VK_NUMPAD9 (69) Numeric keypad 9 key
390 case 92: return 0x69;
391 // VK_MULTIPLY (6A) Multiply key
392 case 67: return 0x6A;
393 // VK_ADD (6B) Add key
394 case 69: return 0x6B;
396 // VK_SUBTRACT (6D) Subtract key
397 case 78: return 0x6D;
398 // VK_DECIMAL (6E) Decimal key
399 case 65: return 0x6E;
400 // VK_DIVIDE (6F) Divide key
401 case 75: return 0x6F;
404 NSString* s = [event charactersIgnoringModifiers];
408 switch ([s characterAtIndex:0]) {
409 // VK_LBUTTON (01) Left mouse button
410 // VK_RBUTTON (02) Right mouse button
411 // VK_CANCEL (03) Control-break processing
412 // VK_MBUTTON (04) Middle mouse button (three-button mouse)
416 // VK_BACK (08) BACKSPACE key
417 case 8: case 0x7F: return 0x08;
418 // VK_TAB (09) TAB key
421 // VK_CLEAR (0C) CLEAR key
422 // handled by key code above
425 case 0xD: case 3: return 0x0D;
427 // VK_SHIFT (10) SHIFT key
428 // VK_CONTROL (11) CTRL key
429 // VK_MENU (12) ALT key
431 // VK_PAUSE (13) PAUSE key
432 case NSPauseFunctionKey: return 0x13;
434 // VK_CAPITAL (14) CAPS LOCK key
435 // VK_KANA (15) Input Method Editor (IME) Kana mode
436 // VK_HANGUEL (15) IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
437 // VK_HANGUL (15) IME Hangul mode
438 // VK_JUNJA (17) IME Junja mode
439 // VK_FINAL (18) IME final mode
440 // VK_HANJA (19) IME Hanja mode
441 // VK_KANJI (19) IME Kanji mode
443 // VK_ESCAPE (1B) ESC key
444 case 0x1B: return 0x1B;
446 // VK_CONVERT (1C) IME convert
447 // VK_NONCONVERT (1D) IME nonconvert
448 // VK_ACCEPT (1E) IME accept
449 // VK_MODECHANGE (1F) IME mode change request
451 // VK_SPACE (20) SPACEBAR
452 case ' ': return 0x20;
453 // VK_PRIOR (21) PAGE UP key
454 case NSPageUpFunctionKey: return 0x21;
455 // VK_NEXT (22) PAGE DOWN key
456 case NSPageDownFunctionKey: return 0x22;
457 // VK_END (23) END key
458 case NSEndFunctionKey: return 0x23;
459 // VK_HOME (24) HOME key
460 case NSHomeFunctionKey: return 0x24;
461 // VK_LEFT (25) LEFT ARROW key
462 case NSLeftArrowFunctionKey: return 0x25;
463 // VK_UP (26) UP ARROW key
464 case NSUpArrowFunctionKey: return 0x26;
465 // VK_RIGHT (27) RIGHT ARROW key
466 case NSRightArrowFunctionKey: return 0x27;
467 // VK_DOWN (28) DOWN ARROW key
468 case NSDownArrowFunctionKey: return 0x28;
469 // VK_SELECT (29) SELECT key
470 case NSSelectFunctionKey: return 0x29;
471 // VK_PRINT (2A) PRINT key
472 case NSPrintFunctionKey: return 0x2A;
473 // VK_EXECUTE (2B) EXECUTE key
474 case NSExecuteFunctionKey: return 0x2B;
475 // VK_SNAPSHOT (2C) PRINT SCREEN key
476 case NSPrintScreenFunctionKey: return 0x2C;
477 // VK_INSERT (2D) INS key
478 case NSInsertFunctionKey: case NSHelpFunctionKey: return 0x2D;
479 // VK_DELETE (2E) DEL key
480 case NSDeleteFunctionKey: return 0x2E;
482 // VK_HELP (2F) HELP key
485 case '0': case ')': return 0x30;
487 case '1': case '!': return 0x31;
489 case '2': case '@': return 0x32;
491 case '3': case '#': return 0x33;
493 case '4': case '$': return 0x34;
495 case '5': case '%': return 0x35;
497 case '6': case '^': return 0x36;
499 case '7': case '&': return 0x37;
501 case '8': case '*': return 0x38;
503 case '9': case '(': return 0x39;
505 case 'a': case 'A': return 0x41;
507 case 'b': case 'B': return 0x42;
509 case 'c': case 'C': return 0x43;
511 case 'd': case 'D': return 0x44;
513 case 'e': case 'E': return 0x45;
515 case 'f': case 'F': return 0x46;
517 case 'g': case 'G': return 0x47;
519 case 'h': case 'H': return 0x48;
521 case 'i': case 'I': return 0x49;
523 case 'j': case 'J': return 0x4A;
525 case 'k': case 'K': return 0x4B;
527 case 'l': case 'L': return 0x4C;
529 case 'm': case 'M': return 0x4D;
531 case 'n': case 'N': return 0x4E;
533 case 'o': case 'O': return 0x4F;
535 case 'p': case 'P': return 0x50;
537 case 'q': case 'Q': return 0x51;
539 case 'r': case 'R': return 0x52;
541 case 's': case 'S': return 0x53;
543 case 't': case 'T': return 0x54;
545 case 'u': case 'U': return 0x55;
547 case 'v': case 'V': return 0x56;
549 case 'w': case 'W': return 0x57;
551 case 'x': case 'X': return 0x58;
553 case 'y': case 'Y': return 0x59;
555 case 'z': case 'Z': return 0x5A;
557 // VK_LWIN (5B) Left Windows key (Microsoft Natural keyboard)
558 // VK_RWIN (5C) Right Windows key (Natural keyboard)
559 // VK_APPS (5D) Applications key (Natural keyboard)
560 // VK_SLEEP (5F) Computer Sleep key
562 // VK_NUMPAD0 (60) Numeric keypad 0 key
563 // VK_NUMPAD1 (61) Numeric keypad 1 key
564 // VK_NUMPAD2 (62) Numeric keypad 2 key
565 // VK_NUMPAD3 (63) Numeric keypad 3 key
566 // VK_NUMPAD4 (64) Numeric keypad 4 key
567 // VK_NUMPAD5 (65) Numeric keypad 5 key
568 // VK_NUMPAD6 (66) Numeric keypad 6 key
569 // VK_NUMPAD7 (67) Numeric keypad 7 key
570 // VK_NUMPAD8 (68) Numeric keypad 8 key
571 // VK_NUMPAD9 (69) Numeric keypad 9 key
572 // VK_MULTIPLY (6A) Multiply key
573 // VK_ADD (6B) Add key
574 // handled by key code above
576 // VK_SEPARATOR (6C) Separator key
578 // VK_SUBTRACT (6D) Subtract key
579 // VK_DECIMAL (6E) Decimal key
580 // VK_DIVIDE (6F) Divide key
581 // handled by key code above
584 case NSF1FunctionKey: return 0x70;
586 case NSF2FunctionKey: return 0x71;
588 case NSF3FunctionKey: return 0x72;
590 case NSF4FunctionKey: return 0x73;
592 case NSF5FunctionKey: return 0x74;
594 case NSF6FunctionKey: return 0x75;
596 case NSF7FunctionKey: return 0x76;
598 case NSF8FunctionKey: return 0x77;
600 case NSF9FunctionKey: return 0x78;
601 // VK_F10 (79) F10 key
602 case NSF10FunctionKey: return 0x79;
603 // VK_F11 (7A) F11 key
604 case NSF11FunctionKey: return 0x7A;
605 // VK_F12 (7B) F12 key
606 case NSF12FunctionKey: return 0x7B;
607 // VK_F13 (7C) F13 key
608 case NSF13FunctionKey: return 0x7C;
609 // VK_F14 (7D) F14 key
610 case NSF14FunctionKey: return 0x7D;
611 // VK_F15 (7E) F15 key
612 case NSF15FunctionKey: return 0x7E;
613 // VK_F16 (7F) F16 key
614 case NSF16FunctionKey: return 0x7F;
615 // VK_F17 (80H) F17 key
616 case NSF17FunctionKey: return 0x80;
617 // VK_F18 (81H) F18 key
618 case NSF18FunctionKey: return 0x81;
619 // VK_F19 (82H) F19 key
620 case NSF19FunctionKey: return 0x82;
621 // VK_F20 (83H) F20 key
622 case NSF20FunctionKey: return 0x83;
623 // VK_F21 (84H) F21 key
624 case NSF21FunctionKey: return 0x84;
625 // VK_F22 (85H) F22 key
626 case NSF22FunctionKey: return 0x85;
627 // VK_F23 (86H) F23 key
628 case NSF23FunctionKey: return 0x86;
629 // VK_F24 (87H) F24 key
630 case NSF24FunctionKey: return 0x87;
632 // VK_NUMLOCK (90) NUM LOCK key
634 // VK_SCROLL (91) SCROLL LOCK key
635 case NSScrollLockFunctionKey: return 0x91;
637 // VK_LSHIFT (A0) Left SHIFT key
638 // VK_RSHIFT (A1) Right SHIFT key
639 // VK_LCONTROL (A2) Left CONTROL key
640 // VK_RCONTROL (A3) Right CONTROL key
641 // VK_LMENU (A4) Left MENU key
642 // VK_RMENU (A5) Right MENU key
643 // VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key
644 // VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key
645 // VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key
646 // VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key
647 // VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key
648 // VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key
649 // VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key
650 // VK_VOLUME_MUTE (AD) Windows 2000/XP: Volume Mute key
651 // VK_VOLUME_DOWN (AE) Windows 2000/XP: Volume Down key
652 // VK_VOLUME_UP (AF) Windows 2000/XP: Volume Up key
653 // VK_MEDIA_NEXT_TRACK (B0) Windows 2000/XP: Next Track key
654 // VK_MEDIA_PREV_TRACK (B1) Windows 2000/XP: Previous Track key
655 // VK_MEDIA_STOP (B2) Windows 2000/XP: Stop Media key
656 // VK_MEDIA_PLAY_PAUSE (B3) Windows 2000/XP: Play/Pause Media key
657 // VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key
658 // VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key
659 // VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key
660 // VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key
662 // VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key
663 case ';': case ':': return 0xBA;
664 // VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key
665 case '=': case '+': return 0xBB;
666 // VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key
667 case ',': case '<': return 0xBC;
668 // VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key
669 case '-': case '_': return 0xBD;
670 // VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key
671 case '.': case '>': return 0xBE;
672 // VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key
673 case '/': case '?': return 0xBF;
674 // VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key
675 case '`': case '~': return 0xC0;
676 // VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key
677 case '[': case '{': return 0xDB;
678 // VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '\|' key
679 case '\\': case '|': return 0xDC;
680 // VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key
681 case ']': case '}': return 0xDD;
682 // VK_OEM_7 (DE) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the 'single-quote/double-quote' key
683 case '\'': case '"': return 0xDE;
685 // VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard.
686 // VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
687 // VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
688 // VK_PACKET (E7) Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT,SendInput, WM_KEYDOWN, and WM_KEYUP
689 // VK_ATTN (F6) Attn key
690 // VK_CRSEL (F7) CrSel key
691 // VK_EXSEL (F8) ExSel key
692 // VK_EREOF (F9) Erase EOF key
693 // VK_PLAY (FA) Play key
694 // VK_ZOOM (FB) Zoom key
695 // VK_NONAME (FC) Reserved for future use
696 // VK_PA1 (FD) PA1 key
697 // VK_OEM_CLEAR (FE) Clear key
703 PlatformKeyboardEvent::PlatformKeyboardEvent(NSEvent *event, bool forceAutoRepeat)
704 : m_text([event characters]),
705 m_unmodifiedText([event charactersIgnoringModifiers]),
706 m_keyIdentifier(keyIdentifierForKeyEvent(event)),
707 m_isKeyUp([event type] == NSKeyUp),
708 m_autoRepeat(forceAutoRepeat || [event isARepeat]),
709 m_WindowsKeyCode(WindowsKeyCodeForKeyEvent(event)),
710 m_isKeypad(isKeypadEvent(event)),
711 m_shiftKey([event modifierFlags] & NSShiftKeyMask),
712 m_ctrlKey([event modifierFlags] & NSControlKeyMask),
713 m_altKey([event modifierFlags] & NSAlternateKeyMask),
714 m_metaKey([event modifierFlags] & NSCommandKeyMask)
716 // Turn 0x7F into 8, because backspace needs to always be 8.
717 if (m_text == "\x7F")
719 if (m_unmodifiedText == "\x7F")
720 m_unmodifiedText = "\x8";
721 // Always use 9 for tab -- we don't want to use AppKit's different character for shift-tab.
722 if (m_WindowsKeyCode == 9) {
724 m_unmodifiedText = "\x9";