Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(948)

Side by Side Diff: ui/events/keycodes/keyboard_code_conversion.cc

Issue 1287103004: Sync ui/events to chromium @ https://codereview.chromium.org/1210203002 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/events/keycodes/keyboard_code_conversion.h" 5 #include "ui/events/keycodes/keyboard_code_conversion.h"
6 6
7 #include <algorithm>
8
7 #include "ui/events/event_constants.h" 9 #include "ui/events/event_constants.h"
10 #include "ui/events/keycodes/dom/dom_code.h"
11 #include "ui/events/keycodes/dom/dom_key.h"
12 #include "ui/events/keycodes/dom_us_layout_data.h"
8 13
9 namespace ui { 14 namespace ui {
10 15
11 uint16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags) { 16 namespace {
17
18 // This table maps a subset of |KeyboardCode| (VKEYs) to DomKey and character.
19 // Only values not otherwise handled by GetMeaningFromKeyCode() are here.
20 const struct KeyboardCodeToMeaning {
21 KeyboardCode key_code;
22 DomKey key;
23 base::char16 plain_character;
24 base::char16 shift_character;
25 } kKeyboardCodeToMeaning[] = {
26 {VKEY_BACK, DomKey::BACKSPACE, '\b', 0},
27 {VKEY_TAB, DomKey::TAB, '\t', 0},
28 {VKEY_RETURN, DomKey::ENTER, '\r', 0},
29 {VKEY_ESCAPE, DomKey::ESCAPE, 0x1B, 0},
30 {VKEY_SPACE, DomKey::CHARACTER, ' ', 0},
31 {VKEY_MULTIPLY, DomKey::CHARACTER, '*', 0},
32 {VKEY_ADD, DomKey::CHARACTER, '+', 0},
33 {VKEY_SEPARATOR, DomKey::CHARACTER, ',', 0},
34 {VKEY_SUBTRACT, DomKey::CHARACTER, '-', 0},
35 {VKEY_DECIMAL, DomKey::CHARACTER, '.', 0},
36 {VKEY_DIVIDE, DomKey::CHARACTER, '/', 0},
37 {VKEY_OEM_1, DomKey::CHARACTER, ';', ':'},
38 {VKEY_OEM_PLUS, DomKey::CHARACTER, '=', '+'},
39 {VKEY_OEM_COMMA, DomKey::CHARACTER, ',', '<'},
40 {VKEY_OEM_MINUS, DomKey::CHARACTER, '-', '_'},
41 {VKEY_OEM_PERIOD, DomKey::CHARACTER, '.', '>'},
42 {VKEY_OEM_2, DomKey::CHARACTER, '/', '?'},
43 {VKEY_OEM_3, DomKey::CHARACTER, '`', '~'},
44 {VKEY_OEM_4, DomKey::CHARACTER, '[', '{'},
45 {VKEY_OEM_5, DomKey::CHARACTER, '\\', '|'},
46 {VKEY_OEM_6, DomKey::CHARACTER, ']', '}'},
47 {VKEY_OEM_7, DomKey::CHARACTER, '\'', '"'},
48 {VKEY_OEM_102, DomKey::CHARACTER, '<', '>'},
49 {VKEY_CLEAR, DomKey::CLEAR, 0, 0},
50 {VKEY_SHIFT, DomKey::SHIFT, 0, 0},
51 {VKEY_CONTROL, DomKey::CONTROL, 0, 0},
52 {VKEY_MENU, DomKey::ALT, 0, 0},
53 {VKEY_PAUSE, DomKey::PAUSE, 0, 0},
54 {VKEY_CAPITAL, DomKey::CAPS_LOCK, 0, 0},
55 // Windows conflates 'KanaMode' and 'HangulMode'.
56 {VKEY_KANA, DomKey::KANA_MODE, 0, 0},
57 {VKEY_JUNJA, DomKey::JUNJA_MODE, 0, 0},
58 {VKEY_FINAL, DomKey::FINAL_MODE, 0, 0},
59 // Windows conflates 'HanjaMode' and 'KanjiMode'.
60 {VKEY_HANJA, DomKey::HANJA_MODE, 0, 0},
61 {VKEY_CONVERT, DomKey::CONVERT, 0, 0},
62 {VKEY_NONCONVERT, DomKey::NON_CONVERT, 0, 0},
63 {VKEY_ACCEPT, DomKey::ACCEPT, 0, 0},
64 {VKEY_MODECHANGE, DomKey::MODE_CHANGE, 0, 0},
65 {VKEY_PRIOR, DomKey::PAGE_UP, 0, 0},
66 {VKEY_NEXT, DomKey::PAGE_DOWN, 0, 0},
67 {VKEY_END, DomKey::END, 0, 0},
68 {VKEY_HOME, DomKey::HOME, 0, 0},
69 {VKEY_LEFT, DomKey::ARROW_LEFT, 0, 0},
70 {VKEY_UP, DomKey::ARROW_UP, 0, 0},
71 {VKEY_RIGHT, DomKey::ARROW_RIGHT, 0, 0},
72 {VKEY_DOWN, DomKey::ARROW_DOWN, 0, 0},
73 {VKEY_SELECT, DomKey::SELECT, 0, 0},
74 {VKEY_PRINT, DomKey::PRINT, 0, 0},
75 {VKEY_EXECUTE, DomKey::EXECUTE, 0, 0},
76 {VKEY_SNAPSHOT, DomKey::PRINT_SCREEN, 0, 0},
77 {VKEY_INSERT, DomKey::INSERT, 0, 0},
78 {VKEY_DELETE, DomKey::DEL, 0, 0},
79 {VKEY_HELP, DomKey::HELP, 0, 0},
80 {VKEY_LWIN, DomKey::OS, 0, 0},
81 {VKEY_RWIN, DomKey::OS, 0, 0},
82 {VKEY_APPS, DomKey::MEDIA_APPS, 0, 0},
83 {VKEY_NUMLOCK, DomKey::NUM_LOCK, 0, 0},
84 {VKEY_SCROLL, DomKey::SCROLL_LOCK, 0, 0},
85 {VKEY_LSHIFT, DomKey::SHIFT, 0, 0},
86 {VKEY_RSHIFT, DomKey::SHIFT, 0, 0},
87 {VKEY_LCONTROL, DomKey::CONTROL, 0, 0},
88 {VKEY_RCONTROL, DomKey::CONTROL, 0, 0},
89 {VKEY_LMENU, DomKey::ALT, 0, 0},
90 {VKEY_RMENU, DomKey::ALT, 0, 0},
91 {VKEY_BROWSER_BACK, DomKey::BROWSER_BACK, 0, 0},
92 {VKEY_BROWSER_FORWARD, DomKey::BROWSER_FORWARD, 0, 0},
93 {VKEY_BROWSER_REFRESH, DomKey::BROWSER_REFRESH, 0, 0},
94 {VKEY_BROWSER_STOP, DomKey::BROWSER_STOP, 0, 0},
95 {VKEY_BROWSER_SEARCH, DomKey::BROWSER_SEARCH, 0, 0},
96 {VKEY_BROWSER_FAVORITES, DomKey::BROWSER_FAVORITES, 0, 0},
97 {VKEY_BROWSER_HOME, DomKey::BROWSER_HOME, 0, 0},
98 {VKEY_VOLUME_MUTE, DomKey::VOLUME_MUTE, 0, 0},
99 {VKEY_VOLUME_DOWN, DomKey::VOLUME_DOWN, 0, 0},
100 {VKEY_VOLUME_UP, DomKey::VOLUME_UP, 0, 0},
101 {VKEY_MEDIA_NEXT_TRACK, DomKey::MEDIA_TRACK_NEXT, 0, 0},
102 {VKEY_MEDIA_PREV_TRACK, DomKey::MEDIA_TRACK_PREVIOUS, 0, 0},
103 {VKEY_MEDIA_STOP, DomKey::MEDIA_STOP, 0, 0},
104 {VKEY_MEDIA_PLAY_PAUSE, DomKey::MEDIA_PLAY_PAUSE, 0, 0},
105 {VKEY_MEDIA_LAUNCH_MAIL, DomKey::LAUNCH_MAIL, 0, 0},
106 {VKEY_MEDIA_LAUNCH_MEDIA_SELECT, DomKey::LAUNCH_MEDIA_PLAYER, 0, 0},
107 {VKEY_MEDIA_LAUNCH_APP1, DomKey::LAUNCH_MY_COMPUTER, 0, 0},
108 {VKEY_MEDIA_LAUNCH_APP2, DomKey::LAUNCH_CALCULATOR, 0, 0},
109 {VKEY_OEM_8, DomKey::SUPER, 0, 0}, // ISO Level 5 Shift in ChromeOS
110 {VKEY_PROCESSKEY, DomKey::PROCESS, 0, 0},
111 {VKEY_DBE_SBCSCHAR, DomKey::HANKAKU, 0, 0},
112 {VKEY_DBE_DBCSCHAR, DomKey::ZENKAKU, 0, 0},
113 {VKEY_ATTN, DomKey::ATTN, 0, 0},
114 {VKEY_CRSEL, DomKey::CR_SEL, 0, 0},
115 {VKEY_EXSEL, DomKey::EX_SEL, 0, 0},
116 {VKEY_EREOF, DomKey::ERASE_EOF, 0, 0},
117 {VKEY_PLAY, DomKey::MEDIA_PLAY, 0, 0},
118 {VKEY_ZOOM, DomKey::ZOOM_TOGGLE, 0, 0},
119 {VKEY_OEM_CLEAR, DomKey::CLEAR, 0, 0},
120 {VKEY_ALTGR, DomKey::ALT_GRAPH, 0, 0},
121 #if defined(OS_POSIX)
122 {VKEY_POWER, DomKey::POWER, 0, 0},
123 {VKEY_BRIGHTNESS_DOWN, DomKey::BRIGHTNESS_DOWN, 0, 0},
124 {VKEY_BRIGHTNESS_UP, DomKey::BRIGHTNESS_UP, 0, 0},
125 {VKEY_COMPOSE, DomKey::COMPOSE, 0, 0},
126 {VKEY_OEM_103, DomKey::MEDIA_REWIND, 0, 0},
127 {VKEY_OEM_104, DomKey::MEDIA_FAST_FORWARD, 0, 0},
128 #endif
129 };
130
131 bool IsRightSideDomCode(DomCode code) {
132 return (code == DomCode::SHIFT_RIGHT) || (code == DomCode::CONTROL_RIGHT) ||
133 (code == DomCode::ALT_RIGHT) || (code == DomCode::OS_RIGHT);
134 }
135
136 bool IsModifierDomCode(DomCode code) {
137 return (code == DomCode::CONTROL_LEFT) || (code == DomCode::CONTROL_RIGHT) ||
138 (code == DomCode::SHIFT_LEFT) || (code == DomCode::SHIFT_RIGHT) ||
139 (code == DomCode::ALT_LEFT) || (code == DomCode::ALT_RIGHT) ||
140 (code == DomCode::OS_LEFT) || (code == DomCode::OS_RIGHT);
141 }
142
143 // Returns the Windows-based VKEY value corresponding to a DOM Level 3 |code|,
144 // assuming a base US English layout. The returned VKEY is located
145 // (e.g. VKEY_LSHIFT).
146 KeyboardCode DomCodeToUsLayoutKeyboardCode(DomCode dom_code) {
147 const DomCodeToKeyboardCodeEntry* end =
148 kDomCodeToKeyboardCodeMap + arraysize(kDomCodeToKeyboardCodeMap);
149 const DomCodeToKeyboardCodeEntry* found =
150 std::lower_bound(kDomCodeToKeyboardCodeMap, end, dom_code,
151 [](const DomCodeToKeyboardCodeEntry& a, DomCode b) {
152 return static_cast<int>(a.dom_code) < static_cast<int>(b);
153 });
154 if ((found != end) && (found->dom_code == dom_code))
155 return found->key_code;
156 return VKEY_UNKNOWN;
157 }
158
159 } // anonymous namespace
160
161 base::char16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags) {
162 ui::DomKey dom_key;
163 base::char16 character;
164 if (GetMeaningFromKeyCode(key_code, flags, &dom_key, &character))
165 return character;
166 return 0;
167 }
168
169 bool GetMeaningFromKeyCode(KeyboardCode key_code,
170 int flags,
171 DomKey* dom_key,
172 base::char16* character) {
12 const bool ctrl = (flags & EF_CONTROL_DOWN) != 0; 173 const bool ctrl = (flags & EF_CONTROL_DOWN) != 0;
13 const bool shift = (flags & EF_SHIFT_DOWN) != 0; 174 const bool shift = (flags & EF_SHIFT_DOWN) != 0;
14 const bool upper = shift ^ ((flags & EF_CAPS_LOCK_DOWN) != 0); 175 const bool upper = shift ^ ((flags & EF_CAPS_LOCK_DOWN) != 0);
15 176
16 // Following Windows behavior to map ctrl-a ~ ctrl-z to \x01 ~ \x1A. 177 // Control characters.
17 if (key_code >= VKEY_A && key_code <= VKEY_Z) {
18 return static_cast<uint16>
19 (key_code - VKEY_A + (ctrl ? 1 : (upper ? 'A' : 'a')));
20 }
21
22 // Other ctrl characters
23 if (ctrl) { 178 if (ctrl) {
179 // Following Windows behavior to map ctrl-a ~ ctrl-z to \x01 ~ \x1A.
180 if (key_code >= VKEY_A && key_code <= VKEY_Z) {
181 *character = static_cast<uint16>(key_code - VKEY_A + 1);
182 switch (key_code) {
183 case VKEY_H:
184 *dom_key = DomKey::BACKSPACE;
185 break;
186 case VKEY_I:
187 *dom_key = DomKey::TAB;
188 break;
189 case VKEY_J:
190 case VKEY_M:
191 *dom_key = DomKey::ENTER;
192 break;
193 default:
194 *dom_key = DomKey::CHARACTER;
195 break;
196 }
197 return true;
198 }
199 // Other control characters.
24 if (shift) { 200 if (shift) {
25 // following graphics chars require shift key to input. 201 // The following graphics characters require the shift key to input.
26 switch (key_code) { 202 switch (key_code) {
27 // ctrl-@ maps to \x00 (Null byte) 203 // ctrl-@ maps to \x00 (Null byte)
28 case VKEY_2: 204 case VKEY_2:
29 return 0; 205 *dom_key = DomKey::CHARACTER;
206 *character = 0;
207 return true;
30 // ctrl-^ maps to \x1E (Record separator, Information separator two) 208 // ctrl-^ maps to \x1E (Record separator, Information separator two)
31 case VKEY_6: 209 case VKEY_6:
32 return 0x1E; 210 *dom_key = DomKey::CHARACTER;
211 *character = 0x1E;
212 return true;
33 // ctrl-_ maps to \x1F (Unit separator, Information separator one) 213 // ctrl-_ maps to \x1F (Unit separator, Information separator one)
34 case VKEY_OEM_MINUS: 214 case VKEY_OEM_MINUS:
35 return 0x1F; 215 *dom_key = DomKey::CHARACTER;
216 *character = 0x1F;
217 return true;
36 // Returns 0 for all other keys to avoid inputting unexpected chars. 218 // Returns 0 for all other keys to avoid inputting unexpected chars.
37 default: 219 default:
38 return 0; 220 *dom_key = DomKey::UNIDENTIFIED;
221 *character = 0;
222 return false;
39 } 223 }
40 } else { 224 } else {
41 switch (key_code) { 225 switch (key_code) {
42 // ctrl-[ maps to \x1B (Escape) 226 // ctrl-[ maps to \x1B (Escape)
43 case VKEY_OEM_4: 227 case VKEY_OEM_4:
44 return 0x1B; 228 *dom_key = DomKey::ESCAPE;
229 *character = 0x1B;
230 return true;
45 // ctrl-\ maps to \x1C (File separator, Information separator four) 231 // ctrl-\ maps to \x1C (File separator, Information separator four)
46 case VKEY_OEM_5: 232 case VKEY_OEM_5:
47 return 0x1C; 233 *dom_key = DomKey::CHARACTER;
234 *character = 0x1C;
235 return true;
48 // ctrl-] maps to \x1D (Group separator, Information separator three) 236 // ctrl-] maps to \x1D (Group separator, Information separator three)
49 case VKEY_OEM_6: 237 case VKEY_OEM_6:
50 return 0x1D; 238 *dom_key = DomKey::CHARACTER;
239 *character = 0x1D;
240 return true;
51 // ctrl-Enter maps to \x0A (Line feed) 241 // ctrl-Enter maps to \x0A (Line feed)
52 case VKEY_RETURN: 242 case VKEY_RETURN:
53 return 0x0A; 243 *dom_key = DomKey::CHARACTER;
244 *character = 0x0A;
245 return true;
54 // Returns 0 for all other keys to avoid inputting unexpected chars. 246 // Returns 0 for all other keys to avoid inputting unexpected chars.
55 default: 247 default:
56 return 0; 248 *dom_key = DomKey::UNIDENTIFIED;
249 *character = 0;
250 return false;
57 } 251 }
58 } 252 }
59 } 253 }
60 254
61 // For IME support. 255 // ASCII alphanumeric characters.
62 if (key_code == ui::VKEY_PROCESSKEY) 256 if (key_code >= VKEY_A && key_code <= VKEY_Z) {
63 return 0xE5; 257 *dom_key = DomKey::CHARACTER;
64 258 *character = static_cast<uint16>(key_code - VKEY_A + (upper ? 'A' : 'a'));
65 // Normal characters 259 return true;
260 }
66 if (key_code >= VKEY_0 && key_code <= VKEY_9) { 261 if (key_code >= VKEY_0 && key_code <= VKEY_9) {
67 return shift ? ")!@#$%^&*("[key_code - VKEY_0] : 262 *dom_key = DomKey::CHARACTER;
68 static_cast<uint16>(key_code); 263 *character =
69 } else if (key_code >= VKEY_NUMPAD0 && key_code <= VKEY_NUMPAD9) { 264 shift ? ")!@#$%^&*("[key_code - VKEY_0] : static_cast<uint16>(key_code);
70 return static_cast<uint16>(key_code - VKEY_NUMPAD0 + '0'); 265 return true;
71 } 266 }
72 267 if (key_code >= VKEY_NUMPAD0 && key_code <= VKEY_NUMPAD9) {
268 *dom_key = DomKey::CHARACTER;
269 *character = static_cast<uint16>(key_code - VKEY_NUMPAD0 + '0');
270 return true;
271 }
272
273 // Function keys.
274 if (key_code >= VKEY_F1 && key_code <= VKEY_F24) {
275 *dom_key =
276 static_cast<DomKey>(key_code - VKEY_F1 + static_cast<int>(DomKey::F1));
277 *character = 0;
278 return true;
279 }
280
281 // Other keys.
282 for (size_t i = 0; i < arraysize(kKeyboardCodeToMeaning); ++i) {
283 if (kKeyboardCodeToMeaning[i].key_code == key_code) {
284 const KeyboardCodeToMeaning* p = &kKeyboardCodeToMeaning[i];
285 *dom_key = p->key;
286 *character = (shift && p->shift_character) ? p->shift_character
287 : p->plain_character;
288 return true;
289 }
290 }
291 *dom_key = DomKey::UNIDENTIFIED;
292 *character = 0;
293 return false;
294 }
295
296 bool DomCodeToUsLayoutMeaning(DomCode dom_code,
297 int flags,
298 DomKey* out_dom_key,
299 base::char16* out_character,
300 KeyboardCode* out_key_code) {
301 if ((flags & EF_CONTROL_DOWN) == EF_CONTROL_DOWN) {
302 if (DomCodeToControlCharacter(dom_code, flags, out_dom_key, out_character,
303 out_key_code)) {
304 return true;
305 }
306 if (!IsModifierDomCode(dom_code)) {
307 *out_dom_key = DomKey::UNIDENTIFIED;
308 *out_character = 0;
309 *out_key_code = LocatedToNonLocatedKeyboardCode(
310 DomCodeToUsLayoutKeyboardCode(dom_code));
311 return true;
312 }
313 } else {
314 for (const auto& it : kPrintableCodeMap) {
315 if (it.dom_code == dom_code) {
316 int state = ((flags & EF_SHIFT_DOWN) == EF_SHIFT_DOWN);
317 base::char16 ch = it.character[state];
318 *out_dom_key = DomKey::CHARACTER;
319 *out_character = ch;
320 if ((flags & EF_CAPS_LOCK_DOWN) == EF_CAPS_LOCK_DOWN) {
321 ch |= 0x20;
322 if ((ch >= 'a') && (ch <= 'z'))
323 *out_character = it.character[state ^ 1];
324 }
325 *out_key_code = LocatedToNonLocatedKeyboardCode(
326 DomCodeToUsLayoutKeyboardCode(dom_code));
327 return true;
328 }
329 }
330 }
331 for (const auto& it : kNonPrintableCodeMap) {
332 if (it.dom_code == dom_code) {
333 *out_dom_key = it.dom_key;
334 *out_character = it.character;
335 *out_key_code = NonPrintableDomKeyToKeyboardCode(it.dom_key);
336 return true;
337 }
338 }
339 if ((flags & EF_CONTROL_DOWN) == EF_CONTROL_DOWN) {
340 *out_dom_key = DomKey::UNIDENTIFIED;
341 *out_character = 0;
342 *out_key_code = LocatedToNonLocatedKeyboardCode(
343 DomCodeToUsLayoutKeyboardCode(dom_code));
344 return true;
345 }
346 return false;
347 }
348
349 bool DomCodeToControlCharacter(DomCode dom_code,
350 int flags,
351 DomKey* dom_key,
352 base::char16* character,
353 KeyboardCode* key_code) {
354 if ((flags & EF_CONTROL_DOWN) == 0)
355 return false;
356
357 int code = static_cast<int>(dom_code);
358 const int kKeyA = static_cast<int>(DomCode::KEY_A);
359 // Control-A - Control-Z map to 0x01 - 0x1A.
360 if (code >= kKeyA && code <= static_cast<int>(DomCode::KEY_Z)) {
361 *character = static_cast<base::char16>(code - kKeyA + 1);
362 switch (dom_code) {
363 case DomCode::KEY_H:
364 *dom_key = DomKey::BACKSPACE;
365 *key_code = VKEY_BACK;
366 break;
367 case DomCode::KEY_I:
368 *dom_key = DomKey::TAB;
369 *key_code = VKEY_TAB;
370 break;
371 case DomCode::KEY_M:
372 *dom_key = DomKey::ENTER;
373 *key_code = VKEY_RETURN;
374 break;
375 default:
376 *dom_key = DomKey::CHARACTER;
377 *key_code = static_cast<KeyboardCode>(code - kKeyA + VKEY_A);
378 break;
379 }
380 return true;
381 }
382
383 if (flags & EF_SHIFT_DOWN) {
384 switch (dom_code) {
385 case DomCode::DIGIT2:
386 // NUL
387 *character = 0;
388 *dom_key = DomKey::CHARACTER;
389 *key_code = VKEY_2;
390 return true;
391 case DomCode::DIGIT6:
392 // RS
393 *character = 0x1E;
394 *dom_key = DomKey::CHARACTER;
395 *key_code = VKEY_6;
396 return true;
397 case DomCode::MINUS:
398 // US
399 *character = 0x1F;
400 *dom_key = DomKey::CHARACTER;
401 *key_code = VKEY_OEM_MINUS;
402 return true;
403 default:
404 return false;
405 }
406 }
407
408 switch (dom_code) {
409 case DomCode::ENTER:
410 // NL
411 *character = 0x0A;
412 *dom_key = DomKey::CHARACTER;
413 *key_code = VKEY_RETURN;
414 return true;
415 case DomCode::BRACKET_LEFT:
416 // ESC
417 *character = 0x1B;
418 *dom_key = DomKey::ESCAPE;
419 *key_code = VKEY_OEM_4;
420 return true;
421 case DomCode::BACKSLASH:
422 // FS
423 *character = 0x1C;
424 *dom_key = DomKey::CHARACTER;
425 *key_code = VKEY_OEM_5;
426 return true;
427 case DomCode::BRACKET_RIGHT:
428 // GS
429 *character = 0x1D;
430 *dom_key = DomKey::CHARACTER;
431 *key_code = VKEY_OEM_6;
432 return true;
433 default:
434 return false;
435 }
436 }
437
438 DomKey CharacterToDomKey(uint32 character) {
439 switch (character) {
440 case 0x08:
441 return DomKey::BACKSPACE;
442 case 0x09:
443 return DomKey::TAB;
444 case 0x0D:
445 return DomKey::ENTER;
446 case 0x1B:
447 return DomKey::ESCAPE;
448 case 0x7F:
449 return DomKey::DEL;
450 default:
451 return DomKey::CHARACTER;
452 }
453 }
454
455 // Returns a Windows-based VKEY for a non-printable DOM Level 3 |key|.
456 // The returned VKEY is non-positional (e.g. VKEY_SHIFT).
457 KeyboardCode NonPrintableDomKeyToKeyboardCode(DomKey dom_key) {
458 for (const auto& it : kDomKeyToKeyboardCodeMap) {
459 if (it.dom_key == dom_key)
460 return it.key_code;
461 }
462 return VKEY_UNKNOWN;
463 }
464
465 // Determine the non-located VKEY corresponding to a located VKEY.
466 KeyboardCode LocatedToNonLocatedKeyboardCode(KeyboardCode key_code) {
73 switch (key_code) { 467 switch (key_code) {
74 case VKEY_TAB: 468 case VKEY_RWIN:
75 return '\t'; 469 return VKEY_LWIN;
76 case VKEY_RETURN: 470 case VKEY_LSHIFT:
77 return '\r'; 471 case VKEY_RSHIFT:
78 case VKEY_MULTIPLY: 472 return VKEY_SHIFT;
79 return '*'; 473 case VKEY_LCONTROL:
80 case VKEY_ADD: 474 case VKEY_RCONTROL:
81 return '+'; 475 return VKEY_CONTROL;
82 case VKEY_SUBTRACT: 476 case VKEY_LMENU:
83 return '-'; 477 case VKEY_RMENU:
84 case VKEY_DECIMAL: 478 return VKEY_MENU;
85 return '.'; 479 case VKEY_NUMPAD0:
86 case VKEY_DIVIDE: 480 return VKEY_0;
87 return '/'; 481 case VKEY_NUMPAD1:
88 case VKEY_SPACE: 482 return VKEY_1;
89 return ' '; 483 case VKEY_NUMPAD2:
90 case VKEY_OEM_1: 484 return VKEY_2;
91 return shift ? ':' : ';'; 485 case VKEY_NUMPAD3:
92 case VKEY_OEM_PLUS: 486 return VKEY_3;
93 return shift ? '+' : '='; 487 case VKEY_NUMPAD4:
94 case VKEY_OEM_COMMA: 488 return VKEY_4;
95 return shift ? '<' : ','; 489 case VKEY_NUMPAD5:
96 case VKEY_OEM_MINUS: 490 return VKEY_5;
97 return shift ? '_' : '-'; 491 case VKEY_NUMPAD6:
98 case VKEY_OEM_PERIOD: 492 return VKEY_6;
99 return shift ? '>' : '.'; 493 case VKEY_NUMPAD7:
100 case VKEY_OEM_2: 494 return VKEY_7;
101 return shift ? '?' : '/'; 495 case VKEY_NUMPAD8:
102 case VKEY_OEM_3: 496 return VKEY_8;
103 return shift ? '~' : '`'; 497 case VKEY_NUMPAD9:
104 case VKEY_OEM_4: 498 return VKEY_9;
105 return shift ? '{' : '[';
106 case VKEY_OEM_5:
107 return shift ? '|' : '\\';
108 case VKEY_OEM_6:
109 return shift ? '}' : ']';
110 case VKEY_OEM_7:
111 return shift ? '"' : '\'';
112 default: 499 default:
113 return 0; 500 return key_code;
114 } 501 }
502 }
503
504 // Determine the located VKEY corresponding to a non-located VKEY.
505 KeyboardCode NonLocatedToLocatedKeyboardCode(KeyboardCode key_code,
506 DomCode dom_code) {
507 switch (key_code) {
508 case VKEY_SHIFT:
509 return IsRightSideDomCode(dom_code) ? VKEY_RSHIFT : VKEY_LSHIFT;
510 case VKEY_CONTROL:
511 return IsRightSideDomCode(dom_code) ? VKEY_RCONTROL : VKEY_LCONTROL;
512 case VKEY_MENU:
513 return IsRightSideDomCode(dom_code) ? VKEY_RMENU : VKEY_LMENU;
514 case VKEY_LWIN:
515 return IsRightSideDomCode(dom_code) ? VKEY_RWIN : VKEY_LWIN;
516 case VKEY_0:
517 return (dom_code == DomCode::NUMPAD0) ? VKEY_NUMPAD0 : VKEY_0;
518 case VKEY_1:
519 return (dom_code == DomCode::NUMPAD1) ? VKEY_NUMPAD1 : VKEY_1;
520 case VKEY_2:
521 return (dom_code == DomCode::NUMPAD2) ? VKEY_NUMPAD2 : VKEY_2;
522 case VKEY_3:
523 return (dom_code == DomCode::NUMPAD3) ? VKEY_NUMPAD3 : VKEY_3;
524 case VKEY_4:
525 return (dom_code == DomCode::NUMPAD4) ? VKEY_NUMPAD4 : VKEY_4;
526 case VKEY_5:
527 return (dom_code == DomCode::NUMPAD5) ? VKEY_NUMPAD5 : VKEY_5;
528 case VKEY_6:
529 return (dom_code == DomCode::NUMPAD6) ? VKEY_NUMPAD6 : VKEY_6;
530 case VKEY_7:
531 return (dom_code == DomCode::NUMPAD7) ? VKEY_NUMPAD7 : VKEY_7;
532 case VKEY_8:
533 return (dom_code == DomCode::NUMPAD8) ? VKEY_NUMPAD8 : VKEY_8;
534 case VKEY_9:
535 return (dom_code == DomCode::NUMPAD9) ? VKEY_NUMPAD9 : VKEY_9;
536 default:
537 return key_code;
538 }
539 }
540
541 DomCode UsLayoutKeyboardCodeToDomCode(KeyboardCode key_code) {
542 key_code = NonLocatedToLocatedKeyboardCode(key_code, DomCode::NONE);
543 for (const auto& it : kDomCodeToKeyboardCodeMap) {
544 if (it.key_code == key_code)
545 return it.dom_code;
546 }
547 for (const auto& it : kFallbackKeyboardCodeToDomCodeMap) {
548 if (it.key_code == key_code)
549 return it.dom_code;
550 }
551 return DomCode::NONE;
115 } 552 }
116 553
117 } // namespace ui 554 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/keycodes/keyboard_code_conversion.h ('k') | ui/events/keycodes/keyboard_code_conversion_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698