1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/keyboard/keyboard_util.h"
#include <string>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/string16.h"
#include "grit/keyboard_resources.h"
#include "grit/keyboard_resources_map.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/root_window.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/keyboard/keyboard_switches.h"
namespace {
const char kKeyDown[] ="keydown";
const char kKeyUp[] = "keyup";
void SendProcessKeyEvent(ui::EventType type, aura::RootWindow* root_window) {
ui::TranslatedKeyEvent event(type == ui::ET_KEY_PRESSED,
ui::VKEY_PROCESSKEY,
ui::EF_NONE);
root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&event);
}
} // namespace
namespace keyboard {
bool IsKeyboardEnabled() {
return CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableVirtualKeyboard);
}
bool InsertText(const base::string16& text, aura::RootWindow* root_window) {
if (!root_window)
return false;
ui::InputMethod* input_method = root_window->GetProperty(
aura::client::kRootWindowInputMethodKey);
if (!input_method)
return false;
ui::TextInputClient* tic = input_method->GetTextInputClient();
if (!tic || tic->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
return false;
tic->InsertText(text);
return true;
}
// TODO(varunjain): It would be cleaner to have something in the
// ui::TextInputClient interface, say MoveCaretInDirection(). The code in
// here would get the ui::InputMethod from the root_window, and the
// ui::TextInputClient from that (see above in InsertText()).
bool MoveCursor(int swipe_direction,
int modifier_flags,
aura::RootWindow* root_window) {
if (!root_window)
return false;
ui::KeyboardCode codex = ui::VKEY_UNKNOWN;
ui::KeyboardCode codey = ui::VKEY_UNKNOWN;
if (swipe_direction & kCursorMoveRight)
codex = ui::VKEY_RIGHT;
else if (swipe_direction & kCursorMoveLeft)
codex = ui::VKEY_LEFT;
if (swipe_direction & kCursorMoveUp)
codey = ui::VKEY_UP;
else if (swipe_direction & kCursorMoveDown)
codey = ui::VKEY_DOWN;
// First deal with the x movement.
if (codex != ui::VKEY_UNKNOWN) {
ui::KeyEvent press_event(ui::ET_KEY_PRESSED, codex, modifier_flags, 0);
root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&press_event);
ui::KeyEvent release_event(ui::ET_KEY_RELEASED, codex, modifier_flags, 0);
root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&release_event);
}
// Then deal with the y movement.
if (codey != ui::VKEY_UNKNOWN) {
ui::KeyEvent press_event(ui::ET_KEY_PRESSED, codey, modifier_flags, 0);
root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&press_event);
ui::KeyEvent release_event(ui::ET_KEY_RELEASED, codey, modifier_flags, 0);
root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&release_event);
}
return true;
}
bool SendKeyEvent(const std::string type,
int key_value,
int key_code,
bool shift_modifier,
aura::RootWindow* root_window) {
ui::EventType event_type = ui::ET_UNKNOWN;
if (type == kKeyDown)
event_type = ui::ET_KEY_PRESSED;
else if (type == kKeyUp)
event_type = ui::ET_KEY_RELEASED;
if (event_type == ui::ET_UNKNOWN)
return false;
int flags = ui::EF_NONE;
if (shift_modifier)
flags = ui::EF_SHIFT_DOWN;
ui::KeyboardCode code = static_cast<ui::KeyboardCode>(key_code);
if (code == ui::VKEY_UNKNOWN) {
// Handling of special printable characters (e.g. accented characters) for
// which there is no key code.
if (event_type == ui::ET_KEY_RELEASED) {
ui::InputMethod* input_method = root_window->GetProperty(
aura::client::kRootWindowInputMethodKey);
if (!input_method)
return false;
ui::TextInputClient* tic = input_method->GetTextInputClient();
SendProcessKeyEvent(ui::ET_KEY_PRESSED, root_window);
tic->InsertChar(static_cast<uint16>(key_value), ui::EF_NONE);
SendProcessKeyEvent(ui::ET_KEY_RELEASED, root_window);
}
} else {
if (event_type == ui::ET_KEY_RELEASED) {
// The number of key press events seen since the last backspace.
static int keys_seen = 0;
if (code == ui::VKEY_BACK) {
// Log the rough lengths of characters typed between backspaces. This
// metric will be used to determine the error rate for the keyboard.
UMA_HISTOGRAM_CUSTOM_COUNTS(
"VirtualKeyboard.KeystrokesBetweenBackspaces",
keys_seen, 1, 1000, 50);
keys_seen = 0;
} else {
++keys_seen;
}
}
ui::KeyEvent event(event_type, code, flags, false);
root_window->AsRootWindowHostDelegate()->OnHostKeyEvent(&event);
}
return true;
}
const GritResourceMap* GetKeyboardExtensionResources(size_t* size) {
// This looks a lot like the contents of a resource map; however it is
// necessary to have a custom path for the extension path, so the resource
// map cannot be used directly.
static const GritResourceMap kKeyboardResources[] = {
{"keyboard/api_adapter.js", IDR_KEYBOARD_API_ADAPTER_JS},
{"keyboard/constants.js", IDR_KEYBOARD_CONSTANTS_JS},
{"keyboard/elements/kb-altkey.html", IDR_KEYBOARD_ELEMENTS_ALTKEY},
{"keyboard/elements/kb-altkey-container.html",
IDR_KEYBOARD_ELEMENTS_ALTKEY_CONTAINER},
{"keyboard/elements/kb-altkey-data.html",
IDR_KEYBOARD_ELEMENTS_ALTKEY_DATA},
{"keyboard/elements/kb-altkey-set.html", IDR_KEYBOARD_ELEMENTS_ALTKEY_SET},
{"keyboard/elements/kb-key.html", IDR_KEYBOARD_ELEMENTS_KEY},
{"keyboard/elements/kb-key-base.html", IDR_KEYBOARD_ELEMENTS_KEY_BASE},
{"keyboard/elements/kb-key-codes.html", IDR_KEYBOARD_ELEMENTS_KEY_CODES},
{"keyboard/elements/kb-key-import.html",
IDR_KEYBOARD_ELEMENTS_KEY_IMPORT},
{"keyboard/elements/kb-key-sequence.html",
IDR_KEYBOARD_ELEMENTS_KEY_SEQUENCE},
{"keyboard/elements/kb-keyboard.html", IDR_KEYBOARD_ELEMENTS_KEYBOARD},
{"keyboard/elements/kb-keyset.html", IDR_KEYBOARD_ELEMENTS_KEYSET},
{"keyboard/elements/kb-row.html", IDR_KEYBOARD_ELEMENTS_ROW},
{"keyboard/elements/kb-shift-key.html", IDR_KEYBOARD_ELEMENTS_SHIFT_KEY},
{"keyboard/images/keyboard.svg", IDR_KEYBOARD_IMAGES_KEYBOARD},
{"keyboard/images/microphone.svg", IDR_KEYBOARD_IMAGES_MICROPHONE},
{"keyboard/images/microphone-green.svg",
IDR_KEYBOARD_IMAGES_MICROPHONE_GREEN},
{"keyboard/index.html", IDR_KEYBOARD_INDEX},
{"keyboard/layouts/dvorak.html", IDR_KEYBOARD_LAYOUTS_DVORAK},
{"keyboard/layouts/latin-accents.js", IDR_KEYBOARD_LAYOUTS_LATIN_ACCENTS},
{"keyboard/layouts/numeric.html", IDR_KEYBOARD_LAYOUTS_NUMERIC},
{"keyboard/layouts/qwerty.html", IDR_KEYBOARD_LAYOUTS_QWERTY},
{"keyboard/layouts/symbol-altkeys.js",
IDR_KEYBOARD_LAYOUTS_SYMBOL_ALTKEYS},
{"keyboard/layouts/spacebar-row.html", IDR_KEYBOARD_SPACEBAR_ROW},
{"keyboard/main.js", IDR_KEYBOARD_MAIN_JS},
{"keyboard/manifest.json", IDR_KEYBOARD_MANIFEST},
{"keyboard/main.css", IDR_KEYBOARD_MAIN_CSS},
{"keyboard/polymer.min.js", IDR_KEYBOARD_POLYMER},
{"keyboard/voice_input.js", IDR_KEYBOARD_VOICE_INPUT_JS},
};
static const size_t kKeyboardResourcesSize = arraysize(kKeyboardResources);
*size = kKeyboardResourcesSize;
return kKeyboardResources;
}
} // namespace keyboard
|