OLD | NEW |
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. | |
19 // Only values not otherwise handled by GetDomKeyFromKeyCode() are here. | |
20 const struct KeyboardCodeToDomKey { | |
21 KeyboardCode key_code; | |
22 DomKey::Base plain; | |
23 DomKey::Base shift; | |
24 } kKeyboardCodeToDomKey[] = { | |
25 {VKEY_BACK, DomKey::BACKSPACE}, | |
26 {VKEY_TAB, DomKey::TAB}, | |
27 {VKEY_RETURN, DomKey::ENTER}, | |
28 {VKEY_ESCAPE, DomKey::ESCAPE}, | |
29 {VKEY_SPACE, DomKey::Constant<' '>::Character}, | |
30 {VKEY_MULTIPLY, DomKey::Constant<'*'>::Character}, | |
31 {VKEY_ADD, DomKey::Constant<'+'>::Character}, | |
32 {VKEY_SEPARATOR, DomKey::Constant<','>::Character}, | |
33 {VKEY_SUBTRACT, DomKey::Constant<'-'>::Character}, | |
34 {VKEY_DECIMAL, DomKey::Constant<'.'>::Character}, | |
35 {VKEY_DIVIDE, DomKey::Constant<'/'>::Character}, | |
36 {VKEY_OEM_1, DomKey::Constant<';'>::Character, | |
37 DomKey::Constant<':'>::Character}, | |
38 {VKEY_OEM_PLUS, DomKey::Constant<'='>::Character, | |
39 DomKey::Constant<'+'>::Character}, | |
40 {VKEY_OEM_COMMA, DomKey::Constant<','>::Character, | |
41 DomKey::Constant<'<'>::Character}, | |
42 {VKEY_OEM_MINUS, DomKey::Constant<'-'>::Character, | |
43 DomKey::Constant<'_'>::Character}, | |
44 {VKEY_OEM_PERIOD, DomKey::Constant<'.'>::Character, | |
45 DomKey::Constant<'>'>::Character}, | |
46 {VKEY_OEM_2, DomKey::Constant<'/'>::Character, | |
47 DomKey::Constant<'?'>::Character}, | |
48 {VKEY_OEM_3, DomKey::Constant<'`'>::Character, | |
49 DomKey::Constant<'~'>::Character}, | |
50 {VKEY_OEM_4, DomKey::Constant<'['>::Character, | |
51 DomKey::Constant<'{'>::Character}, | |
52 {VKEY_OEM_5, DomKey::Constant<'\\'>::Character, | |
53 DomKey::Constant<'|'>::Character}, | |
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}, | |
66 // Windows conflates 'KanaMode' and 'HangulMode'. | |
67 {VKEY_KANA, DomKey::KANA_MODE}, | |
68 {VKEY_JUNJA, DomKey::JUNJA_MODE}, | |
69 {VKEY_FINAL, DomKey::FINAL_MODE}, | |
70 // Windows conflates 'HanjaMode' and 'KanjiMode'. | |
71 {VKEY_HANJA, DomKey::HANJA_MODE}, | |
72 {VKEY_CONVERT, DomKey::CONVERT}, | |
73 {VKEY_NONCONVERT, DomKey::NON_CONVERT}, | |
74 {VKEY_ACCEPT, DomKey::ACCEPT}, | |
75 {VKEY_MODECHANGE, DomKey::MODE_CHANGE}, | |
76 {VKEY_PRIOR, DomKey::PAGE_UP}, | |
77 {VKEY_NEXT, DomKey::PAGE_DOWN}, | |
78 {VKEY_END, DomKey::END}, | |
79 {VKEY_HOME, DomKey::HOME}, | |
80 {VKEY_LEFT, DomKey::ARROW_LEFT}, | |
81 {VKEY_UP, DomKey::ARROW_UP}, | |
82 {VKEY_RIGHT, DomKey::ARROW_RIGHT}, | |
83 {VKEY_DOWN, DomKey::ARROW_DOWN}, | |
84 {VKEY_SELECT, DomKey::SELECT}, | |
85 {VKEY_PRINT, DomKey::PRINT}, | |
86 {VKEY_EXECUTE, DomKey::EXECUTE}, | |
87 {VKEY_SNAPSHOT, DomKey::PRINT_SCREEN}, | |
88 {VKEY_INSERT, DomKey::INSERT}, | |
89 {VKEY_DELETE, DomKey::DEL}, | |
90 {VKEY_HELP, DomKey::HELP}, | |
91 {VKEY_LWIN, DomKey::OS}, | |
92 {VKEY_RWIN, DomKey::OS}, | |
93 {VKEY_APPS, DomKey::MEDIA_APPS}, | |
94 {VKEY_NUMLOCK, DomKey::NUM_LOCK}, | |
95 {VKEY_SCROLL, DomKey::SCROLL_LOCK}, | |
96 {VKEY_LSHIFT, DomKey::SHIFT}, | |
97 {VKEY_RSHIFT, DomKey::SHIFT}, | |
98 {VKEY_LCONTROL, DomKey::CONTROL}, | |
99 {VKEY_RCONTROL, DomKey::CONTROL}, | |
100 {VKEY_LMENU, DomKey::ALT}, | |
101 {VKEY_RMENU, DomKey::ALT}, | |
102 {VKEY_BROWSER_BACK, DomKey::BROWSER_BACK}, | |
103 {VKEY_BROWSER_FORWARD, DomKey::BROWSER_FORWARD}, | |
104 {VKEY_BROWSER_REFRESH, DomKey::BROWSER_REFRESH}, | |
105 {VKEY_BROWSER_STOP, DomKey::BROWSER_STOP}, | |
106 {VKEY_BROWSER_SEARCH, DomKey::BROWSER_SEARCH}, | |
107 {VKEY_BROWSER_FAVORITES, DomKey::BROWSER_FAVORITES}, | |
108 {VKEY_BROWSER_HOME, DomKey::BROWSER_HOME}, | |
109 {VKEY_VOLUME_MUTE, DomKey::VOLUME_MUTE}, | |
110 {VKEY_VOLUME_DOWN, DomKey::VOLUME_DOWN}, | |
111 {VKEY_VOLUME_UP, DomKey::VOLUME_UP}, | |
112 {VKEY_MEDIA_NEXT_TRACK, DomKey::MEDIA_TRACK_NEXT}, | |
113 {VKEY_MEDIA_PREV_TRACK, DomKey::MEDIA_TRACK_PREVIOUS}, | |
114 {VKEY_MEDIA_STOP, DomKey::MEDIA_STOP}, | |
115 {VKEY_MEDIA_PLAY_PAUSE, DomKey::MEDIA_PLAY_PAUSE}, | |
116 {VKEY_MEDIA_LAUNCH_MAIL, DomKey::LAUNCH_MAIL}, | |
117 {VKEY_MEDIA_LAUNCH_MEDIA_SELECT, DomKey::LAUNCH_MEDIA_PLAYER}, | |
118 {VKEY_MEDIA_LAUNCH_APP1, DomKey::LAUNCH_MY_COMPUTER}, | |
119 {VKEY_MEDIA_LAUNCH_APP2, DomKey::LAUNCH_CALCULATOR}, | |
120 {VKEY_OEM_8, DomKey::SUPER}, // ISO Level 5 Shift in ChromeOS | |
121 {VKEY_PROCESSKEY, DomKey::PROCESS}, | |
122 {VKEY_DBE_SBCSCHAR, DomKey::HANKAKU}, | |
123 {VKEY_DBE_DBCSCHAR, DomKey::ZENKAKU}, | |
124 {VKEY_ATTN, DomKey::ATTN}, | |
125 {VKEY_CRSEL, DomKey::CR_SEL}, | |
126 {VKEY_EXSEL, DomKey::EX_SEL}, | |
127 {VKEY_EREOF, DomKey::ERASE_EOF}, | |
128 {VKEY_PLAY, DomKey::MEDIA_PLAY}, | |
129 {VKEY_ZOOM, DomKey::ZOOM_TOGGLE}, | |
130 {VKEY_OEM_CLEAR, DomKey::CLEAR}, | |
131 {VKEY_ALTGR, DomKey::ALT_GRAPH}, | |
132 #if defined(OS_POSIX) | |
133 {VKEY_POWER, DomKey::POWER}, | |
134 {VKEY_BRIGHTNESS_DOWN, DomKey::BRIGHTNESS_DOWN}, | |
135 {VKEY_BRIGHTNESS_UP, DomKey::BRIGHTNESS_UP}, | |
136 {VKEY_COMPOSE, DomKey::COMPOSE}, | |
137 {VKEY_OEM_103, DomKey::MEDIA_REWIND}, | |
138 {VKEY_OEM_104, DomKey::MEDIA_FAST_FORWARD}, | |
139 #endif | |
140 }; | |
141 | |
142 bool IsRightSideDomCode(DomCode code) { | 18 bool IsRightSideDomCode(DomCode code) { |
143 return (code == DomCode::SHIFT_RIGHT) || (code == DomCode::CONTROL_RIGHT) || | 19 return (code == DomCode::SHIFT_RIGHT) || (code == DomCode::CONTROL_RIGHT) || |
144 (code == DomCode::ALT_RIGHT) || (code == DomCode::OS_RIGHT); | 20 (code == DomCode::ALT_RIGHT) || (code == DomCode::OS_RIGHT); |
145 } | 21 } |
146 | 22 |
147 bool IsModifierDomCode(DomCode code) { | 23 bool IsModifierDomCode(DomCode code) { |
148 return (code == DomCode::CONTROL_LEFT) || (code == DomCode::CONTROL_RIGHT) || | 24 return (code == DomCode::CONTROL_LEFT) || (code == DomCode::CONTROL_RIGHT) || |
149 (code == DomCode::SHIFT_LEFT) || (code == DomCode::SHIFT_RIGHT) || | 25 (code == DomCode::SHIFT_LEFT) || (code == DomCode::SHIFT_RIGHT) || |
150 (code == DomCode::ALT_LEFT) || (code == DomCode::ALT_RIGHT) || | 26 (code == DomCode::ALT_LEFT) || (code == DomCode::ALT_RIGHT) || |
151 (code == DomCode::OS_LEFT) || (code == DomCode::OS_RIGHT); | 27 (code == DomCode::OS_LEFT) || (code == DomCode::OS_RIGHT); |
152 } | 28 } |
153 | 29 |
154 } // anonymous namespace | 30 } // anonymous namespace |
155 | 31 |
156 base::char16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags) { | 32 base::char16 DomCodeToUsLayoutCharacter(DomCode dom_code, int flags) { |
157 DomKey key = GetDomKeyFromKeyCode(key_code, flags); | 33 DomKey dom_key; |
158 if (key.IsCharacter()) | 34 KeyboardCode key_code; |
159 return key.ToCharacter(); | 35 if (DomCodeToUsLayoutDomKey(dom_code, flags, &dom_key, &key_code) && |
| 36 dom_key.IsCharacter()) { |
| 37 return dom_key.ToCharacter(); |
| 38 } |
160 return 0; | 39 return 0; |
161 } | 40 } |
162 | 41 |
163 DomKey GetDomKeyFromKeyCode(KeyboardCode key_code, int flags) { | |
164 const bool ctrl = (flags & EF_CONTROL_DOWN) != 0; | |
165 const bool shift = (flags & EF_SHIFT_DOWN) != 0; | |
166 const bool upper = shift ^ ((flags & EF_CAPS_LOCK_DOWN) != 0); | |
167 | |
168 // Control characters. | |
169 if (ctrl) { | |
170 // Following Windows behavior to map ctrl-a ~ ctrl-z to \x01 ~ \x1A. | |
171 if (key_code >= VKEY_A && key_code <= VKEY_Z) | |
172 return DomKey::FromCharacter(key_code - VKEY_A + 1); | |
173 // Other control characters. | |
174 if (shift) { | |
175 // The following graphics characters require the shift key to input. | |
176 switch (key_code) { | |
177 // ctrl-@ maps to \x00 (Null byte) | |
178 case VKEY_2: | |
179 return DomKey::FromCharacter(0); | |
180 // ctrl-^ maps to \x1E (Record separator, Information separator two) | |
181 case VKEY_6: | |
182 return DomKey::FromCharacter(0x1E); | |
183 // ctrl-_ maps to \x1F (Unit separator, Information separator one) | |
184 case VKEY_OEM_MINUS: | |
185 return DomKey::FromCharacter(0x1F); | |
186 // Returns UNIDENTIFIED for all other keys to avoid inputting | |
187 // unexpected chars. | |
188 default: | |
189 return DomKey::UNIDENTIFIED; | |
190 } | |
191 } else { | |
192 switch (key_code) { | |
193 // ctrl-[ maps to \x1B (Escape) | |
194 case VKEY_OEM_4: | |
195 return DomKey::ESCAPE; | |
196 // ctrl-\ maps to \x1C (File separator, Information separator four) | |
197 case VKEY_OEM_5: | |
198 return DomKey::FromCharacter(0x1C); | |
199 // ctrl-] maps to \x1D (Group separator, Information separator three) | |
200 case VKEY_OEM_6: | |
201 return DomKey::FromCharacter(0x1D); | |
202 // ctrl-Enter maps to \x0A (Line feed) | |
203 case VKEY_RETURN: | |
204 return DomKey::FromCharacter(0x0A); | |
205 // Returns UNIDENTIFIED for all other keys to avoid inputting | |
206 // unexpected chars. | |
207 default: | |
208 return DomKey::UNIDENTIFIED; | |
209 } | |
210 } | |
211 } | |
212 | |
213 // ASCII alphanumeric characters. | |
214 if (key_code >= VKEY_A && key_code <= VKEY_Z) | |
215 return DomKey::FromCharacter(key_code - VKEY_A + (upper ? 'A' : 'a')); | |
216 if (key_code >= VKEY_0 && key_code <= VKEY_9) { | |
217 return DomKey::FromCharacter(shift ? ")!@#$%^&*("[key_code - VKEY_0] | |
218 : '0' + key_code - VKEY_0); | |
219 } | |
220 if (key_code >= VKEY_NUMPAD0 && key_code <= VKEY_NUMPAD9) | |
221 return DomKey::FromCharacter(key_code - VKEY_NUMPAD0 + '0'); | |
222 | |
223 // Function keys. | |
224 if (key_code >= VKEY_F1 && key_code <= VKEY_F24) | |
225 return DomKey::FromCharacter(key_code - VKEY_F1 + DomKey::F1); | |
226 | |
227 // Other keys. | |
228 for (const auto& k : kKeyboardCodeToDomKey) { | |
229 if (k.key_code == key_code) | |
230 return (shift && k.shift) ? k.shift : k.plain; | |
231 } | |
232 return DomKey::UNIDENTIFIED; | |
233 } | |
234 | |
235 bool DomCodeToUsLayoutDomKey(DomCode dom_code, | 42 bool DomCodeToUsLayoutDomKey(DomCode dom_code, |
236 int flags, | 43 int flags, |
237 DomKey* out_dom_key, | 44 DomKey* out_dom_key, |
238 KeyboardCode* out_key_code) { | 45 KeyboardCode* out_key_code) { |
239 if ((flags & EF_CONTROL_DOWN) == EF_CONTROL_DOWN) { | 46 if ((flags & EF_CONTROL_DOWN) == EF_CONTROL_DOWN) { |
240 if (DomCodeToControlCharacter(dom_code, flags, out_dom_key, out_key_code)) { | 47 if (DomCodeToControlCharacter(dom_code, flags, out_dom_key, out_key_code)) { |
241 return true; | 48 return true; |
242 } | 49 } |
243 if (!IsModifierDomCode(dom_code)) { | 50 if (!IsModifierDomCode(dom_code)) { |
244 *out_dom_key = DomKey::UNIDENTIFIED; | 51 *out_dom_key = DomKey::UNIDENTIFIED; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 [](const DomCodeToKeyboardCodeEntry& a, DomCode b) { | 270 [](const DomCodeToKeyboardCodeEntry& a, DomCode b) { |
464 return static_cast<int>(a.dom_code) < static_cast<int>(b); | 271 return static_cast<int>(a.dom_code) < static_cast<int>(b); |
465 }); | 272 }); |
466 if ((found != end) && (found->dom_code == dom_code)) | 273 if ((found != end) && (found->dom_code == dom_code)) |
467 return found->key_code; | 274 return found->key_code; |
468 | 275 |
469 return VKEY_UNKNOWN; | 276 return VKEY_UNKNOWN; |
470 } | 277 } |
471 | 278 |
472 } // namespace ui | 279 } // namespace ui |
OLD | NEW |