Home | History | Annotate | Download | only in cocoa
      1 /*
      2  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 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 "core/platform/cocoa/KeyEventCocoa.h"
     28 
     29 #import "core/platform/Logging.h"
     30 #import "core/platform/WindowsKeyboardCodes.h"
     31 #import <wtf/ASCIICType.h>
     32 #import <wtf/text/WTFString.h>
     33 
     34 using namespace WTF;
     35 
     36 namespace WebCore {
     37 
     38 String keyIdentifierForCharCode(unichar charCode)
     39 {
     40     switch (charCode) {
     41         // Each identifier listed in the DOM spec is listed here.
     42         // Many are simply commented out since they do not appear on standard Macintosh keyboards
     43         // or are on a key that doesn't have a corresponding character.
     44 
     45         // "Accept"
     46         // "AllCandidates"
     47 
     48         // "Alt"
     49         case NSMenuFunctionKey:
     50             return "Alt";
     51 
     52         // "Apps"
     53         // "BrowserBack"
     54         // "BrowserForward"
     55         // "BrowserHome"
     56         // "BrowserRefresh"
     57         // "BrowserSearch"
     58         // "BrowserStop"
     59         // "CapsLock"
     60 
     61         // "Clear"
     62         case NSClearLineFunctionKey:
     63             return "Clear";
     64 
     65         // "CodeInput"
     66         // "Compose"
     67         // "Control"
     68         // "Crsel"
     69         // "Convert"
     70         // "Copy"
     71         // "Cut"
     72 
     73         // "Down"
     74         case NSDownArrowFunctionKey:
     75             return "Down";
     76         // "End"
     77         case NSEndFunctionKey:
     78             return "End";
     79         // "Enter"
     80         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
     81             return "Enter";
     82 
     83         // "EraseEof"
     84 
     85         // "Execute"
     86         case NSExecuteFunctionKey:
     87             return "Execute";
     88 
     89         // "Exsel"
     90 
     91         // "F1"
     92         case NSF1FunctionKey:
     93             return "F1";
     94         // "F2"
     95         case NSF2FunctionKey:
     96             return "F2";
     97         // "F3"
     98         case NSF3FunctionKey:
     99             return "F3";
    100         // "F4"
    101         case NSF4FunctionKey:
    102             return "F4";
    103         // "F5"
    104         case NSF5FunctionKey:
    105             return "F5";
    106         // "F6"
    107         case NSF6FunctionKey:
    108             return "F6";
    109         // "F7"
    110         case NSF7FunctionKey:
    111             return "F7";
    112         // "F8"
    113         case NSF8FunctionKey:
    114             return "F8";
    115         // "F9"
    116         case NSF9FunctionKey:
    117             return "F9";
    118         // "F10"
    119         case NSF10FunctionKey:
    120             return "F10";
    121         // "F11"
    122         case NSF11FunctionKey:
    123             return "F11";
    124         // "F12"
    125         case NSF12FunctionKey:
    126             return "F12";
    127         // "F13"
    128         case NSF13FunctionKey:
    129             return "F13";
    130         // "F14"
    131         case NSF14FunctionKey:
    132             return "F14";
    133         // "F15"
    134         case NSF15FunctionKey:
    135             return "F15";
    136         // "F16"
    137         case NSF16FunctionKey:
    138             return "F16";
    139         // "F17"
    140         case NSF17FunctionKey:
    141             return "F17";
    142         // "F18"
    143         case NSF18FunctionKey:
    144             return "F18";
    145         // "F19"
    146         case NSF19FunctionKey:
    147             return "F19";
    148         // "F20"
    149         case NSF20FunctionKey:
    150             return "F20";
    151         // "F21"
    152         case NSF21FunctionKey:
    153             return "F21";
    154         // "F22"
    155         case NSF22FunctionKey:
    156             return "F22";
    157         // "F23"
    158         case NSF23FunctionKey:
    159             return "F23";
    160         // "F24"
    161         case NSF24FunctionKey:
    162             return "F24";
    163 
    164         // "FinalMode"
    165 
    166         // "Find"
    167         case NSFindFunctionKey:
    168             return "Find";
    169 
    170         // "FullWidth"
    171         // "HalfWidth"
    172         // "HangulMode"
    173         // "HanjaMode"
    174 
    175         // "Help"
    176         case NSHelpFunctionKey:
    177             return "Help";
    178 
    179         // "Hiragana"
    180 
    181         // "Home"
    182         case NSHomeFunctionKey:
    183             return "Home";
    184         // "Insert"
    185         case NSInsertFunctionKey:
    186             return "Insert";
    187 
    188         // "JapaneseHiragana"
    189         // "JapaneseKatakana"
    190         // "JapaneseRomaji"
    191         // "JunjaMode"
    192         // "KanaMode"
    193         // "KanjiMode"
    194         // "Katakana"
    195         // "LaunchApplication1"
    196         // "LaunchApplication2"
    197         // "LaunchMail"
    198 
    199         // "Left"
    200         case NSLeftArrowFunctionKey:
    201             return "Left";
    202 
    203         // "Meta"
    204         // "MediaNextTrack"
    205         // "MediaPlayPause"
    206         // "MediaPreviousTrack"
    207         // "MediaStop"
    208 
    209         // "ModeChange"
    210         case NSModeSwitchFunctionKey:
    211             return "ModeChange";
    212 
    213         // "Nonconvert"
    214         // "NumLock"
    215 
    216         // "PageDown"
    217         case NSPageDownFunctionKey:
    218             return "PageDown";
    219         // "PageUp"
    220         case NSPageUpFunctionKey:
    221             return "PageUp";
    222 
    223         // "Paste"
    224 
    225         // "Pause"
    226         case NSPauseFunctionKey:
    227             return "Pause";
    228 
    229         // "Play"
    230         // "PreviousCandidate"
    231 
    232         // "PrintScreen"
    233         case NSPrintScreenFunctionKey:
    234             return "PrintScreen";
    235 
    236         // "Process"
    237         // "Props"
    238 
    239         // "Right"
    240         case NSRightArrowFunctionKey:
    241             return "Right";
    242 
    243         // "RomanCharacters"
    244 
    245         // "Scroll"
    246         case NSScrollLockFunctionKey:
    247             return "Scroll";
    248         // "Select"
    249         case NSSelectFunctionKey:
    250             return "Select";
    251 
    252         // "SelectMedia"
    253         // "Shift"
    254 
    255         // "Stop"
    256         case NSStopFunctionKey:
    257             return "Stop";
    258         // "Up"
    259         case NSUpArrowFunctionKey:
    260             return "Up";
    261         // "Undo"
    262         case NSUndoFunctionKey:
    263             return "Undo";
    264 
    265         // "VolumeDown"
    266         // "VolumeMute"
    267         // "VolumeUp"
    268         // "Win"
    269         // "Zoom"
    270 
    271         // More function keys, not in the key identifier specification.
    272         case NSF25FunctionKey:
    273             return "F25";
    274         case NSF26FunctionKey:
    275             return "F26";
    276         case NSF27FunctionKey:
    277             return "F27";
    278         case NSF28FunctionKey:
    279             return "F28";
    280         case NSF29FunctionKey:
    281             return "F29";
    282         case NSF30FunctionKey:
    283             return "F30";
    284         case NSF31FunctionKey:
    285             return "F31";
    286         case NSF32FunctionKey:
    287             return "F32";
    288         case NSF33FunctionKey:
    289             return "F33";
    290         case NSF34FunctionKey:
    291             return "F34";
    292         case NSF35FunctionKey:
    293             return "F35";
    294 
    295         // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
    296         case 0x7F:
    297             return "U+0008";
    298         // Standard says that DEL becomes U+007F.
    299         case NSDeleteFunctionKey:
    300             return "U+007F";
    301 
    302         // Always use 0x09 for tab instead of AppKit's backtab character.
    303         case NSBackTabCharacter:
    304             return "U+0009";
    305 
    306         case NSBeginFunctionKey:
    307         case NSBreakFunctionKey:
    308         case NSClearDisplayFunctionKey:
    309         case NSDeleteCharFunctionKey:
    310         case NSDeleteLineFunctionKey:
    311         case NSInsertCharFunctionKey:
    312         case NSInsertLineFunctionKey:
    313         case NSNextFunctionKey:
    314         case NSPrevFunctionKey:
    315         case NSPrintFunctionKey:
    316         case NSRedoFunctionKey:
    317         case NSResetFunctionKey:
    318         case NSSysReqFunctionKey:
    319         case NSSystemFunctionKey:
    320         case NSUserFunctionKey:
    321             // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
    322             // For now, just fall through to the default.
    323         default:
    324             return String::format("U+%04X", toASCIIUpper(charCode));
    325     }
    326 }
    327 
    328 int windowsKeyCodeForKeyCode(uint16_t keyCode)
    329 {
    330     static const int windowsKeyCode[] = {
    331         /* 0 */ VK_A,
    332         /* 1 */ VK_S,
    333         /* 2 */ VK_D,
    334         /* 3 */ VK_F,
    335         /* 4 */ VK_H,
    336         /* 5 */ VK_G,
    337         /* 6 */ VK_Z,
    338         /* 7 */ VK_X,
    339         /* 8 */ VK_C,
    340         /* 9 */ VK_V,
    341         /* 0x0A */ VK_OEM_3, // "Section" - key to the left from 1 (ISO Keyboard Only)
    342         /* 0x0B */ VK_B,
    343         /* 0x0C */ VK_Q,
    344         /* 0x0D */ VK_W,
    345         /* 0x0E */ VK_E,
    346         /* 0x0F */ VK_R,
    347         /* 0x10 */ VK_Y,
    348         /* 0x11 */ VK_T,
    349         /* 0x12 */ VK_1,
    350         /* 0x13 */ VK_2,
    351         /* 0x14 */ VK_3,
    352         /* 0x15 */ VK_4,
    353         /* 0x16 */ VK_6,
    354         /* 0x17 */ VK_5,
    355         /* 0x18 */ VK_OEM_PLUS, // =+
    356         /* 0x19 */ VK_9,
    357         /* 0x1A */ VK_7,
    358         /* 0x1B */ VK_OEM_MINUS, // -_
    359         /* 0x1C */ VK_8,
    360         /* 0x1D */ VK_0,
    361         /* 0x1E */ VK_OEM_6, // ]}
    362         /* 0x1F */ VK_O,
    363         /* 0x20 */ VK_U,
    364         /* 0x21 */ VK_OEM_4, // {[
    365         /* 0x22 */ VK_I,
    366         /* 0x23 */ VK_P,
    367         /* 0x24 */ VK_RETURN, // Return
    368         /* 0x25 */ VK_L,
    369         /* 0x26 */ VK_J,
    370         /* 0x27 */ VK_OEM_7, // '"
    371         /* 0x28 */ VK_K,
    372         /* 0x29 */ VK_OEM_1, // ;:
    373         /* 0x2A */ VK_OEM_5, // \|
    374         /* 0x2B */ VK_OEM_COMMA, // ,<
    375         /* 0x2C */ VK_OEM_2, // /?
    376         /* 0x2D */ VK_N,
    377         /* 0x2E */ VK_M,
    378         /* 0x2F */ VK_OEM_PERIOD, // .>
    379         /* 0x30 */ VK_TAB,
    380         /* 0x31 */ VK_SPACE,
    381         /* 0x32 */ VK_OEM_3, // `~
    382         /* 0x33 */ VK_BACK, // Backspace
    383         /* 0x34 */ 0, // n/a
    384         /* 0x35 */ VK_ESCAPE,
    385         /* 0x36 */ VK_APPS, // Right Command
    386         /* 0x37 */ VK_LWIN, // Left Command
    387         /* 0x38 */ VK_LSHIFT, // Left Shift
    388         /* 0x39 */ VK_CAPITAL, // Caps Lock
    389         /* 0x3A */ VK_LMENU, // Left Option
    390         /* 0x3B */ VK_LCONTROL, // Left Ctrl
    391         /* 0x3C */ VK_RSHIFT, // Right Shift
    392         /* 0x3D */ VK_RMENU, // Right Option
    393         /* 0x3E */ VK_RCONTROL, // Right Ctrl
    394         /* 0x3F */ 0, // fn
    395         /* 0x40 */ VK_F17,
    396         /* 0x41 */ VK_DECIMAL, // Num Pad .
    397         /* 0x42 */ 0, // n/a
    398         /* 0x43 */ VK_MULTIPLY, // Num Pad *
    399         /* 0x44 */ 0, // n/a
    400         /* 0x45 */ VK_ADD, // Num Pad +
    401         /* 0x46 */ 0, // n/a
    402         /* 0x47 */ VK_CLEAR, // Num Pad Clear
    403         /* 0x48 */ VK_VOLUME_UP,
    404         /* 0x49 */ VK_VOLUME_DOWN,
    405         /* 0x4A */ VK_VOLUME_MUTE,
    406         /* 0x4B */ VK_DIVIDE, // Num Pad /
    407         /* 0x4C */ VK_RETURN, // Num Pad Enter
    408         /* 0x4D */ 0, // n/a
    409         /* 0x4E */ VK_SUBTRACT, // Num Pad -
    410         /* 0x4F */ VK_F18,
    411         /* 0x50 */ VK_F19,
    412         /* 0x51 */ VK_OEM_PLUS, // Num Pad =. There is no such key on common PC keyboards, mapping to normal "+=".
    413         /* 0x52 */ VK_NUMPAD0,
    414         /* 0x53 */ VK_NUMPAD1,
    415         /* 0x54 */ VK_NUMPAD2,
    416         /* 0x55 */ VK_NUMPAD3,
    417         /* 0x56 */ VK_NUMPAD4,
    418         /* 0x57 */ VK_NUMPAD5,
    419         /* 0x58 */ VK_NUMPAD6,
    420         /* 0x59 */ VK_NUMPAD7,
    421         /* 0x5A */ VK_F20,
    422         /* 0x5B */ VK_NUMPAD8,
    423         /* 0x5C */ VK_NUMPAD9,
    424         /* 0x5D */ 0, // Yen (JIS Keyboard Only)
    425         /* 0x5E */ 0, // Underscore (JIS Keyboard Only)
    426         /* 0x5F */ 0, // KeypadComma (JIS Keyboard Only)
    427         /* 0x60 */ VK_F5,
    428         /* 0x61 */ VK_F6,
    429         /* 0x62 */ VK_F7,
    430         /* 0x63 */ VK_F3,
    431         /* 0x64 */ VK_F8,
    432         /* 0x65 */ VK_F9,
    433         /* 0x66 */ 0, // Eisu (JIS Keyboard Only)
    434         /* 0x67 */ VK_F11,
    435         /* 0x68 */ 0, // Kana (JIS Keyboard Only)
    436         /* 0x69 */ VK_F13,
    437         /* 0x6A */ VK_F16,
    438         /* 0x6B */ VK_F14,
    439         /* 0x6C */ 0, // n/a
    440         /* 0x6D */ VK_F10,
    441         /* 0x6E */ 0, // n/a (Windows95 key?)
    442         /* 0x6F */ VK_F12,
    443         /* 0x70 */ 0, // n/a
    444         /* 0x71 */ VK_F15,
    445         /* 0x72 */ VK_INSERT, // Help
    446         /* 0x73 */ VK_HOME, // Home
    447         /* 0x74 */ VK_PRIOR, // Page Up
    448         /* 0x75 */ VK_DELETE, // Forward Delete
    449         /* 0x76 */ VK_F4,
    450         /* 0x77 */ VK_END, // End
    451         /* 0x78 */ VK_F2,
    452         /* 0x79 */ VK_NEXT, // Page Down
    453         /* 0x7A */ VK_F1,
    454         /* 0x7B */ VK_LEFT, // Left Arrow
    455         /* 0x7C */ VK_RIGHT, // Right Arrow
    456         /* 0x7D */ VK_DOWN, // Down Arrow
    457         /* 0x7E */ VK_UP, // Up Arrow
    458         /* 0x7F */ 0 // n/a
    459     };
    460 
    461     if (keyCode >= 0x80)
    462         return 0;
    463 
    464      return windowsKeyCode[keyCode];
    465 }
    466 
    467 int windowsKeyCodeForCharCode(unichar charCode)
    468 {
    469     switch (charCode) {
    470 
    471         case 'a': case 'A': return VK_A;
    472         case 'b': case 'B': return VK_B;
    473         case 'c': case 'C': return VK_C;
    474         case 'd': case 'D': return VK_D;
    475         case 'e': case 'E': return VK_E;
    476         case 'f': case 'F': return VK_F;
    477         case 'g': case 'G': return VK_G;
    478         case 'h': case 'H': return VK_H;
    479         case 'i': case 'I': return VK_I;
    480         case 'j': case 'J': return VK_J;
    481         case 'k': case 'K': return VK_K;
    482         case 'l': case 'L': return VK_L;
    483         case 'm': case 'M': return VK_M;
    484         case 'n': case 'N': return VK_N;
    485         case 'o': case 'O': return VK_O;
    486         case 'p': case 'P': return VK_P;
    487         case 'q': case 'Q': return VK_Q;
    488         case 'r': case 'R': return VK_R;
    489         case 's': case 'S': return VK_S;
    490         case 't': case 'T': return VK_T;
    491         case 'u': case 'U': return VK_U;
    492         case 'v': case 'V': return VK_V;
    493         case 'w': case 'W': return VK_W;
    494         case 'x': case 'X': return VK_X;
    495         case 'y': case 'Y': return VK_Y;
    496         case 'z': case 'Z': return VK_Z;
    497 
    498         // AppKit generates Unicode PUA character codes for some function keys; using these when key code is not known.
    499         case NSPauseFunctionKey: return VK_PAUSE;
    500         case NSSelectFunctionKey: return VK_SELECT;
    501         case NSPrintFunctionKey: return VK_PRINT;
    502         case NSExecuteFunctionKey: return VK_EXECUTE;
    503         case NSPrintScreenFunctionKey: return VK_SNAPSHOT;
    504         case NSInsertFunctionKey: return VK_INSERT;
    505 
    506         case NSF21FunctionKey: return VK_F21;
    507         case NSF22FunctionKey: return VK_F22;
    508         case NSF23FunctionKey: return VK_F23;
    509         case NSF24FunctionKey: return VK_F24;
    510         case NSScrollLockFunctionKey: return VK_SCROLL;
    511 
    512         // This is for U.S. keyboard mapping, and doesn't necessarily make sense for different keyboard layouts.
    513         // For example, '"' on Windows Russian layout is VK_2, not VK_OEM_7.
    514         case ';': case ':': return VK_OEM_1;
    515         case '=': case '+': return VK_OEM_PLUS;
    516         case ',': case '<': return VK_OEM_COMMA;
    517         case '-': case '_': return VK_OEM_MINUS;
    518         case '.': case '>': return VK_OEM_PERIOD;
    519         case '/': case '?': return VK_OEM_2;
    520         case '`': case '~': return VK_OEM_3;
    521         case '[': case '{': return VK_OEM_4;
    522         case '\\': case '|': return VK_OEM_5;
    523         case ']': case '}': return VK_OEM_6;
    524         case '\'': case '"': return VK_OEM_7;
    525 
    526     }
    527 
    528     return 0;
    529 }
    530 
    531 }
    532