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

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

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