Home | History | Annotate | Download | only in mac
      1 /*
      2  * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      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.
     12  *
     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.
     24  */
     25 
     26 #import "config.h"
     27 #import "PlatformKeyboardEvent.h"
     28 
     29 #import "Logging.h"
     30 #import <Carbon/Carbon.h>
     31 #import <wtf/ASCIICType.h>
     32 
     33 using namespace WTF;
     34 
     35 namespace WebCore {
     36 
     37 static String keyIdentifierForKeyEvent(NSEvent* event)
     38 {
     39     if ([event type] == NSFlagsChanged)
     40         switch ([event keyCode]) {
     41             case 54: // Right Command
     42             case 55: // Left Command
     43                 return "Meta";
     44 
     45             case 57: // Capslock
     46                 return "CapsLock";
     47 
     48             case 56: // Left Shift
     49             case 60: // Right Shift
     50                 return "Shift";
     51 
     52             case 58: // Left Alt
     53             case 61: // Right Alt
     54                 return "Alt";
     55 
     56             case 59: // Left Ctrl
     57             case 62: // Right Ctrl
     58                 return "Control";
     59 
     60             default:
     61                 ASSERT_NOT_REACHED();
     62                 return "";
     63         }
     64 
     65     NSString *s = [event charactersIgnoringModifiers];
     66     if ([s length] != 1) {
     67         LOG(Events, "received an unexpected number of characters in key event: %u", [s length]);
     68         return "Unidentified";
     69     }
     70     unichar c = [s characterAtIndex:0];
     71     switch (c) {
     72         // Each identifier listed in the DOM spec is listed here.
     73         // Many are simply commented out since they do not appear on standard Macintosh keyboards
     74         // or are on a key that doesn't have a corresponding character.
     75 
     76         // "Accept"
     77         // "AllCandidates"
     78 
     79         // "Alt"
     80         case NSMenuFunctionKey:
     81             return "Alt";
     82 
     83         // "Apps"
     84         // "BrowserBack"
     85         // "BrowserForward"
     86         // "BrowserHome"
     87         // "BrowserRefresh"
     88         // "BrowserSearch"
     89         // "BrowserStop"
     90         // "CapsLock"
     91 
     92         // "Clear"
     93         case NSClearLineFunctionKey:
     94             return "Clear";
     95 
     96         // "CodeInput"
     97         // "Compose"
     98         // "Control"
     99         // "Crsel"
    100         // "Convert"
    101         // "Copy"
    102         // "Cut"
    103 
    104         // "Down"
    105         case NSDownArrowFunctionKey:
    106             return "Down";
    107         // "End"
    108         case NSEndFunctionKey:
    109             return "End";
    110         // "Enter"
    111         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
    112             return "Enter";
    113 
    114         // "EraseEof"
    115 
    116         // "Execute"
    117         case NSExecuteFunctionKey:
    118             return "Execute";
    119 
    120         // "Exsel"
    121 
    122         // "F1"
    123         case NSF1FunctionKey:
    124             return "F1";
    125         // "F2"
    126         case NSF2FunctionKey:
    127             return "F2";
    128         // "F3"
    129         case NSF3FunctionKey:
    130             return "F3";
    131         // "F4"
    132         case NSF4FunctionKey:
    133             return "F4";
    134         // "F5"
    135         case NSF5FunctionKey:
    136             return "F5";
    137         // "F6"
    138         case NSF6FunctionKey:
    139             return "F6";
    140         // "F7"
    141         case NSF7FunctionKey:
    142             return "F7";
    143         // "F8"
    144         case NSF8FunctionKey:
    145             return "F8";
    146         // "F9"
    147         case NSF9FunctionKey:
    148             return "F9";
    149         // "F10"
    150         case NSF10FunctionKey:
    151             return "F10";
    152         // "F11"
    153         case NSF11FunctionKey:
    154             return "F11";
    155         // "F12"
    156         case NSF12FunctionKey:
    157             return "F12";
    158         // "F13"
    159         case NSF13FunctionKey:
    160             return "F13";
    161         // "F14"
    162         case NSF14FunctionKey:
    163             return "F14";
    164         // "F15"
    165         case NSF15FunctionKey:
    166             return "F15";
    167         // "F16"
    168         case NSF16FunctionKey:
    169             return "F16";
    170         // "F17"
    171         case NSF17FunctionKey:
    172             return "F17";
    173         // "F18"
    174         case NSF18FunctionKey:
    175             return "F18";
    176         // "F19"
    177         case NSF19FunctionKey:
    178             return "F19";
    179         // "F20"
    180         case NSF20FunctionKey:
    181             return "F20";
    182         // "F21"
    183         case NSF21FunctionKey:
    184             return "F21";
    185         // "F22"
    186         case NSF22FunctionKey:
    187             return "F22";
    188         // "F23"
    189         case NSF23FunctionKey:
    190             return "F23";
    191         // "F24"
    192         case NSF24FunctionKey:
    193             return "F24";
    194 
    195         // "FinalMode"
    196 
    197         // "Find"
    198         case NSFindFunctionKey:
    199             return "Find";
    200 
    201         // "FullWidth"
    202         // "HalfWidth"
    203         // "HangulMode"
    204         // "HanjaMode"
    205 
    206         // "Help"
    207         case NSHelpFunctionKey:
    208             return "Help";
    209 
    210         // "Hiragana"
    211 
    212         // "Home"
    213         case NSHomeFunctionKey:
    214             return "Home";
    215         // "Insert"
    216         case NSInsertFunctionKey:
    217             return "Insert";
    218 
    219         // "JapaneseHiragana"
    220         // "JapaneseKatakana"
    221         // "JapaneseRomaji"
    222         // "JunjaMode"
    223         // "KanaMode"
    224         // "KanjiMode"
    225         // "Katakana"
    226         // "LaunchApplication1"
    227         // "LaunchApplication2"
    228         // "LaunchMail"
    229 
    230         // "Left"
    231         case NSLeftArrowFunctionKey:
    232             return "Left";
    233 
    234         // "Meta"
    235         // "MediaNextTrack"
    236         // "MediaPlayPause"
    237         // "MediaPreviousTrack"
    238         // "MediaStop"
    239 
    240         // "ModeChange"
    241         case NSModeSwitchFunctionKey:
    242             return "ModeChange";
    243 
    244         // "Nonconvert"
    245         // "NumLock"
    246 
    247         // "PageDown"
    248         case NSPageDownFunctionKey:
    249             return "PageDown";
    250         // "PageUp"
    251         case NSPageUpFunctionKey:
    252             return "PageUp";
    253 
    254         // "Paste"
    255 
    256         // "Pause"
    257         case NSPauseFunctionKey:
    258             return "Pause";
    259 
    260         // "Play"
    261         // "PreviousCandidate"
    262 
    263         // "PrintScreen"
    264         case NSPrintScreenFunctionKey:
    265             return "PrintScreen";
    266 
    267         // "Process"
    268         // "Props"
    269 
    270         // "Right"
    271         case NSRightArrowFunctionKey:
    272             return "Right";
    273 
    274         // "RomanCharacters"
    275 
    276         // "Scroll"
    277         case NSScrollLockFunctionKey:
    278             return "Scroll";
    279         // "Select"
    280         case NSSelectFunctionKey:
    281             return "Select";
    282 
    283         // "SelectMedia"
    284         // "Shift"
    285 
    286         // "Stop"
    287         case NSStopFunctionKey:
    288             return "Stop";
    289         // "Up"
    290         case NSUpArrowFunctionKey:
    291             return "Up";
    292         // "Undo"
    293         case NSUndoFunctionKey:
    294             return "Undo";
    295 
    296         // "VolumeDown"
    297         // "VolumeMute"
    298         // "VolumeUp"
    299         // "Win"
    300         // "Zoom"
    301 
    302         // More function keys, not in the key identifier specification.
    303         case NSF25FunctionKey:
    304             return "F25";
    305         case NSF26FunctionKey:
    306             return "F26";
    307         case NSF27FunctionKey:
    308             return "F27";
    309         case NSF28FunctionKey:
    310             return "F28";
    311         case NSF29FunctionKey:
    312             return "F29";
    313         case NSF30FunctionKey:
    314             return "F30";
    315         case NSF31FunctionKey:
    316             return "F31";
    317         case NSF32FunctionKey:
    318             return "F32";
    319         case NSF33FunctionKey:
    320             return "F33";
    321         case NSF34FunctionKey:
    322             return "F34";
    323         case NSF35FunctionKey:
    324             return "F35";
    325 
    326         // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
    327         case 0x7F:
    328             return "U+0008";
    329         // Standard says that DEL becomes U+007F.
    330         case NSDeleteFunctionKey:
    331             return "U+007F";
    332 
    333         // Always use 0x09 for tab instead of AppKit's backtab character.
    334         case NSBackTabCharacter:
    335             return "U+0009";
    336 
    337         case NSBeginFunctionKey:
    338         case NSBreakFunctionKey:
    339         case NSClearDisplayFunctionKey:
    340         case NSDeleteCharFunctionKey:
    341         case NSDeleteLineFunctionKey:
    342         case NSInsertCharFunctionKey:
    343         case NSInsertLineFunctionKey:
    344         case NSNextFunctionKey:
    345         case NSPrevFunctionKey:
    346         case NSPrintFunctionKey:
    347         case NSRedoFunctionKey:
    348         case NSResetFunctionKey:
    349         case NSSysReqFunctionKey:
    350         case NSSystemFunctionKey:
    351         case NSUserFunctionKey:
    352             // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
    353             // For now, just fall through to the default.
    354         default:
    355             return String::format("U+%04X", toASCIIUpper(c));
    356     }
    357 }
    358 
    359 static bool isKeypadEvent(NSEvent* event)
    360 {
    361     // Check that this is the type of event that has a keyCode.
    362     switch ([event type]) {
    363         case NSKeyDown:
    364         case NSKeyUp:
    365         case NSFlagsChanged:
    366             break;
    367         default:
    368             return false;
    369     }
    370 
    371     if ([event modifierFlags] & NSNumericPadKeyMask)
    372         return true;
    373 
    374     switch ([event keyCode]) {
    375         case 71: // Clear
    376         case 81: // =
    377         case 75: // /
    378         case 67: // *
    379         case 78: // -
    380         case 69: // +
    381         case 76: // Enter
    382         case 65: // .
    383         case 82: // 0
    384         case 83: // 1
    385         case 84: // 2
    386         case 85: // 3
    387         case 86: // 4
    388         case 87: // 5
    389         case 88: // 6
    390         case 89: // 7
    391         case 91: // 8
    392         case 92: // 9
    393             return true;
    394      }
    395 
    396      return false;
    397 }
    398 
    399 static int windowsKeyCodeForKeyEvent(NSEvent* event)
    400 {
    401     switch ([event keyCode]) {
    402         // VK_TAB (09) TAB key
    403         case 48: return 0x09;
    404 
    405         // VK_APPS (5D) Right windows/meta key
    406         case 54: // Right Command
    407             return 0x5D;
    408 
    409         // VK_LWIN (5B) Left windows/meta key
    410         case 55: // Left Command
    411             return 0x5B;
    412 
    413         // VK_CAPITAL (14) caps locks key
    414         case 57: // Capslock
    415             return 0x14;
    416 
    417         // VK_SHIFT (10) either shift key
    418         case 56: // Left Shift
    419         case 60: // Right Shift
    420             return 0x10;
    421 
    422         // VK_MENU (12) either alt key
    423         case 58: // Left Alt
    424         case 61: // Right Alt
    425             return 0x12;
    426 
    427         // VK_CONTROL (11) either ctrl key
    428         case 59: // Left Ctrl
    429         case 62: // Right Ctrl
    430             return 0x11;
    431 
    432         // VK_CLEAR (0C) CLEAR key
    433         case 71: return 0x0C;
    434 
    435         // VK_NUMPAD0 (60) Numeric keypad 0 key
    436         case 82: return 0x60;
    437         // VK_NUMPAD1 (61) Numeric keypad 1 key
    438         case 83: return 0x61;
    439         // VK_NUMPAD2 (62) Numeric keypad 2 key
    440         case 84: return 0x62;
    441         // VK_NUMPAD3 (63) Numeric keypad 3 key
    442         case 85: return 0x63;
    443         // VK_NUMPAD4 (64) Numeric keypad 4 key
    444         case 86: return 0x64;
    445         // VK_NUMPAD5 (65) Numeric keypad 5 key
    446         case 87: return 0x65;
    447         // VK_NUMPAD6 (66) Numeric keypad 6 key
    448         case 88: return 0x66;
    449         // VK_NUMPAD7 (67) Numeric keypad 7 key
    450         case 89: return 0x67;
    451         // VK_NUMPAD8 (68) Numeric keypad 8 key
    452         case 91: return 0x68;
    453         // VK_NUMPAD9 (69) Numeric keypad 9 key
    454         case 92: return 0x69;
    455         // VK_MULTIPLY (6A) Multiply key
    456         case 67: return 0x6A;
    457         // VK_ADD (6B) Add key
    458         case 69: return 0x6B;
    459 
    460         // VK_SUBTRACT (6D) Subtract key
    461         case 78: return 0x6D;
    462         // VK_DECIMAL (6E) Decimal key
    463         case 65: return 0x6E;
    464         // VK_DIVIDE (6F) Divide key
    465         case 75: return 0x6F;
    466      }
    467 
    468     NSString* s = [event charactersIgnoringModifiers];
    469     if ([s length] != 1)
    470         return 0;
    471 
    472     switch ([s characterAtIndex:0]) {
    473         // VK_LBUTTON (01) Left mouse button
    474         // VK_RBUTTON (02) Right mouse button
    475         // VK_CANCEL (03) Control-break processing
    476         // VK_MBUTTON (04) Middle mouse button (three-button mouse)
    477         // VK_XBUTTON1 (05)
    478         // VK_XBUTTON2 (06)
    479 
    480         // VK_BACK (08) BACKSPACE key
    481         case 8: case 0x7F: return 0x08;
    482         // VK_TAB (09) TAB key
    483         case 9: return 0x09;
    484 
    485         // VK_CLEAR (0C) CLEAR key
    486         // handled by key code above
    487 
    488         // VK_RETURN (0D)
    489         case 0xD: case 3: return 0x0D;
    490 
    491         // VK_SHIFT (10) SHIFT key
    492         // VK_CONTROL (11) CTRL key
    493         // VK_MENU (12) ALT key
    494 
    495         // VK_PAUSE (13) PAUSE key
    496         case NSPauseFunctionKey: return 0x13;
    497 
    498         // VK_CAPITAL (14) CAPS LOCK key
    499         // VK_KANA (15) Input Method Editor (IME) Kana mode
    500         // VK_HANGUEL (15) IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
    501         // VK_HANGUL (15) IME Hangul mode
    502         // VK_JUNJA (17) IME Junja mode
    503         // VK_FINAL (18) IME final mode
    504         // VK_HANJA (19) IME Hanja mode
    505         // VK_KANJI (19) IME Kanji mode
    506 
    507         // VK_ESCAPE (1B) ESC key
    508         case 0x1B: return 0x1B;
    509 
    510         // VK_CONVERT (1C) IME convert
    511         // VK_NONCONVERT (1D) IME nonconvert
    512         // VK_ACCEPT (1E) IME accept
    513         // VK_MODECHANGE (1F) IME mode change request
    514 
    515         // VK_SPACE (20) SPACEBAR
    516         case ' ': return 0x20;
    517         // VK_PRIOR (21) PAGE UP key
    518         case NSPageUpFunctionKey: return 0x21;
    519         // VK_NEXT (22) PAGE DOWN key
    520         case NSPageDownFunctionKey: return 0x22;
    521         // VK_END (23) END key
    522         case NSEndFunctionKey: return 0x23;
    523         // VK_HOME (24) HOME key
    524         case NSHomeFunctionKey: return 0x24;
    525         // VK_LEFT (25) LEFT ARROW key
    526         case NSLeftArrowFunctionKey: return 0x25;
    527         // VK_UP (26) UP ARROW key
    528         case NSUpArrowFunctionKey: return 0x26;
    529         // VK_RIGHT (27) RIGHT ARROW key
    530         case NSRightArrowFunctionKey: return 0x27;
    531         // VK_DOWN (28) DOWN ARROW key
    532         case NSDownArrowFunctionKey: return 0x28;
    533         // VK_SELECT (29) SELECT key
    534         case NSSelectFunctionKey: return 0x29;
    535         // VK_PRINT (2A) PRINT key
    536         case NSPrintFunctionKey: return 0x2A;
    537         // VK_EXECUTE (2B) EXECUTE key
    538         case NSExecuteFunctionKey: return 0x2B;
    539         // VK_SNAPSHOT (2C) PRINT SCREEN key
    540         case NSPrintScreenFunctionKey: return 0x2C;
    541         // VK_INSERT (2D) INS key
    542         case NSInsertFunctionKey: case NSHelpFunctionKey: return 0x2D;
    543         // VK_DELETE (2E) DEL key
    544         case NSDeleteFunctionKey: return 0x2E;
    545 
    546         // VK_HELP (2F) HELP key
    547 
    548         //  (30) 0 key
    549         case '0': case ')': return 0x30;
    550         //  (31) 1 key
    551         case '1': case '!': return 0x31;
    552         //  (32) 2 key
    553         case '2': case '@': return 0x32;
    554         //  (33) 3 key
    555         case '3': case '#': return 0x33;
    556         //  (34) 4 key
    557         case '4': case '$': return 0x34;
    558         //  (35) 5 key
    559         case '5': case '%': return 0x35;
    560         //  (36) 6 key
    561         case '6': case '^': return 0x36;
    562         //  (37) 7 key
    563         case '7': case '&': return 0x37;
    564         //  (38) 8 key
    565         case '8': case '*': return 0x38;
    566         //  (39) 9 key
    567         case '9': case '(': return 0x39;
    568         //  (41) A key
    569         case 'a': case 'A': return 0x41;
    570         //  (42) B key
    571         case 'b': case 'B': return 0x42;
    572         //  (43) C key
    573         case 'c': case 'C': return 0x43;
    574         //  (44) D key
    575         case 'd': case 'D': return 0x44;
    576         //  (45) E key
    577         case 'e': case 'E': return 0x45;
    578         //  (46) F key
    579         case 'f': case 'F': return 0x46;
    580         //  (47) G key
    581         case 'g': case 'G': return 0x47;
    582         //  (48) H key
    583         case 'h': case 'H': return 0x48;
    584         //  (49) I key
    585         case 'i': case 'I': return 0x49;
    586         //  (4A) J key
    587         case 'j': case 'J': return 0x4A;
    588         //  (4B) K key
    589         case 'k': case 'K': return 0x4B;
    590         //  (4C) L key
    591         case 'l': case 'L': return 0x4C;
    592         //  (4D) M key
    593         case 'm': case 'M': return 0x4D;
    594         //  (4E) N key
    595         case 'n': case 'N': return 0x4E;
    596         //  (4F) O key
    597         case 'o': case 'O': return 0x4F;
    598         //  (50) P key
    599         case 'p': case 'P': return 0x50;
    600         //  (51) Q key
    601         case 'q': case 'Q': return 0x51;
    602         //  (52) R key
    603         case 'r': case 'R': return 0x52;
    604         //  (53) S key
    605         case 's': case 'S': return 0x53;
    606         //  (54) T key
    607         case 't': case 'T': return 0x54;
    608         //  (55) U key
    609         case 'u': case 'U': return 0x55;
    610         //  (56) V key
    611         case 'v': case 'V': return 0x56;
    612         //  (57) W key
    613         case 'w': case 'W': return 0x57;
    614         //  (58) X key
    615         case 'x': case 'X': return 0x58;
    616         //  (59) Y key
    617         case 'y': case 'Y': return 0x59;
    618         //  (5A) Z key
    619         case 'z': case 'Z': return 0x5A;
    620 
    621         // VK_LWIN (5B) Left Windows key (Microsoft Natural keyboard)
    622         // VK_RWIN (5C) Right Windows key (Natural keyboard)
    623         // VK_APPS (5D) Applications key (Natural keyboard)
    624         // VK_SLEEP (5F) Computer Sleep key
    625 
    626         // VK_NUMPAD0 (60) Numeric keypad 0 key
    627         // VK_NUMPAD1 (61) Numeric keypad 1 key
    628         // VK_NUMPAD2 (62) Numeric keypad 2 key
    629         // VK_NUMPAD3 (63) Numeric keypad 3 key
    630         // VK_NUMPAD4 (64) Numeric keypad 4 key
    631         // VK_NUMPAD5 (65) Numeric keypad 5 key
    632         // VK_NUMPAD6 (66) Numeric keypad 6 key
    633         // VK_NUMPAD7 (67) Numeric keypad 7 key
    634         // VK_NUMPAD8 (68) Numeric keypad 8 key
    635         // VK_NUMPAD9 (69) Numeric keypad 9 key
    636         // VK_MULTIPLY (6A) Multiply key
    637         // VK_ADD (6B) Add key
    638         // handled by key code above
    639 
    640         // VK_SEPARATOR (6C) Separator key
    641 
    642         // VK_SUBTRACT (6D) Subtract key
    643         // VK_DECIMAL (6E) Decimal key
    644         // VK_DIVIDE (6F) Divide key
    645         // handled by key code above
    646 
    647         // VK_F1 (70) F1 key
    648         case NSF1FunctionKey: return 0x70;
    649         // VK_F2 (71) F2 key
    650         case NSF2FunctionKey: return 0x71;
    651         // VK_F3 (72) F3 key
    652         case NSF3FunctionKey: return 0x72;
    653         // VK_F4 (73) F4 key
    654         case NSF4FunctionKey: return 0x73;
    655         // VK_F5 (74) F5 key
    656         case NSF5FunctionKey: return 0x74;
    657         // VK_F6 (75) F6 key
    658         case NSF6FunctionKey: return 0x75;
    659         // VK_F7 (76) F7 key
    660         case NSF7FunctionKey: return 0x76;
    661         // VK_F8 (77) F8 key
    662         case NSF8FunctionKey: return 0x77;
    663         // VK_F9 (78) F9 key
    664         case NSF9FunctionKey: return 0x78;
    665         // VK_F10 (79) F10 key
    666         case NSF10FunctionKey: return 0x79;
    667         // VK_F11 (7A) F11 key
    668         case NSF11FunctionKey: return 0x7A;
    669         // VK_F12 (7B) F12 key
    670         case NSF12FunctionKey: return 0x7B;
    671         // VK_F13 (7C) F13 key
    672         case NSF13FunctionKey: return 0x7C;
    673         // VK_F14 (7D) F14 key
    674         case NSF14FunctionKey: return 0x7D;
    675         // VK_F15 (7E) F15 key
    676         case NSF15FunctionKey: return 0x7E;
    677         // VK_F16 (7F) F16 key
    678         case NSF16FunctionKey: return 0x7F;
    679         // VK_F17 (80H) F17 key
    680         case NSF17FunctionKey: return 0x80;
    681         // VK_F18 (81H) F18 key
    682         case NSF18FunctionKey: return 0x81;
    683         // VK_F19 (82H) F19 key
    684         case NSF19FunctionKey: return 0x82;
    685         // VK_F20 (83H) F20 key
    686         case NSF20FunctionKey: return 0x83;
    687         // VK_F21 (84H) F21 key
    688         case NSF21FunctionKey: return 0x84;
    689         // VK_F22 (85H) F22 key
    690         case NSF22FunctionKey: return 0x85;
    691         // VK_F23 (86H) F23 key
    692         case NSF23FunctionKey: return 0x86;
    693         // VK_F24 (87H) F24 key
    694         case NSF24FunctionKey: return 0x87;
    695 
    696         // VK_NUMLOCK (90) NUM LOCK key
    697 
    698         // VK_SCROLL (91) SCROLL LOCK key
    699         case NSScrollLockFunctionKey: return 0x91;
    700 
    701         // VK_LSHIFT (A0) Left SHIFT key
    702         // VK_RSHIFT (A1) Right SHIFT key
    703         // VK_LCONTROL (A2) Left CONTROL key
    704         // VK_RCONTROL (A3) Right CONTROL key
    705         // VK_LMENU (A4) Left MENU key
    706         // VK_RMENU (A5) Right MENU key
    707         // VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key
    708         // VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key
    709         // VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key
    710         // VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key
    711         // VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key
    712         // VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key
    713         // VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key
    714         // VK_VOLUME_MUTE (AD) Windows 2000/XP: Volume Mute key
    715         // VK_VOLUME_DOWN (AE) Windows 2000/XP: Volume Down key
    716         // VK_VOLUME_UP (AF) Windows 2000/XP: Volume Up key
    717         // VK_MEDIA_NEXT_TRACK (B0) Windows 2000/XP: Next Track key
    718         // VK_MEDIA_PREV_TRACK (B1) Windows 2000/XP: Previous Track key
    719         // VK_MEDIA_STOP (B2) Windows 2000/XP: Stop Media key
    720         // VK_MEDIA_PLAY_PAUSE (B3) Windows 2000/XP: Play/Pause Media key
    721         // VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key
    722         // VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key
    723         // VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key
    724         // VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key
    725 
    726         // VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key
    727         case ';': case ':': return 0xBA;
    728         // VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key
    729         case '=': case '+': return 0xBB;
    730         // VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key
    731         case ',': case '<': return 0xBC;
    732         // VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key
    733         case '-': case '_': return 0xBD;
    734         // VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key
    735         case '.': case '>': return 0xBE;
    736         // VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key
    737         case '/': case '?': return 0xBF;
    738         // VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key
    739         case '`': case '~': return 0xC0;
    740         // VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key
    741         case '[': case '{': return 0xDB;
    742         // VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '\|' key
    743         case '\\': case '|': return 0xDC;
    744         // VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key
    745         case ']': case '}': return 0xDD;
    746         // 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
    747         case '\'': case '"': return 0xDE;
    748 
    749         // VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard.
    750         // VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
    751         // VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
    752         // 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
    753         // VK_ATTN (F6) Attn key
    754         // VK_CRSEL (F7) CrSel key
    755         // VK_EXSEL (F8) ExSel key
    756         // VK_EREOF (F9) Erase EOF key
    757         // VK_PLAY (FA) Play key
    758         // VK_ZOOM (FB) Zoom key
    759         // VK_NONAME (FC) Reserved for future use
    760         // VK_PA1 (FD) PA1 key
    761         // VK_OEM_CLEAR (FE) Clear key
    762     }
    763 
    764     return 0;
    765 }
    766 
    767 static inline bool isKeyUpEvent(NSEvent *event)
    768 {
    769     if ([event type] != NSFlagsChanged)
    770         return [event type] == NSKeyUp;
    771     // FIXME: This logic fails if the user presses both Shift keys at once, for example:
    772     // we treat releasing one of them as keyDown.
    773     switch ([event keyCode]) {
    774         case 54: // Right Command
    775         case 55: // Left Command
    776             return ([event modifierFlags] & NSCommandKeyMask) == 0;
    777 
    778         case 57: // Capslock
    779             return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
    780 
    781         case 56: // Left Shift
    782         case 60: // Right Shift
    783             return ([event modifierFlags] & NSShiftKeyMask) == 0;
    784 
    785         case 58: // Left Alt
    786         case 61: // Right Alt
    787             return ([event modifierFlags] & NSAlternateKeyMask) == 0;
    788 
    789         case 59: // Left Ctrl
    790         case 62: // Right Ctrl
    791             return ([event modifierFlags] & NSControlKeyMask) == 0;
    792 
    793         case 63: // Function
    794             return ([event modifierFlags] & NSFunctionKeyMask) == 0;
    795     }
    796     return false;
    797 }
    798 
    799 static inline String textFromEvent(NSEvent* event)
    800 {
    801     if ([event type] == NSFlagsChanged)
    802         return "";
    803     return [event characters];
    804 }
    805 
    806 
    807 static inline String unmodifiedTextFromEvent(NSEvent* event)
    808 {
    809     if ([event type] == NSFlagsChanged)
    810         return "";
    811     return [event charactersIgnoringModifiers];
    812 }
    813 
    814 PlatformKeyboardEvent::PlatformKeyboardEvent()
    815     : m_type(KeyDown)
    816     , m_autoRepeat(false)
    817     , m_windowsVirtualKeyCode(0)
    818     , m_nativeVirtualKeyCode(0)
    819     , m_isKeypad(false)
    820     , m_shiftKey(false)
    821     , m_ctrlKey(false)
    822     , m_altKey(false)
    823     , m_metaKey(false)
    824 {
    825 }
    826 
    827 PlatformKeyboardEvent::PlatformKeyboardEvent(NSEvent *event)
    828     : m_type(isKeyUpEvent(event) ? PlatformKeyboardEvent::KeyUp : PlatformKeyboardEvent::KeyDown)
    829     , m_text(textFromEvent(event))
    830     , m_unmodifiedText(unmodifiedTextFromEvent(event))
    831     , m_keyIdentifier(keyIdentifierForKeyEvent(event))
    832     , m_autoRepeat(([event type] != NSFlagsChanged) && [event isARepeat])
    833     , m_windowsVirtualKeyCode(windowsKeyCodeForKeyEvent(event))
    834     , m_nativeVirtualKeyCode([event keyCode])
    835     , m_isKeypad(isKeypadEvent(event))
    836     , m_shiftKey([event modifierFlags] & NSShiftKeyMask)
    837     , m_ctrlKey([event modifierFlags] & NSControlKeyMask)
    838     , m_altKey([event modifierFlags] & NSAlternateKeyMask)
    839     , m_metaKey([event modifierFlags] & NSCommandKeyMask)
    840     , m_macEvent(event)
    841 {
    842     // Always use 13 for Enter/Return -- we don't want to use AppKit's different character for Enter.
    843     if (m_windowsVirtualKeyCode == '\r') {
    844         m_text = "\r";
    845         m_unmodifiedText = "\r";
    846     }
    847 
    848     // The adjustments below are only needed in backward compatibility mode, but we cannot tell what mode we are in from here.
    849 
    850     // Turn 0x7F into 8, because backspace needs to always be 8.
    851     if (m_text == "\x7F")
    852         m_text = "\x8";
    853     if (m_unmodifiedText == "\x7F")
    854         m_unmodifiedText = "\x8";
    855     // Always use 9 for tab -- we don't want to use AppKit's different character for shift-tab.
    856     if (m_windowsVirtualKeyCode == 9) {
    857         m_text = "\x9";
    858         m_unmodifiedText = "\x9";
    859     }
    860 }
    861 
    862 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
    863 {
    864     // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
    865     ASSERT(m_type == KeyDown);
    866     ASSERT(type == RawKeyDown || type == Char);
    867     m_type = type;
    868     if (backwardCompatibilityMode)
    869         return;
    870 
    871     if (type == RawKeyDown) {
    872         m_text = String();
    873         m_unmodifiedText = String();
    874     } else {
    875         m_keyIdentifier = String();
    876         m_windowsVirtualKeyCode = 0;
    877         if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
    878             // According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
    879             // happen to be in this range, e.g. the Apple logo (Option+Shift+K).
    880             // 0xF7FF is an arbitrary cut-off.
    881             m_text = String();
    882             m_unmodifiedText = String();
    883         }
    884     }
    885 }
    886 
    887 bool PlatformKeyboardEvent::currentCapsLockState()
    888 {
    889     return GetCurrentKeyModifiers() & alphaLock;
    890 }
    891 
    892 }
    893