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
3 // found in the LICENSE file.
5 #include "ui/keyboard/keyboard_util.h"
9 #include "base/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/string16.h"
14 #include "grit/keyboard_resources.h"
15 #include "grit/keyboard_resources_map.h"
16 #include "ui/aura/client/aura_constants.h"
17 #include "ui/aura/window_tree_host.h"
18 #include "ui/base/ime/input_method.h"
19 #include "ui/base/ime/text_input_client.h"
20 #include "ui/events/event_processor.h"
21 #include "ui/keyboard/keyboard_switches.h"
26 const char kKeyDown
[] ="keydown";
27 const char kKeyUp
[] = "keyup";
29 void SendProcessKeyEvent(ui::EventType type
,
30 aura::WindowTreeHost
* host
) {
31 ui::KeyEvent
event(type
, ui::VKEY_PROCESSKEY
, ui::EF_NONE
);
32 event
.SetTranslated(true);
33 ui::EventDispatchDetails details
=
34 host
->event_processor()->OnEventFromSource(&event
);
35 CHECK(!details
.dispatcher_destroyed
);
38 base::LazyInstance
<base::Time
> g_keyboard_load_time_start
=
39 LAZY_INSTANCE_INITIALIZER
;
41 bool g_accessibility_keyboard_enabled
= false;
43 base::LazyInstance
<GURL
> g_override_content_url
= LAZY_INSTANCE_INITIALIZER
;
45 bool g_touch_keyboard_enabled
= false;
47 keyboard::KeyboardOverscrolOverride g_keyboard_overscroll_override
=
48 keyboard::KEYBOARD_OVERSCROLL_OVERRIDE_NONE
;
50 keyboard::KeyboardShowOverride g_keyboard_show_override
=
51 keyboard::KEYBOARD_SHOW_OVERRIDE_NONE
;
57 gfx::Rect
DefaultKeyboardBoundsFromWindowBounds(
58 const gfx::Rect
& window_bounds
) {
59 // Initialize default keyboard height to 0. The keyboard window height should
60 // only be set by window.resizeTo in virtual keyboard web contents. Otherwise,
61 // the default height may conflict with the new height and causing some
62 // strange animation issues.
63 return KeyboardBoundsFromWindowBounds(window_bounds
, 0);
66 gfx::Rect
KeyboardBoundsFromWindowBounds(const gfx::Rect
& window_bounds
,
67 int keyboard_height
) {
70 window_bounds
.bottom() - keyboard_height
,
71 window_bounds
.width(),
75 void SetAccessibilityKeyboardEnabled(bool enabled
) {
76 g_accessibility_keyboard_enabled
= enabled
;
79 bool GetAccessibilityKeyboardEnabled() {
80 return g_accessibility_keyboard_enabled
;
83 void SetTouchKeyboardEnabled(bool enabled
) {
84 g_touch_keyboard_enabled
= enabled
;
87 bool GetTouchKeyboardEnabled() {
88 return g_touch_keyboard_enabled
;
91 std::string
GetKeyboardLayout() {
92 // TODO(bshe): layout string is currently hard coded. We should use more
93 // standard keyboard layouts.
94 return GetAccessibilityKeyboardEnabled() ? "system-qwerty" : "qwerty";
97 bool IsKeyboardEnabled() {
98 // Accessibility setting prioritized over policy setting.
99 if (g_accessibility_keyboard_enabled
)
101 // Policy strictly disables showing a virtual keyboard.
102 if (g_keyboard_show_override
== keyboard::KEYBOARD_SHOW_OVERRIDE_DISABLED
)
104 // Check if any of the flags are enabled.
105 return CommandLine::ForCurrentProcess()->HasSwitch(
106 switches::kEnableVirtualKeyboard
) ||
107 g_touch_keyboard_enabled
||
108 (g_keyboard_show_override
== keyboard::KEYBOARD_SHOW_OVERRIDE_ENABLED
);
111 bool IsKeyboardOverscrollEnabled() {
112 if (!IsKeyboardEnabled())
115 // Users of the accessibility on-screen keyboard are likely to be using mouse
116 // input, which may interfere with overscrolling.
117 if (g_accessibility_keyboard_enabled
)
120 // If overscroll enabled override is set, use it instead. Currently
121 // login / out-of-box disable keyboard overscroll. http://crbug.com/363635
122 if (g_keyboard_overscroll_override
!= KEYBOARD_OVERSCROLL_OVERRIDE_NONE
) {
123 return g_keyboard_overscroll_override
==
124 KEYBOARD_OVERSCROLL_OVERRIDE_ENABLED
;
127 if (CommandLine::ForCurrentProcess()->HasSwitch(
128 switches::kDisableVirtualKeyboardOverscroll
)) {
134 void SetKeyboardOverscrollOverride(KeyboardOverscrolOverride override
) {
135 g_keyboard_overscroll_override
= override
;
138 void SetKeyboardShowOverride(KeyboardShowOverride override
) {
139 g_keyboard_show_override
= override
;
142 bool IsInputViewEnabled() {
143 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableInputView
))
145 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableInputView
))
147 // Default value if no command line flags specified.
151 bool IsExperimentalInputViewEnabled() {
152 if (CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kEnableExperimentalInputViewFeatures
)) {
159 bool InsertText(const base::string16
& text
, aura::Window
* root_window
) {
163 ui::InputMethod
* input_method
= root_window
->GetProperty(
164 aura::client::kRootWindowInputMethodKey
);
168 ui::TextInputClient
* tic
= input_method
->GetTextInputClient();
169 if (!tic
|| tic
->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE
)
172 tic
->InsertText(text
);
177 // TODO(varunjain): It would be cleaner to have something in the
178 // ui::TextInputClient interface, say MoveCaretInDirection(). The code in
179 // here would get the ui::InputMethod from the root_window, and the
180 // ui::TextInputClient from that (see above in InsertText()).
181 bool MoveCursor(int swipe_direction
,
183 aura::WindowTreeHost
* host
) {
186 ui::KeyboardCode codex
= ui::VKEY_UNKNOWN
;
187 ui::KeyboardCode codey
= ui::VKEY_UNKNOWN
;
188 if (swipe_direction
& kCursorMoveRight
)
189 codex
= ui::VKEY_RIGHT
;
190 else if (swipe_direction
& kCursorMoveLeft
)
191 codex
= ui::VKEY_LEFT
;
193 if (swipe_direction
& kCursorMoveUp
)
195 else if (swipe_direction
& kCursorMoveDown
)
196 codey
= ui::VKEY_DOWN
;
198 // First deal with the x movement.
199 if (codex
!= ui::VKEY_UNKNOWN
) {
200 ui::KeyEvent
press_event(ui::ET_KEY_PRESSED
, codex
, modifier_flags
);
201 ui::EventDispatchDetails details
=
202 host
->event_processor()->OnEventFromSource(&press_event
);
203 CHECK(!details
.dispatcher_destroyed
);
204 ui::KeyEvent
release_event(ui::ET_KEY_RELEASED
, codex
, modifier_flags
);
205 details
= host
->event_processor()->OnEventFromSource(&release_event
);
206 CHECK(!details
.dispatcher_destroyed
);
209 // Then deal with the y movement.
210 if (codey
!= ui::VKEY_UNKNOWN
) {
211 ui::KeyEvent
press_event(ui::ET_KEY_PRESSED
, codey
, modifier_flags
);
212 ui::EventDispatchDetails details
=
213 host
->event_processor()->OnEventFromSource(&press_event
);
214 CHECK(!details
.dispatcher_destroyed
);
215 ui::KeyEvent
release_event(ui::ET_KEY_RELEASED
, codey
, modifier_flags
);
216 details
= host
->event_processor()->OnEventFromSource(&release_event
);
217 CHECK(!details
.dispatcher_destroyed
);
222 bool SendKeyEvent(const std::string type
,
225 std::string key_name
,
227 aura::WindowTreeHost
* host
) {
228 ui::EventType event_type
= ui::ET_UNKNOWN
;
229 if (type
== kKeyDown
)
230 event_type
= ui::ET_KEY_PRESSED
;
231 else if (type
== kKeyUp
)
232 event_type
= ui::ET_KEY_RELEASED
;
233 if (event_type
== ui::ET_UNKNOWN
)
236 ui::KeyboardCode code
= static_cast<ui::KeyboardCode
>(key_code
);
238 if (code
== ui::VKEY_UNKNOWN
) {
239 // Handling of special printable characters (e.g. accented characters) for
240 // which there is no key code.
241 if (event_type
== ui::ET_KEY_RELEASED
) {
242 ui::InputMethod
* input_method
= host
->window()->GetProperty(
243 aura::client::kRootWindowInputMethodKey
);
247 ui::TextInputClient
* tic
= input_method
->GetTextInputClient();
249 SendProcessKeyEvent(ui::ET_KEY_PRESSED
, host
);
250 tic
->InsertChar(static_cast<uint16
>(key_value
), ui::EF_NONE
);
251 SendProcessKeyEvent(ui::ET_KEY_RELEASED
, host
);
254 if (event_type
== ui::ET_KEY_RELEASED
) {
255 // The number of key press events seen since the last backspace.
256 static int keys_seen
= 0;
257 if (code
== ui::VKEY_BACK
) {
258 // Log the rough lengths of characters typed between backspaces. This
259 // metric will be used to determine the error rate for the keyboard.
260 UMA_HISTOGRAM_CUSTOM_COUNTS(
261 "VirtualKeyboard.KeystrokesBetweenBackspaces",
262 keys_seen
, 1, 1000, 50);
269 ui::KeyEvent
event(event_type
, code
, key_name
, modifiers
);
270 ui::EventDispatchDetails details
=
271 host
->event_processor()->OnEventFromSource(&event
);
272 CHECK(!details
.dispatcher_destroyed
);
277 void MarkKeyboardLoadStarted() {
278 if (!g_keyboard_load_time_start
.Get().ToInternalValue())
279 g_keyboard_load_time_start
.Get() = base::Time::Now();
282 void MarkKeyboardLoadFinished() {
283 // Possible to get a load finished without a start if navigating directly to
284 // chrome://keyboard.
285 if (!g_keyboard_load_time_start
.Get().ToInternalValue())
288 // It should not be possible to finish loading the keyboard without starting
290 DCHECK(g_keyboard_load_time_start
.Get().ToInternalValue());
292 static bool logged
= false;
294 // Log the delta only once.
296 "VirtualKeyboard.FirstLoadTime",
297 base::Time::Now() - g_keyboard_load_time_start
.Get());
302 const GritResourceMap
* GetKeyboardExtensionResources(size_t* size
) {
303 // This looks a lot like the contents of a resource map; however it is
304 // necessary to have a custom path for the extension path, so the resource
305 // map cannot be used directly.
306 static const GritResourceMap kKeyboardResources
[] = {
307 {"keyboard/locales/en.js", IDR_KEYBOARD_LOCALES_EN
},
308 {"keyboard/config/emoji.js", IDR_KEYBOARD_CONFIG_EMOJI
},
309 {"keyboard/config/hwt.js", IDR_KEYBOARD_CONFIG_HWT
},
310 {"keyboard/config/us.js", IDR_KEYBOARD_CONFIG_US
},
311 {"keyboard/images/backspace.png", IDR_KEYBOARD_IMAGES_BACKSPACE
},
312 {"keyboard/images/check.png", IDR_KEYBOARD_IMAGES_CHECK
},
313 {"keyboard/images/compact.png", IDR_KEYBOARD_IMAGES_COMPACT
},
314 {"keyboard/images/down.png", IDR_KEYBOARD_IMAGES_DOWN
},
315 {"keyboard/images/enter.png", IDR_KEYBOARD_IMAGES_RETURN
},
316 {"keyboard/images/error.png", IDR_KEYBOARD_IMAGES_ERROR
},
317 {"keyboard/images/globe.png", IDR_KEYBOARD_IMAGES_GLOBE
},
318 {"keyboard/images/hide.png", IDR_KEYBOARD_IMAGES_HIDE_KEYBOARD
},
319 {"keyboard/images/keyboard.svg", IDR_KEYBOARD_IMAGES_KEYBOARD
},
320 {"keyboard/images/left.png", IDR_KEYBOARD_IMAGES_LEFT
},
321 {"keyboard/images/penci.png", IDR_KEYBOARD_IMAGES_PENCIL
},
322 {"keyboard/images/regular_size.png", IDR_KEYBOARD_IMAGES_FULLSIZE
},
323 {"keyboard/images/menu.png", IDR_KEYBOARD_IMAGES_MENU
},
324 {"keyboard/images/pencil.png", IDR_KEYBOARD_IMAGES_PENCIL
},
325 {"keyboard/images/right.png", IDR_KEYBOARD_IMAGES_RIGHT
},
326 {"keyboard/images/search.png", IDR_KEYBOARD_IMAGES_SEARCH
},
327 {"keyboard/images/setting.png", IDR_KEYBOARD_IMAGES_SETTINGS
},
328 {"keyboard/images/shift.png", IDR_KEYBOARD_IMAGES_SHIFT
},
329 {"keyboard/images/space.png", IDR_KEYBOARD_IMAGES_SPACE
},
330 {"keyboard/images/tab.png", IDR_KEYBOARD_IMAGES_TAB
},
331 {"keyboard/images/up.png", IDR_KEYBOARD_IMAGES_UP
},
332 {"keyboard/index.html", IDR_KEYBOARD_INDEX
},
333 {"keyboard/inputview_adapter.js", IDR_KEYBOARD_INPUTVIEW_ADAPTER
},
334 {"keyboard/inputview.css", IDR_KEYBOARD_INPUTVIEW_CSS
},
335 {"keyboard/inputview.js", IDR_KEYBOARD_INPUTVIEW_JS
},
336 {"keyboard/inputview_layouts/101kbd.js", IDR_KEYBOARD_LAYOUTS_101
},
337 {"keyboard/inputview_layouts/compactkbd-qwerty.js",
338 IDR_KEYBOARD_LAYOUTS_COMPACT_QWERTY
},
339 {"keyboard/inputview_layouts/compactkbd-numberpad.js",
340 IDR_KEYBOARD_LAYOUTS_COMPACT_NUMBERPAD
},
341 {"keyboard/inputview_layouts/emoji.js", IDR_KEYBOARD_LAYOUTS_EMOJI
},
342 {"keyboard/inputview_layouts/handwriting.js", IDR_KEYBOARD_LAYOUTS_HWT
},
343 {"keyboard/keyboard_mojo.js", IDR_KEYBOARD_MOJO_JS
},
344 {"keyboard/manifest.json", IDR_KEYBOARD_MANIFEST
},
345 {"keyboard/sounds/keypress-delete.wav",
346 IDR_KEYBOARD_SOUNDS_KEYPRESS_DELETE
},
347 {"keyboard/sounds/keypress-return.wav",
348 IDR_KEYBOARD_SOUNDS_KEYPRESS_RETURN
},
349 {"keyboard/sounds/keypress-spacebar.wav",
350 IDR_KEYBOARD_SOUNDS_KEYPRESS_SPACEBAR
},
351 {"keyboard/sounds/keypress-standard.wav",
352 IDR_KEYBOARD_SOUNDS_KEYPRESS_STANDARD
},
354 static const size_t kKeyboardResourcesSize
= arraysize(kKeyboardResources
);
355 *size
= kKeyboardResourcesSize
;
356 return kKeyboardResources
;
359 void SetOverrideContentUrl(const GURL
& url
) {
360 g_override_content_url
.Get() = url
;
363 const GURL
& GetOverrideContentUrl() {
364 return g_override_content_url
.Get();
367 void LogKeyboardControlEvent(KeyboardControlEvent event
) {
368 UMA_HISTOGRAM_ENUMERATION(
369 "VirtualKeyboard.KeyboardControlEvent",
371 keyboard::KEYBOARD_CONTROL_MAX
);
374 } // namespace keyboard