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> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "grit/keyboard_resources.h" | 12 #include "grit/keyboard_resources.h" |
13 #include "grit/keyboard_resources_map.h" | 13 #include "grit/keyboard_resources_map.h" |
14 #include "ui/aura/client/aura_constants.h" | 14 #include "ui/aura/client/aura_constants.h" |
15 #include "ui/aura/root_window.h" | 15 #include "ui/aura/root_window.h" |
16 #include "ui/base/ime/input_method.h" | 16 #include "ui/base/ime/input_method.h" |
17 #include "ui/base/ime/text_input_client.h" | 17 #include "ui/base/ime/text_input_client.h" |
18 #include "ui/keyboard/keyboard_switches.h" | 18 #include "ui/keyboard/keyboard_switches.h" |
19 | 19 |
| 20 namespace { |
| 21 |
| 22 const char kKeyDown[] ="keydown"; |
| 23 const char kKeyUp[] = "keyup"; |
| 24 |
| 25 } // namespace |
| 26 |
20 namespace keyboard { | 27 namespace keyboard { |
21 | 28 |
22 bool IsKeyboardEnabled() { | 29 bool IsKeyboardEnabled() { |
23 return CommandLine::ForCurrentProcess()->HasSwitch( | 30 return CommandLine::ForCurrentProcess()->HasSwitch( |
24 switches::kEnableVirtualKeyboard); | 31 switches::kEnableVirtualKeyboard); |
25 } | 32 } |
26 | 33 |
27 bool InsertText(const base::string16& text, aura::RootWindow* root_window) { | 34 bool InsertText(const base::string16& text, aura::RootWindow* root_window) { |
28 if (!root_window) | 35 if (!root_window) |
29 return false; | 36 return false; |
30 | 37 |
31 // Handle Backspace and Enter specially: using TextInputClient::InsertText is | |
32 // very unreliable for these characters. | |
33 // TODO(bryeung): remove this code once virtual keyboards are able to send | |
34 // these events directly via the Input Injection API. | |
35 if (text.length() == 1) { | |
36 ui::KeyboardCode code = ui::VKEY_UNKNOWN; | |
37 if (text[0] == L'\n') | |
38 code = ui::VKEY_RETURN; | |
39 else if (text[0] == L'\b') | |
40 code = ui::VKEY_BACK; | |
41 | |
42 if (code != ui::VKEY_UNKNOWN) { | |
43 ui::KeyEvent press_event(ui::ET_KEY_PRESSED, code, 0, 0); | |
44 root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&press_event); | |
45 | |
46 ui::KeyEvent release_event(ui::ET_KEY_RELEASED, code, 0, 0); | |
47 root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&release_event); | |
48 | |
49 return true; | |
50 } | |
51 } | |
52 | |
53 ui::InputMethod* input_method = root_window->GetProperty( | 38 ui::InputMethod* input_method = root_window->GetProperty( |
54 aura::client::kRootWindowInputMethodKey); | 39 aura::client::kRootWindowInputMethodKey); |
55 if (!input_method) | 40 if (!input_method) |
56 return false; | 41 return false; |
57 | 42 |
58 ui::TextInputClient* tic = input_method->GetTextInputClient(); | 43 ui::TextInputClient* tic = input_method->GetTextInputClient(); |
59 if (!tic || tic->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 44 if (!tic || tic->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
60 return false; | 45 return false; |
61 | 46 |
62 tic->InsertText(text); | 47 tic->InsertText(text); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 // Then deal with the y movement. | 81 // Then deal with the y movement. |
97 if (codey != ui::VKEY_UNKNOWN) { | 82 if (codey != ui::VKEY_UNKNOWN) { |
98 ui::KeyEvent press_event(ui::ET_KEY_PRESSED, codey, modifier_flags, 0); | 83 ui::KeyEvent press_event(ui::ET_KEY_PRESSED, codey, modifier_flags, 0); |
99 root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&press_event); | 84 root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&press_event); |
100 ui::KeyEvent release_event(ui::ET_KEY_RELEASED, codey, modifier_flags, 0); | 85 ui::KeyEvent release_event(ui::ET_KEY_RELEASED, codey, modifier_flags, 0); |
101 root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&release_event); | 86 root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&release_event); |
102 } | 87 } |
103 return true; | 88 return true; |
104 } | 89 } |
105 | 90 |
| 91 bool SendKeyEvent(const std::string type, |
| 92 int key_value, |
| 93 int key_code, |
| 94 bool shift_modifier, |
| 95 aura::RootWindow* root_window) { |
| 96 ui::EventType event_type = ui::ET_UNKNOWN; |
| 97 if (type == kKeyDown) |
| 98 event_type = ui::ET_KEY_PRESSED; |
| 99 else if (type == kKeyUp) |
| 100 event_type = ui::ET_KEY_RELEASED; |
| 101 if (event_type == ui::ET_UNKNOWN) |
| 102 return false; |
| 103 |
| 104 int flags = ui::EF_NONE; |
| 105 if (shift_modifier) |
| 106 flags = ui::EF_SHIFT_DOWN; |
| 107 |
| 108 ui::KeyboardCode code = static_cast<ui::KeyboardCode>(key_code); |
| 109 |
| 110 ui::KeyEvent event(event_type, code, flags, false); |
| 111 event.set_character(key_value); |
| 112 event.set_unmodified_character(key_value); |
| 113 |
| 114 if (code != ui::VKEY_UNKNOWN) { |
| 115 root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&event); |
| 116 } else if (event_type == ui::ET_KEY_RELEASED) { |
| 117 // TODO(kevers): Fix key handling to support key_value when code is |
| 118 // VKEY_UNKNOWN. |
| 119 base::string16 text; |
| 120 text.push_back(static_cast<char16>(key_value)); |
| 121 InsertText(text, root_window); |
| 122 } |
| 123 return true; |
| 124 } |
| 125 |
106 const GritResourceMap* GetKeyboardExtensionResources(size_t* size) { | 126 const GritResourceMap* GetKeyboardExtensionResources(size_t* size) { |
107 // This looks a lot like the contents of a resource map; however it is | 127 // This looks a lot like the contents of a resource map; however it is |
108 // necessary to have a custom path for the extension path, so the resource | 128 // necessary to have a custom path for the extension path, so the resource |
109 // map cannot be used directly. | 129 // map cannot be used directly. |
110 static const GritResourceMap kKeyboardResources[] = { | 130 static const GritResourceMap kKeyboardResources[] = { |
111 {"keyboard/api_adapter.js", IDR_KEYBOARD_API_ADAPTER_JS}, | 131 {"keyboard/api_adapter.js", IDR_KEYBOARD_API_ADAPTER_JS}, |
112 {"keyboard/constants.js", IDR_KEYBOARD_CONSTANTS_JS}, | 132 {"keyboard/constants.js", IDR_KEYBOARD_CONSTANTS_JS}, |
113 {"keyboard/elements/kb-altkey.html", IDR_KEYBOARD_ELEMENTS_ALTKEY}, | 133 {"keyboard/elements/kb-altkey.html", IDR_KEYBOARD_ELEMENTS_ALTKEY}, |
114 {"keyboard/elements/kb-altkey-container.html", | 134 {"keyboard/elements/kb-altkey-container.html", |
115 IDR_KEYBOARD_ELEMENTS_ALTKEY_CONTAINER}, | 135 IDR_KEYBOARD_ELEMENTS_ALTKEY_CONTAINER}, |
(...skipping 26 matching lines...) Expand all Loading... |
142 {"keyboard/main.css", IDR_KEYBOARD_MAIN_CSS}, | 162 {"keyboard/main.css", IDR_KEYBOARD_MAIN_CSS}, |
143 {"keyboard/polymer.min.js", IDR_KEYBOARD_POLYMER}, | 163 {"keyboard/polymer.min.js", IDR_KEYBOARD_POLYMER}, |
144 {"keyboard/voice_input.js", IDR_KEYBOARD_VOICE_INPUT_JS}, | 164 {"keyboard/voice_input.js", IDR_KEYBOARD_VOICE_INPUT_JS}, |
145 }; | 165 }; |
146 static const size_t kKeyboardResourcesSize = arraysize(kKeyboardResources); | 166 static const size_t kKeyboardResourcesSize = arraysize(kKeyboardResources); |
147 *size = kKeyboardResourcesSize; | 167 *size = kKeyboardResourcesSize; |
148 return kKeyboardResources; | 168 return kKeyboardResources; |
149 } | 169 } |
150 | 170 |
151 } // namespace keyboard | 171 } // namespace keyboard |
OLD | NEW |