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/dom3/dom_code.h" | 10 #include "ui/events/keycodes/dom3/dom_code.h" |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 [](const DomCodeToKeyboardCodeEntry& a, DomCode b) { | 151 [](const DomCodeToKeyboardCodeEntry& a, DomCode b) { |
152 return static_cast<int>(a.dom_code) < static_cast<int>(b); | 152 return static_cast<int>(a.dom_code) < static_cast<int>(b); |
153 }); | 153 }); |
154 if ((found != end) && (found->dom_code == dom_code)) | 154 if ((found != end) && (found->dom_code == dom_code)) |
155 return found->key_code; | 155 return found->key_code; |
156 return VKEY_UNKNOWN; | 156 return VKEY_UNKNOWN; |
157 } | 157 } |
158 | 158 |
159 } // anonymous namespace | 159 } // anonymous namespace |
160 | 160 |
161 base::char16 GetCharacterFromKeyCode(KeyboardCode key_code, int flags) { | 161 base::char16 DomCodeToUsLayoutCharacter(DomCode dom_code, int flags) { |
162 ui::DomKey dom_key; | |
163 base::char16 character; | 162 base::char16 character; |
164 if (GetMeaningFromKeyCode(key_code, flags, &dom_key, &character)) | 163 DomKey dom_key; |
| 164 KeyboardCode key_code; |
| 165 if (DomCodeToUsLayoutMeaning(dom_code, flags, &dom_key, &character, |
| 166 &key_code)) |
165 return character; | 167 return character; |
166 return 0; | 168 return 0; |
167 } | 169 } |
168 | 170 |
169 bool GetMeaningFromKeyCode(KeyboardCode key_code, | |
170 int flags, | |
171 DomKey* dom_key, | |
172 base::char16* character) { | |
173 const bool ctrl = (flags & EF_CONTROL_DOWN) != 0; | |
174 const bool shift = (flags & EF_SHIFT_DOWN) != 0; | |
175 const bool upper = shift ^ ((flags & EF_CAPS_LOCK_DOWN) != 0); | |
176 | |
177 // Control characters. | |
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. | |
200 if (shift) { | |
201 // The following graphics characters require the shift key to input. | |
202 switch (key_code) { | |
203 // ctrl-@ maps to \x00 (Null byte) | |
204 case VKEY_2: | |
205 *dom_key = DomKey::CHARACTER; | |
206 *character = 0; | |
207 return true; | |
208 // ctrl-^ maps to \x1E (Record separator, Information separator two) | |
209 case VKEY_6: | |
210 *dom_key = DomKey::CHARACTER; | |
211 *character = 0x1E; | |
212 return true; | |
213 // ctrl-_ maps to \x1F (Unit separator, Information separator one) | |
214 case VKEY_OEM_MINUS: | |
215 *dom_key = DomKey::CHARACTER; | |
216 *character = 0x1F; | |
217 return true; | |
218 // Returns 0 for all other keys to avoid inputting unexpected chars. | |
219 default: | |
220 *dom_key = DomKey::UNIDENTIFIED; | |
221 *character = 0; | |
222 return false; | |
223 } | |
224 } else { | |
225 switch (key_code) { | |
226 // ctrl-[ maps to \x1B (Escape) | |
227 case VKEY_OEM_4: | |
228 *dom_key = DomKey::ESCAPE; | |
229 *character = 0x1B; | |
230 return true; | |
231 // ctrl-\ maps to \x1C (File separator, Information separator four) | |
232 case VKEY_OEM_5: | |
233 *dom_key = DomKey::CHARACTER; | |
234 *character = 0x1C; | |
235 return true; | |
236 // ctrl-] maps to \x1D (Group separator, Information separator three) | |
237 case VKEY_OEM_6: | |
238 *dom_key = DomKey::CHARACTER; | |
239 *character = 0x1D; | |
240 return true; | |
241 // ctrl-Enter maps to \x0A (Line feed) | |
242 case VKEY_RETURN: | |
243 *dom_key = DomKey::CHARACTER; | |
244 *character = 0x0A; | |
245 return true; | |
246 // Returns 0 for all other keys to avoid inputting unexpected chars. | |
247 default: | |
248 *dom_key = DomKey::UNIDENTIFIED; | |
249 *character = 0; | |
250 return false; | |
251 } | |
252 } | |
253 } | |
254 | |
255 // ASCII alphanumeric characters. | |
256 if (key_code >= VKEY_A && key_code <= VKEY_Z) { | |
257 *dom_key = DomKey::CHARACTER; | |
258 *character = static_cast<uint16>(key_code - VKEY_A + (upper ? 'A' : 'a')); | |
259 return true; | |
260 } | |
261 if (key_code >= VKEY_0 && key_code <= VKEY_9) { | |
262 *dom_key = DomKey::CHARACTER; | |
263 *character = | |
264 shift ? ")!@#$%^&*("[key_code - VKEY_0] : static_cast<uint16>(key_code); | |
265 return true; | |
266 } | |
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, | 171 bool DomCodeToUsLayoutMeaning(DomCode dom_code, |
297 int flags, | 172 int flags, |
298 DomKey* out_dom_key, | 173 DomKey* out_dom_key, |
299 base::char16* out_character, | 174 base::char16* out_character, |
300 KeyboardCode* out_key_code) { | 175 KeyboardCode* out_key_code) { |
301 if ((flags & EF_CONTROL_DOWN) == EF_CONTROL_DOWN) { | 176 if ((flags & EF_CONTROL_DOWN) == EF_CONTROL_DOWN) { |
302 if (DomCodeToControlCharacter(dom_code, flags, out_dom_key, out_character, | 177 if (DomCodeToControlCharacter(dom_code, flags, out_dom_key, out_character, |
303 out_key_code)) { | 178 out_key_code)) { |
304 return true; | 179 return true; |
305 } | 180 } |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 return it.dom_code; | 396 return it.dom_code; |
522 } | 397 } |
523 for (const auto& it : kFallbackKeyboardCodeToDomCodeMap) { | 398 for (const auto& it : kFallbackKeyboardCodeToDomCodeMap) { |
524 if (it.key_code == key_code) | 399 if (it.key_code == key_code) |
525 return it.dom_code; | 400 return it.dom_code; |
526 } | 401 } |
527 return DomCode::NONE; | 402 return DomCode::NONE; |
528 } | 403 } |
529 | 404 |
530 } // namespace ui | 405 } // namespace ui |
OLD | NEW |