OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/dom/keycode_converter.h" | 5 #include "ui/events/keycodes/dom/keycode_converter.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversion_utils.h" |
8 #include "ui/events/keycodes/dom/dom_code.h" | 9 #include "ui/events/keycodes/dom/dom_code.h" |
9 #include "ui/events/keycodes/dom/dom_key.h" | 10 #include "ui/events/keycodes/dom/dom_key.h" |
10 | 11 |
11 namespace ui { | 12 namespace ui { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // Table of USB codes (equivalent to DomCode values), native scan codes, | 16 // Table of USB codes (equivalent to DomCode values), native scan codes, |
16 // and DOM Level 3 |code| strings. | 17 // and DOM Level 3 |code| strings. |
17 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
(...skipping 11 matching lines...) Expand all Loading... |
29 #undef USB_KEYMAP_DECLARATION | 30 #undef USB_KEYMAP_DECLARATION |
30 | 31 |
31 const size_t kKeycodeMapEntries = arraysize(usb_keycode_map); | 32 const size_t kKeycodeMapEntries = arraysize(usb_keycode_map); |
32 | 33 |
33 // Table of DomKey enum values and DOM Level 3 |key| strings. | 34 // Table of DomKey enum values and DOM Level 3 |key| strings. |
34 struct DomKeyMapEntry { | 35 struct DomKeyMapEntry { |
35 DomKey dom_key; | 36 DomKey dom_key; |
36 const char* string; | 37 const char* string; |
37 }; | 38 }; |
38 | 39 |
39 #define DOM_KEY_MAP(key, id) {DomKey::id, key} | |
40 #define DOM_KEY_MAP_DECLARATION const DomKeyMapEntry dom_key_map[] = | 40 #define DOM_KEY_MAP_DECLARATION const DomKeyMapEntry dom_key_map[] = |
| 41 #define DOM_KEY_UNI(key, id, value) {DomKey::id, key} |
| 42 #define DOM_KEY_MAP_BEGIN |
| 43 #define DOM_KEY_MAP(key, id) {DomKey::id, key} |
| 44 #define DOM_KEY_MAP_END |
41 #include "ui/events/keycodes/dom/dom_key_data.inc" | 45 #include "ui/events/keycodes/dom/dom_key_data.inc" |
| 46 #undef DOM_KEY_MAP_DECLARATION |
| 47 #undef DOM_KEY_MAP_BEGIN |
42 #undef DOM_KEY_MAP | 48 #undef DOM_KEY_MAP |
43 #undef DOM_KEY_MAP_DECLARATION | 49 #undef DOM_KEY_MAP_END |
| 50 #undef DOM_KEY_UNI |
44 | 51 |
45 const size_t kDomKeyMapEntries = arraysize(dom_key_map); | 52 const size_t kDomKeyMapEntries = arraysize(dom_key_map); |
46 | 53 |
47 } // namespace | 54 } // namespace |
48 | 55 |
49 // static | 56 // static |
50 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() { | 57 size_t KeycodeConverter::NumKeycodeMapEntriesForTest() { |
51 return kKeycodeMapEntries; | 58 return kKeycodeMapEntries; |
52 } | 59 } |
53 | 60 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 if (key.code == dom_code) | 166 if (key.code == dom_code) |
160 return key.location; | 167 return key.location; |
161 } | 168 } |
162 return DomKeyLocation::STANDARD; | 169 return DomKeyLocation::STANDARD; |
163 } | 170 } |
164 | 171 |
165 // static | 172 // static |
166 DomKey KeycodeConverter::KeyStringToDomKey(const char* key) { | 173 DomKey KeycodeConverter::KeyStringToDomKey(const char* key) { |
167 if (!key || !*key) | 174 if (!key || !*key) |
168 return DomKey::NONE; | 175 return DomKey::NONE; |
| 176 // Check for standard key names. |
169 for (size_t i = 0; i < kDomKeyMapEntries; ++i) { | 177 for (size_t i = 0; i < kDomKeyMapEntries; ++i) { |
170 if (dom_key_map[i].string && | 178 if (dom_key_map[i].string && strcmp(dom_key_map[i].string, key) == 0) { |
171 strcmp(dom_key_map[i].string, key) == 0) { | |
172 return dom_key_map[i].dom_key; | 179 return dom_key_map[i].dom_key; |
173 } | 180 } |
174 } | 181 } |
| 182 if (strcmp(key, "Dead") == 0) { |
| 183 // The web KeyboardEvent string does not encode the combining character, |
| 184 // so we just set it to the Unicode designated non-character 0xFFFF. |
| 185 // This will round-trip convert back to 'Dead' but take no part in |
| 186 // character composition. |
| 187 return DomKey::DeadKeyFromCombiningCharacter(0xFFFF); |
| 188 } |
| 189 // Otherwise, if the string contains a single Unicode character, |
| 190 // the key value is that character. |
| 191 int32_t char_index = 0; |
| 192 uint32_t character; |
| 193 if (base::ReadUnicodeCharacter(key, static_cast<int32_t>(strlen(key)), |
| 194 &char_index, &character) && |
| 195 key[++char_index] == 0) { |
| 196 return DomKey::FromCharacter(character); |
| 197 } |
175 return DomKey::NONE; | 198 return DomKey::NONE; |
176 } | 199 } |
177 | 200 |
178 // static | 201 // static |
179 const char* KeycodeConverter::DomKeyToKeyString(DomKey dom_key) { | 202 std::string KeycodeConverter::DomKeyToKeyString(DomKey dom_key) { |
| 203 if (dom_key.IsDeadKey()) { |
| 204 // All dead-key combining codes collapse to 'Dead', as UI Events |
| 205 // KeyboardEvent represents the combining character separately. |
| 206 return "Dead"; |
| 207 } |
180 for (size_t i = 0; i < kDomKeyMapEntries; ++i) { | 208 for (size_t i = 0; i < kDomKeyMapEntries; ++i) { |
181 if (dom_key_map[i].dom_key == dom_key) | 209 if (dom_key_map[i].dom_key == dom_key) { |
182 return dom_key_map[i].string; | 210 if (dom_key_map[i].string) |
| 211 return dom_key_map[i].string; |
| 212 break; |
| 213 } |
183 } | 214 } |
184 return ""; | 215 if (dom_key.IsCharacter()) { |
| 216 std::string s; |
| 217 base::WriteUnicodeCharacter(dom_key.ToCharacter(), &s); |
| 218 return s; |
| 219 } |
| 220 return std::string(); |
185 } | 221 } |
186 | 222 |
187 // static | 223 // static |
188 bool KeycodeConverter::IsDomKeyForModifier(DomKey dom_key) { | 224 bool KeycodeConverter::IsDomKeyForModifier(DomKey dom_key) { |
189 switch (dom_key) { | 225 switch (dom_key) { |
190 case DomKey::ACCEL: | 226 case DomKey::ACCEL: |
191 case DomKey::ALT: | 227 case DomKey::ALT: |
192 case DomKey::ALT_GRAPH: | 228 case DomKey::ALT_GRAPH: |
193 case DomKey::CAPS_LOCK: | 229 case DomKey::CAPS_LOCK: |
194 case DomKey::CONTROL: | 230 case DomKey::CONTROL: |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { | 307 for (size_t i = 0; i < kKeycodeMapEntries; ++i) { |
272 if (usb_keycode_map[i].code && | 308 if (usb_keycode_map[i].code && |
273 strcmp(usb_keycode_map[i].code, code) == 0) { | 309 strcmp(usb_keycode_map[i].code, code) == 0) { |
274 return usb_keycode_map[i].usb_keycode; | 310 return usb_keycode_map[i].usb_keycode; |
275 } | 311 } |
276 } | 312 } |
277 return InvalidUsbKeycode(); | 313 return InvalidUsbKeycode(); |
278 } | 314 } |
279 | 315 |
280 } // namespace ui | 316 } // namespace ui |
OLD | NEW |