OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/keyboard/keyboard_util.h" | 5 #include "ui/keyboard/keyboard_util.h" |
6 | 6 |
| 7 #include <string> |
| 8 |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/values.h" |
| 14 #include "ui/base/events/event.h" |
| 15 #include "ui/base/events/event_constants.h" |
| 16 #include "ui/base/events/key_identifier_conversion.h" |
8 #include "ui/keyboard/keyboard_switches.h" | 17 #include "ui/keyboard/keyboard_switches.h" |
9 | 18 |
| 19 namespace { |
| 20 |
| 21 // KeyEvent dictionary keys |
| 22 const char kType[] = "type"; |
| 23 const char kKeyIdentifier[] = "keyIdentifier"; |
| 24 const char kAlt[] = "altKey"; |
| 25 const char kCtrl[] = "ctrlKey"; |
| 26 const char kMeta[] = "metaKey"; |
| 27 const char kShift[] = "shiftKey"; |
| 28 const char kKeyDown[] = "keydown"; |
| 29 const char kKeyUp[] = "keyup"; |
| 30 |
| 31 // Errors. |
| 32 const char kInvalidArgumentsListError[] = |
| 33 "Argument list does not contain a dictionary."; |
| 34 const char kInvalidKeyEventMissingKeyIdentifierError[] = |
| 35 "KeyEvent object is missing the keyIdentifier field"; |
| 36 const char kInvalidKeyEventMissingTypeError[] = |
| 37 "KeyEvent object is missing the type field"; |
| 38 const char kUnknownKeyEventTypeError[] = |
| 39 "Unknown event type in KeyEvent."; |
| 40 const char kUnknownOrUnsupportedKeyIdentiferError[] = |
| 41 "Unknown or unsupported key identifier."; |
| 42 const char kUnsupportedModifierError[] = |
| 43 "Unsupported modifier (meta)."; |
| 44 |
| 45 ui::EventType GetTypeFromString(const std::string& type) { |
| 46 if (type == kKeyDown) { |
| 47 return ui::ET_KEY_PRESSED; |
| 48 } else if (type == kKeyUp) { |
| 49 return ui::ET_KEY_RELEASED; |
| 50 } |
| 51 return ui::ET_UNKNOWN; |
| 52 } |
| 53 |
| 54 // Converts a hex string "U+NNNN" to uint16. Returns 0 on error. |
| 55 uint16 UnicodeIdentifierStringToInt(const std::string& key_identifier) { |
| 56 int character = 0; |
| 57 if ((key_identifier.length() == 6) && |
| 58 (key_identifier.substr(0, 2) == "U+") && |
| 59 (key_identifier.substr(2).find_first_not_of("0123456789abcdefABCDEF") == |
| 60 std::string::npos)) { |
| 61 const bool result = |
| 62 base::HexStringToInt(key_identifier.substr(2), &character); |
| 63 DCHECK(result) << key_identifier; |
| 64 } |
| 65 return character; |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
10 namespace keyboard { | 70 namespace keyboard { |
11 | 71 |
12 bool IsKeyboardEnabled() { | 72 bool IsKeyboardEnabled() { |
13 return CommandLine::ForCurrentProcess()->HasSwitch( | 73 return CommandLine::ForCurrentProcess()->HasSwitch( |
14 switches::kEnableVirtualKeyboard); | 74 switches::kEnableVirtualKeyboard); |
15 } | 75 } |
16 | 76 |
| 77 ui::KeyEvent* KeyEventFromArgs(const base::ListValue* args, |
| 78 std::string* error) { |
| 79 const DictionaryValue* key_event; |
| 80 if (!args->GetDictionary(0, &key_event)) { |
| 81 *error = kInvalidArgumentsListError; |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 std::string type_name; |
| 86 if (!key_event->GetString(kType, &type_name)) { |
| 87 *error = kInvalidKeyEventMissingTypeError; |
| 88 return NULL; |
| 89 } |
| 90 |
| 91 ui::EventType type = GetTypeFromString(type_name); |
| 92 if (type == ui::ET_UNKNOWN) { |
| 93 *error = kUnknownKeyEventTypeError; |
| 94 return NULL; |
| 95 } |
| 96 |
| 97 std::string identifier; |
| 98 if (!key_event->GetString(kKeyIdentifier, &identifier)) { |
| 99 *error = kInvalidKeyEventMissingKeyIdentifierError; |
| 100 return NULL; |
| 101 } |
| 102 TrimWhitespaceASCII(identifier, TRIM_ALL, &identifier); |
| 103 |
| 104 const ui::KeyEvent& prototype_event = |
| 105 ui::KeyEventFromKeyIdentifier(identifier); |
| 106 uint16 character = 0; |
| 107 if (prototype_event.key_code() == ui::VKEY_UNKNOWN) { |
| 108 character = UnicodeIdentifierStringToInt(identifier); |
| 109 if (!character) { |
| 110 *error = kUnknownOrUnsupportedKeyIdentiferError; |
| 111 return NULL; |
| 112 } |
| 113 } |
| 114 |
| 115 int flags = 0; |
| 116 if (prototype_event.key_code() != ui::VKEY_UNKNOWN) |
| 117 flags = prototype_event.flags(); |
| 118 |
| 119 bool flag = false; |
| 120 if (key_event->GetBoolean(kAlt, &flag) && flag) |
| 121 flags |= ui::EF_ALT_DOWN; |
| 122 if (key_event->GetBoolean(kCtrl, &flag) && flag) |
| 123 flags |= ui::EF_CONTROL_DOWN; |
| 124 if (key_event->GetBoolean(kShift, &flag) && flag) |
| 125 flags |= ui::EF_SHIFT_DOWN; |
| 126 if (key_event->GetBoolean(kMeta, &flag) && flag) { |
| 127 // ui::KeyEvent does not have a Meta flag, so return an error for now. |
| 128 *error = kUnsupportedModifierError; |
| 129 return NULL; |
| 130 } |
| 131 |
| 132 ui::KeyEvent* event = new ui::KeyEvent( |
| 133 type, prototype_event.key_code(), flags, prototype_event.is_char()); |
| 134 if (character) { |
| 135 event->set_character(character); |
| 136 event->set_unmodified_character(character); |
| 137 } |
| 138 |
| 139 return event; |
| 140 } |
| 141 |
17 } // namespace keyboard | 142 } // namespace keyboard |
OLD | NEW |