1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
9 #import <Carbon/Carbon.h>
11 #include "base/logging.h"
17 // A struct to hold a Windows keycode to Mac virtual keycode mapping.
21 unichar characterIgnoringModifiers;
24 // Customized less operator for using std::lower_bound() on a KeyCodeMap array.
25 bool operator<(const KeyCodeMap& a, const KeyCodeMap& b) {
26 return a.keycode < b.keycode;
29 // This array must keep sorted ascending according to the value of |keycode|,
30 // so that we can binary search it.
31 // TODO(suzhe): This map is not complete, missing entries have macKeycode == -1.
32 const KeyCodeMap kKeyCodesMap[] = {
33 { VKEY_BACK /* 0x08 */, kVK_Delete, kBackspaceCharCode },
34 { VKEY_TAB /* 0x09 */, kVK_Tab, kTabCharCode },
35 { VKEY_BACKTAB /* 0x0A */, 0x21E4, '\031' },
36 { VKEY_CLEAR /* 0x0C */, kVK_ANSI_KeypadClear, kClearCharCode },
37 { VKEY_RETURN /* 0x0D */, kVK_Return, kReturnCharCode },
38 { VKEY_SHIFT /* 0x10 */, kVK_Shift, 0 },
39 { VKEY_CONTROL /* 0x11 */, kVK_Control, 0 },
40 { VKEY_MENU /* 0x12 */, kVK_Option, 0 },
41 { VKEY_PAUSE /* 0x13 */, -1, NSPauseFunctionKey },
42 { VKEY_CAPITAL /* 0x14 */, kVK_CapsLock, 0 },
43 { VKEY_KANA /* 0x15 */, kVK_JIS_Kana, 0 },
44 { VKEY_HANGUL /* 0x15 */, -1, 0 },
45 { VKEY_JUNJA /* 0x17 */, -1, 0 },
46 { VKEY_FINAL /* 0x18 */, -1, 0 },
47 { VKEY_HANJA /* 0x19 */, -1, 0 },
48 { VKEY_KANJI /* 0x19 */, -1, 0 },
49 { VKEY_ESCAPE /* 0x1B */, kVK_Escape, kEscapeCharCode },
50 { VKEY_CONVERT /* 0x1C */, -1, 0 },
51 { VKEY_NONCONVERT /* 0x1D */, -1, 0 },
52 { VKEY_ACCEPT /* 0x1E */, -1, 0 },
53 { VKEY_MODECHANGE /* 0x1F */, -1, 0 },
54 { VKEY_SPACE /* 0x20 */, kVK_Space, kSpaceCharCode },
55 { VKEY_PRIOR /* 0x21 */, kVK_PageUp, NSPageUpFunctionKey },
56 { VKEY_NEXT /* 0x22 */, kVK_PageDown, NSPageDownFunctionKey },
57 { VKEY_END /* 0x23 */, kVK_End, NSEndFunctionKey },
58 { VKEY_HOME /* 0x24 */, kVK_Home, NSHomeFunctionKey },
59 { VKEY_LEFT /* 0x25 */, kVK_LeftArrow, NSLeftArrowFunctionKey },
60 { VKEY_UP /* 0x26 */, kVK_UpArrow, NSUpArrowFunctionKey },
61 { VKEY_RIGHT /* 0x27 */, kVK_RightArrow, NSRightArrowFunctionKey },
62 { VKEY_DOWN /* 0x28 */, kVK_DownArrow, NSDownArrowFunctionKey },
63 { VKEY_SELECT /* 0x29 */, -1, 0 },
64 { VKEY_PRINT /* 0x2A */, -1, NSPrintFunctionKey },
65 { VKEY_EXECUTE /* 0x2B */, -1, NSExecuteFunctionKey },
66 { VKEY_SNAPSHOT /* 0x2C */, -1, NSPrintScreenFunctionKey },
67 { VKEY_INSERT /* 0x2D */, -1, NSInsertFunctionKey },
68 { VKEY_DELETE /* 0x2E */, kVK_ForwardDelete, kDeleteCharCode },
69 { VKEY_HELP /* 0x2F */, kVK_Help, kHelpCharCode },
70 { VKEY_0 /* 0x30 */, kVK_ANSI_0, '0' },
71 { VKEY_1 /* 0x31 */, kVK_ANSI_1, '1' },
72 { VKEY_2 /* 0x32 */, kVK_ANSI_2, '2' },
73 { VKEY_3 /* 0x33 */, kVK_ANSI_3, '3' },
74 { VKEY_4 /* 0x34 */, kVK_ANSI_4, '4' },
75 { VKEY_5 /* 0x35 */, kVK_ANSI_5, '5' },
76 { VKEY_6 /* 0x36 */, kVK_ANSI_6, '6' },
77 { VKEY_7 /* 0x37 */, kVK_ANSI_7, '7' },
78 { VKEY_8 /* 0x38 */, kVK_ANSI_8, '8' },
79 { VKEY_9 /* 0x39 */, kVK_ANSI_9, '9' },
80 { VKEY_A /* 0x41 */, kVK_ANSI_A, 'a' },
81 { VKEY_B /* 0x42 */, kVK_ANSI_B, 'b' },
82 { VKEY_C /* 0x43 */, kVK_ANSI_C, 'c' },
83 { VKEY_D /* 0x44 */, kVK_ANSI_D, 'd' },
84 { VKEY_E /* 0x45 */, kVK_ANSI_E, 'e' },
85 { VKEY_F /* 0x46 */, kVK_ANSI_F, 'f' },
86 { VKEY_G /* 0x47 */, kVK_ANSI_G, 'g' },
87 { VKEY_H /* 0x48 */, kVK_ANSI_H, 'h' },
88 { VKEY_I /* 0x49 */, kVK_ANSI_I, 'i' },
89 { VKEY_J /* 0x4A */, kVK_ANSI_J, 'j' },
90 { VKEY_K /* 0x4B */, kVK_ANSI_K, 'k' },
91 { VKEY_L /* 0x4C */, kVK_ANSI_L, 'l' },
92 { VKEY_M /* 0x4D */, kVK_ANSI_M, 'm' },
93 { VKEY_N /* 0x4E */, kVK_ANSI_N, 'n' },
94 { VKEY_O /* 0x4F */, kVK_ANSI_O, 'o' },
95 { VKEY_P /* 0x50 */, kVK_ANSI_P, 'p' },
96 { VKEY_Q /* 0x51 */, kVK_ANSI_Q, 'q' },
97 { VKEY_R /* 0x52 */, kVK_ANSI_R, 'r' },
98 { VKEY_S /* 0x53 */, kVK_ANSI_S, 's' },
99 { VKEY_T /* 0x54 */, kVK_ANSI_T, 't' },
100 { VKEY_U /* 0x55 */, kVK_ANSI_U, 'u' },
101 { VKEY_V /* 0x56 */, kVK_ANSI_V, 'v' },
102 { VKEY_W /* 0x57 */, kVK_ANSI_W, 'w' },
103 { VKEY_X /* 0x58 */, kVK_ANSI_X, 'x' },
104 { VKEY_Y /* 0x59 */, kVK_ANSI_Y, 'y' },
105 { VKEY_Z /* 0x5A */, kVK_ANSI_Z, 'z' },
106 { VKEY_LWIN /* 0x5B */, kVK_Command, 0 },
107 { VKEY_RWIN /* 0x5C */, 0x36, 0 },
108 { VKEY_APPS /* 0x5D */, 0x36, 0 },
109 { VKEY_SLEEP /* 0x5F */, -1, 0 },
110 { VKEY_NUMPAD0 /* 0x60 */, kVK_ANSI_Keypad0, '0' },
111 { VKEY_NUMPAD1 /* 0x61 */, kVK_ANSI_Keypad1, '1' },
112 { VKEY_NUMPAD2 /* 0x62 */, kVK_ANSI_Keypad2, '2' },
113 { VKEY_NUMPAD3 /* 0x63 */, kVK_ANSI_Keypad3, '3' },
114 { VKEY_NUMPAD4 /* 0x64 */, kVK_ANSI_Keypad4, '4' },
115 { VKEY_NUMPAD5 /* 0x65 */, kVK_ANSI_Keypad5, '5' },
116 { VKEY_NUMPAD6 /* 0x66 */, kVK_ANSI_Keypad6, '6' },
117 { VKEY_NUMPAD7 /* 0x67 */, kVK_ANSI_Keypad7, '7' },
118 { VKEY_NUMPAD8 /* 0x68 */, kVK_ANSI_Keypad8, '8' },
119 { VKEY_NUMPAD9 /* 0x69 */, kVK_ANSI_Keypad9, '9' },
120 { VKEY_MULTIPLY /* 0x6A */, kVK_ANSI_KeypadMultiply, '*' },
121 { VKEY_ADD /* 0x6B */, kVK_ANSI_KeypadPlus, '+' },
122 { VKEY_SEPARATOR /* 0x6C */, -1, 0 },
123 { VKEY_SUBTRACT /* 0x6D */, kVK_ANSI_KeypadMinus, '-' },
124 { VKEY_DECIMAL /* 0x6E */, kVK_ANSI_KeypadDecimal, '.' },
125 { VKEY_DIVIDE /* 0x6F */, kVK_ANSI_KeypadDivide, '/' },
126 { VKEY_F1 /* 0x70 */, kVK_F1, NSF1FunctionKey },
127 { VKEY_F2 /* 0x71 */, kVK_F2, NSF2FunctionKey },
128 { VKEY_F3 /* 0x72 */, kVK_F3, NSF3FunctionKey },
129 { VKEY_F4 /* 0x73 */, kVK_F4, NSF4FunctionKey },
130 { VKEY_F5 /* 0x74 */, kVK_F5, NSF5FunctionKey },
131 { VKEY_F6 /* 0x75 */, kVK_F6, NSF6FunctionKey },
132 { VKEY_F7 /* 0x76 */, kVK_F7, NSF7FunctionKey },
133 { VKEY_F8 /* 0x77 */, kVK_F8, NSF8FunctionKey },
134 { VKEY_F9 /* 0x78 */, kVK_F9, NSF9FunctionKey },
135 { VKEY_F10 /* 0x79 */, kVK_F10, NSF10FunctionKey },
136 { VKEY_F11 /* 0x7A */, kVK_F11, NSF11FunctionKey },
137 { VKEY_F12 /* 0x7B */, kVK_F12, NSF12FunctionKey },
138 { VKEY_F13 /* 0x7C */, kVK_F13, NSF13FunctionKey },
139 { VKEY_F14 /* 0x7D */, kVK_F14, NSF14FunctionKey },
140 { VKEY_F15 /* 0x7E */, kVK_F15, NSF15FunctionKey },
141 { VKEY_F16 /* 0x7F */, kVK_F16, NSF16FunctionKey },
142 { VKEY_F17 /* 0x80 */, kVK_F17, NSF17FunctionKey },
143 { VKEY_F18 /* 0x81 */, kVK_F18, NSF18FunctionKey },
144 { VKEY_F19 /* 0x82 */, kVK_F19, NSF19FunctionKey },
145 { VKEY_F20 /* 0x83 */, kVK_F20, NSF20FunctionKey },
146 { VKEY_F21 /* 0x84 */, -1, NSF21FunctionKey },
147 { VKEY_F22 /* 0x85 */, -1, NSF22FunctionKey },
148 { VKEY_F23 /* 0x86 */, -1, NSF23FunctionKey },
149 { VKEY_F24 /* 0x87 */, -1, NSF24FunctionKey },
150 { VKEY_NUMLOCK /* 0x90 */, -1, 0 },
151 { VKEY_SCROLL /* 0x91 */, -1, NSScrollLockFunctionKey },
152 { VKEY_LSHIFT /* 0xA0 */, kVK_Shift, 0 },
153 { VKEY_RSHIFT /* 0xA1 */, kVK_Shift, 0 },
154 { VKEY_LCONTROL /* 0xA2 */, kVK_Control, 0 },
155 { VKEY_RCONTROL /* 0xA3 */, kVK_Control, 0 },
156 { VKEY_LMENU /* 0xA4 */, -1, 0 },
157 { VKEY_RMENU /* 0xA5 */, -1, 0 },
158 { VKEY_BROWSER_BACK /* 0xA6 */, -1, 0 },
159 { VKEY_BROWSER_FORWARD /* 0xA7 */, -1, 0 },
160 { VKEY_BROWSER_REFRESH /* 0xA8 */, -1, 0 },
161 { VKEY_BROWSER_STOP /* 0xA9 */, -1, 0 },
162 { VKEY_BROWSER_SEARCH /* 0xAA */, -1, 0 },
163 { VKEY_BROWSER_FAVORITES /* 0xAB */, -1, 0 },
164 { VKEY_BROWSER_HOME /* 0xAC */, -1, 0 },
165 { VKEY_VOLUME_MUTE /* 0xAD */, -1, 0 },
166 { VKEY_VOLUME_DOWN /* 0xAE */, -1, 0 },
167 { VKEY_VOLUME_UP /* 0xAF */, -1, 0 },
168 { VKEY_MEDIA_NEXT_TRACK /* 0xB0 */, -1, 0 },
169 { VKEY_MEDIA_PREV_TRACK /* 0xB1 */, -1, 0 },
170 { VKEY_MEDIA_STOP /* 0xB2 */, -1, 0 },
171 { VKEY_MEDIA_PLAY_PAUSE /* 0xB3 */, -1, 0 },
172 { VKEY_MEDIA_LAUNCH_MAIL /* 0xB4 */, -1, 0 },
173 { VKEY_MEDIA_LAUNCH_MEDIA_SELECT /* 0xB5 */, -1, 0 },
174 { VKEY_MEDIA_LAUNCH_APP1 /* 0xB6 */, -1, 0 },
175 { VKEY_MEDIA_LAUNCH_APP2 /* 0xB7 */, -1, 0 },
176 { VKEY_OEM_1 /* 0xBA */, kVK_ANSI_Semicolon, ';' },
177 { VKEY_OEM_PLUS /* 0xBB */, kVK_ANSI_Equal, '=' },
178 { VKEY_OEM_COMMA /* 0xBC */, kVK_ANSI_Comma, ',' },
179 { VKEY_OEM_MINUS /* 0xBD */, kVK_ANSI_Minus, '-' },
180 { VKEY_OEM_PERIOD /* 0xBE */, kVK_ANSI_Period, '.' },
181 { VKEY_OEM_2 /* 0xBF */, kVK_ANSI_Slash, '/' },
182 { VKEY_OEM_3 /* 0xC0 */, kVK_ANSI_Grave, '`' },
183 { VKEY_OEM_4 /* 0xDB */, kVK_ANSI_LeftBracket, '[' },
184 { VKEY_OEM_5 /* 0xDC */, kVK_ANSI_Backslash, '\\' },
185 { VKEY_OEM_6 /* 0xDD */, kVK_ANSI_RightBracket, ']' },
186 { VKEY_OEM_7 /* 0xDE */, kVK_ANSI_Quote, '\'' },
187 { VKEY_OEM_8 /* 0xDF */, -1, 0 },
188 { VKEY_OEM_102 /* 0xE2 */, -1, 0 },
189 { VKEY_PROCESSKEY /* 0xE5 */, -1, 0 },
190 { VKEY_PACKET /* 0xE7 */, -1, 0 },
191 { VKEY_ATTN /* 0xF6 */, -1, 0 },
192 { VKEY_CRSEL /* 0xF7 */, -1, 0 },
193 { VKEY_EXSEL /* 0xF8 */, -1, 0 },
194 { VKEY_EREOF /* 0xF9 */, -1, 0 },
195 { VKEY_PLAY /* 0xFA */, -1, 0 },
196 { VKEY_ZOOM /* 0xFB */, -1, 0 },
197 { VKEY_NONAME /* 0xFC */, -1, 0 },
198 { VKEY_PA1 /* 0xFD */, -1, 0 },
199 { VKEY_OEM_CLEAR /* 0xFE */, kVK_ANSI_KeypadClear, kClearCharCode }
202 // A convenient array for getting symbol characters on the number keys.
203 const char kShiftCharsForNumberKeys[] = ")!@#$%^&*(";
205 // Translates from character code to keyboard code.
206 KeyboardCode KeyboardCodeFromCharCode(unichar charCode) {
208 case 8: case 0x7F: return VKEY_BACK;
209 case 9: return VKEY_TAB;
210 case 0xD: case 3: return VKEY_RETURN;
211 case 0x1B: return VKEY_ESCAPE;
212 case ' ': return VKEY_SPACE;
213 case NSHomeFunctionKey: return VKEY_HOME;
214 case NSEndFunctionKey: return VKEY_END;
215 case NSPageUpFunctionKey: return VKEY_PRIOR;
216 case NSPageDownFunctionKey: return VKEY_NEXT;
217 case NSUpArrowFunctionKey: return VKEY_UP;
218 case NSDownArrowFunctionKey: return VKEY_DOWN;
219 case NSLeftArrowFunctionKey: return VKEY_LEFT;
220 case NSRightArrowFunctionKey: return VKEY_RIGHT;
221 case NSDeleteFunctionKey: return VKEY_DELETE;
223 case '0': case ')': return VKEY_0;
224 case '1': case '!': return VKEY_1;
225 case '2': case '@': return VKEY_2;
226 case '3': case '#': return VKEY_3;
227 case '4': case '$': return VKEY_4;
228 case '5': case '%': return VKEY_5;
229 case '6': case '^': return VKEY_6;
230 case '7': case '&': return VKEY_7;
231 case '8': case '*': return VKEY_8;
232 case '9': case '(': return VKEY_9;
234 case 'a': case 'A': return VKEY_A;
235 case 'b': case 'B': return VKEY_B;
236 case 'c': case 'C': return VKEY_C;
237 case 'd': case 'D': return VKEY_D;
238 case 'e': case 'E': return VKEY_E;
239 case 'f': case 'F': return VKEY_F;
240 case 'g': case 'G': return VKEY_G;
241 case 'h': case 'H': return VKEY_H;
242 case 'i': case 'I': return VKEY_I;
243 case 'j': case 'J': return VKEY_J;
244 case 'k': case 'K': return VKEY_K;
245 case 'l': case 'L': return VKEY_L;
246 case 'm': case 'M': return VKEY_M;
247 case 'n': case 'N': return VKEY_N;
248 case 'o': case 'O': return VKEY_O;
249 case 'p': case 'P': return VKEY_P;
250 case 'q': case 'Q': return VKEY_Q;
251 case 'r': case 'R': return VKEY_R;
252 case 's': case 'S': return VKEY_S;
253 case 't': case 'T': return VKEY_T;
254 case 'u': case 'U': return VKEY_U;
255 case 'v': case 'V': return VKEY_V;
256 case 'w': case 'W': return VKEY_W;
257 case 'x': case 'X': return VKEY_X;
258 case 'y': case 'Y': return VKEY_Y;
259 case 'z': case 'Z': return VKEY_Z;
261 case NSPauseFunctionKey: return VKEY_PAUSE;
262 case NSSelectFunctionKey: return VKEY_SELECT;
263 case NSPrintFunctionKey: return VKEY_PRINT;
264 case NSExecuteFunctionKey: return VKEY_EXECUTE;
265 case NSPrintScreenFunctionKey: return VKEY_SNAPSHOT;
266 case NSInsertFunctionKey: return VKEY_INSERT;
267 case NSHelpFunctionKey: return VKEY_INSERT;
269 case NSF1FunctionKey: return VKEY_F1;
270 case NSF2FunctionKey: return VKEY_F2;
271 case NSF3FunctionKey: return VKEY_F3;
272 case NSF4FunctionKey: return VKEY_F4;
273 case NSF5FunctionKey: return VKEY_F5;
274 case NSF6FunctionKey: return VKEY_F6;
275 case NSF7FunctionKey: return VKEY_F7;
276 case NSF8FunctionKey: return VKEY_F8;
277 case NSF9FunctionKey: return VKEY_F9;
278 case NSF10FunctionKey: return VKEY_F10;
279 case NSF11FunctionKey: return VKEY_F11;
280 case NSF12FunctionKey: return VKEY_F12;
281 case NSF13FunctionKey: return VKEY_F13;
282 case NSF14FunctionKey: return VKEY_F14;
283 case NSF15FunctionKey: return VKEY_F15;
284 case NSF16FunctionKey: return VKEY_F16;
285 case NSF17FunctionKey: return VKEY_F17;
286 case NSF18FunctionKey: return VKEY_F18;
287 case NSF19FunctionKey: return VKEY_F19;
288 case NSF20FunctionKey: return VKEY_F20;
290 case NSF21FunctionKey: return VKEY_F21;
291 case NSF22FunctionKey: return VKEY_F22;
292 case NSF23FunctionKey: return VKEY_F23;
293 case NSF24FunctionKey: return VKEY_F24;
294 case NSScrollLockFunctionKey: return VKEY_SCROLL;
296 // U.S. Specific mappings. Mileage may vary.
297 case ';': case ':': return VKEY_OEM_1;
298 case '=': case '+': return VKEY_OEM_PLUS;
299 case ',': case '<': return VKEY_OEM_COMMA;
300 case '-': case '_': return VKEY_OEM_MINUS;
301 case '.': case '>': return VKEY_OEM_PERIOD;
302 case '/': case '?': return VKEY_OEM_2;
303 case '`': case '~': return VKEY_OEM_3;
304 case '[': case '{': return VKEY_OEM_4;
305 case '\\': case '|': return VKEY_OEM_5;
306 case ']': case '}': return VKEY_OEM_6;
307 case '\'': case '"': return VKEY_OEM_7;
313 KeyboardCode KeyboardCodeFromKeyCode(unsigned short keyCode) {
314 static const KeyboardCode kKeyboardCodes[] = {
325 /* 0x0A */ VKEY_OEM_3, // Section key.
339 /* 0x18 */ VKEY_OEM_PLUS, // =+
342 /* 0x1B */ VKEY_OEM_MINUS, // -_
345 /* 0x1E */ VKEY_OEM_6, // ]}
348 /* 0x21 */ VKEY_OEM_4, // {[
351 /* 0x24 */ VKEY_RETURN, // Return
354 /* 0x27 */ VKEY_OEM_7, // '"
356 /* 0x29 */ VKEY_OEM_1, // ;:
357 /* 0x2A */ VKEY_OEM_5, // \|
358 /* 0x2B */ VKEY_OEM_COMMA, // ,<
359 /* 0x2C */ VKEY_OEM_2, // /?
362 /* 0x2F */ VKEY_OEM_PERIOD, // .>
364 /* 0x31 */ VKEY_SPACE,
365 /* 0x32 */ VKEY_OEM_3, // `~
366 /* 0x33 */ VKEY_BACK, // Backspace
367 /* 0x34 */ VKEY_UNKNOWN, // n/a
368 /* 0x35 */ VKEY_ESCAPE,
369 /* 0x36 */ VKEY_APPS, // Right Command
370 /* 0x37 */ VKEY_LWIN, // Left Command
371 /* 0x38 */ VKEY_SHIFT, // Left Shift
372 /* 0x39 */ VKEY_CAPITAL, // Caps Lock
373 /* 0x3A */ VKEY_MENU, // Left Option
374 /* 0x3B */ VKEY_CONTROL, // Left Ctrl
375 /* 0x3C */ VKEY_SHIFT, // Right Shift
376 /* 0x3D */ VKEY_MENU, // Right Option
377 /* 0x3E */ VKEY_CONTROL, // Right Ctrl
378 /* 0x3F */ VKEY_UNKNOWN, // fn
380 /* 0x41 */ VKEY_DECIMAL, // Num Pad .
381 /* 0x42 */ VKEY_UNKNOWN, // n/a
382 /* 0x43 */ VKEY_MULTIPLY, // Num Pad *
383 /* 0x44 */ VKEY_UNKNOWN, // n/a
384 /* 0x45 */ VKEY_ADD, // Num Pad +
385 /* 0x46 */ VKEY_UNKNOWN, // n/a
386 /* 0x47 */ VKEY_CLEAR, // Num Pad Clear
387 /* 0x48 */ VKEY_VOLUME_UP,
388 /* 0x49 */ VKEY_VOLUME_DOWN,
389 /* 0x4A */ VKEY_VOLUME_MUTE,
390 /* 0x4B */ VKEY_DIVIDE, // Num Pad /
391 /* 0x4C */ VKEY_RETURN, // Num Pad Enter
392 /* 0x4D */ VKEY_UNKNOWN, // n/a
393 /* 0x4E */ VKEY_SUBTRACT, // Num Pad -
396 /* 0x51 */ VKEY_OEM_PLUS, // Num Pad =.
397 /* 0x52 */ VKEY_NUMPAD0,
398 /* 0x53 */ VKEY_NUMPAD1,
399 /* 0x54 */ VKEY_NUMPAD2,
400 /* 0x55 */ VKEY_NUMPAD3,
401 /* 0x56 */ VKEY_NUMPAD4,
402 /* 0x57 */ VKEY_NUMPAD5,
403 /* 0x58 */ VKEY_NUMPAD6,
404 /* 0x59 */ VKEY_NUMPAD7,
406 /* 0x5B */ VKEY_NUMPAD8,
407 /* 0x5C */ VKEY_NUMPAD9,
408 /* 0x5D */ VKEY_UNKNOWN, // Yen (JIS Keyboard Only)
409 /* 0x5E */ VKEY_UNKNOWN, // Underscore (JIS Keyboard Only)
410 /* 0x5F */ VKEY_UNKNOWN, // KeypadComma (JIS Keyboard Only)
417 /* 0x66 */ VKEY_UNKNOWN, // Eisu (JIS Keyboard Only)
419 /* 0x68 */ VKEY_UNKNOWN, // Kana (JIS Keyboard Only)
423 /* 0x6C */ VKEY_UNKNOWN, // n/a
425 /* 0x6E */ VKEY_UNKNOWN, // n/a (Windows95 key?)
427 /* 0x70 */ VKEY_UNKNOWN, // n/a
429 /* 0x72 */ VKEY_INSERT, // Help
430 /* 0x73 */ VKEY_HOME, // Home
431 /* 0x74 */ VKEY_PRIOR, // Page Up
432 /* 0x75 */ VKEY_DELETE, // Forward Delete
434 /* 0x77 */ VKEY_END, // End
436 /* 0x79 */ VKEY_NEXT, // Page Down
438 /* 0x7B */ VKEY_LEFT, // Left Arrow
439 /* 0x7C */ VKEY_RIGHT, // Right Arrow
440 /* 0x7D */ VKEY_DOWN, // Down Arrow
441 /* 0x7E */ VKEY_UP, // Up Arrow
442 /* 0x7F */ VKEY_UNKNOWN // n/a
448 return kKeyboardCodes[keyCode];
453 int MacKeyCodeForWindowsKeyCode(KeyboardCode keycode,
456 unichar* characterIgnoringModifiers) {
458 from.keycode = keycode;
460 const KeyCodeMap* ptr = std::lower_bound(
461 kKeyCodesMap, kKeyCodesMap + arraysize(kKeyCodesMap), from);
463 if (ptr >= kKeyCodesMap + arraysize(kKeyCodesMap) ||
464 ptr->keycode != keycode || ptr->macKeycode == -1)
467 int macKeycode = ptr->macKeycode;
468 if (characterIgnoringModifiers)
469 *characterIgnoringModifiers = ptr->characterIgnoringModifiers;
474 *character = ptr->characterIgnoringModifiers;
476 // Fill in |character| according to flags.
477 if (flags & NSShiftKeyMask) {
478 if (keycode >= VKEY_0 && keycode <= VKEY_9) {
479 *character = kShiftCharsForNumberKeys[keycode - VKEY_0];
480 } else if (keycode >= VKEY_A && keycode <= VKEY_Z) {
481 *character = 'A' + (keycode - VKEY_A);
483 switch (macKeycode) {
493 case kVK_ANSI_LeftBracket:
496 case kVK_ANSI_RightBracket:
499 case kVK_ANSI_Backslash:
502 case kVK_ANSI_Semicolon:
511 case kVK_ANSI_Period:
523 // Control characters.
524 if (flags & NSControlKeyMask) {
525 if (keycode >= VKEY_A && keycode <= VKEY_Z)
526 *character = 1 + keycode - VKEY_A;
527 else if (macKeycode == kVK_ANSI_LeftBracket)
529 else if (macKeycode == kVK_ANSI_Backslash)
531 else if (macKeycode == kVK_ANSI_RightBracket)
535 // TODO(suzhe): Support characters for Option key bindings.
539 KeyboardCode KeyboardCodeFromNSEvent(NSEvent* event) {
540 KeyboardCode code = VKEY_UNKNOWN;
542 if ([event type] == NSKeyDown || [event type] == NSKeyUp) {
543 NSString* characters = [event characters];
544 if ([characters length] > 0)
545 code = KeyboardCodeFromCharCode([characters characterAtIndex:0]);
549 characters = [event charactersIgnoringModifiers];
550 if ([characters length] > 0)
551 code = KeyboardCodeFromCharCode([characters characterAtIndex:0]);
555 return KeyboardCodeFromKeyCode([event keyCode]);