Commit 6974908e authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

[Switch Access] Create EventHelper to handle synthetic key/mouse events

Bug: None
Change-Id: Icf3fa74c760378f73b536fbf59e224cc879832e5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1830140
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Reviewed-by: default avatarAkihiro Ota <akihiroota@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701717}
parent ae0aee38
...@@ -36,6 +36,7 @@ run_jsbundler("switch_access_copied_files") { ...@@ -36,6 +36,7 @@ run_jsbundler("switch_access_copied_files") {
"back_button_manager.js", "back_button_manager.js",
"background.js", "background.js",
"commands.js", "commands.js",
"event_helper.js",
"focus_ring_manager.js", "focus_ring_manager.js",
"icons/back.svg", "icons/back.svg",
"icons/copy.svg", "icons/copy.svg",
...@@ -175,6 +176,7 @@ js_type_check("closure_compile") { ...@@ -175,6 +176,7 @@ js_type_check("closure_compile") {
":back_button_manager", ":back_button_manager",
":background", ":background",
":commands", ":commands",
":event_helper",
":focus_ring_manager", ":focus_ring_manager",
":menu_manager", ":menu_manager",
":menu_panel", ":menu_panel",
...@@ -203,6 +205,7 @@ js_library("auto_scan_manager") { ...@@ -203,6 +205,7 @@ js_library("auto_scan_manager") {
js_library("navigation_manager") { js_library("navigation_manager") {
deps = [ deps = [
":back_button_manager", ":back_button_manager",
":event_helper",
":focus_ring_manager", ":focus_ring_manager",
":menu_manager", ":menu_manager",
":menu_panel_interface", ":menu_panel_interface",
...@@ -244,6 +247,10 @@ js_library("commands") { ...@@ -244,6 +247,10 @@ js_library("commands") {
] ]
} }
js_library("event_helper") {
externs_list = [ "$externs_path/automation.js" ]
}
js_library("focus_ring_manager") { js_library("focus_ring_manager") {
deps = [ deps = [
":back_button_manager", ":back_button_manager",
...@@ -309,11 +316,15 @@ js_library("switch_access_predicate") { ...@@ -309,11 +316,15 @@ js_library("switch_access_predicate") {
} }
js_library("text_input_manager") { js_library("text_input_manager") {
deps = [
":event_helper",
]
externs_list = [ "$externs_path/accessibility_private.js" ] externs_list = [ "$externs_path/accessibility_private.js" ]
} }
js_library("text_navigation_manager") { js_library("text_navigation_manager") {
deps = [ deps = [
":event_helper",
":switch_access_constants", ":switch_access_constants",
] ]
externs_list = [ "$externs_path/accessibility_private.js" ] externs_list = [ "$externs_path/accessibility_private.js" ]
......
// Copyright 2019 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.
/** Functions to send synthetic key and mouse events. */
const EventHelper = {
/**
* Sends a single key stroke (down and up) with the given key code and
* keyboard modifiers (whether or not CTRL, ALT, SEARCH, and SHIFT are
* being held).
* @param {!EventHelper.KeyCode} keyCode
* @param {!chrome.accessibilityPrivate.SyntheticKeyboardModifiers} modifiers
*/
simulateKeyPress: (keyCode, modifiers = {}) => {
let type = chrome.accessibilityPrivate.SyntheticKeyboardEventType.KEYDOWN;
chrome.accessibilityPrivate.sendSyntheticKeyEvent(
{type, keyCode, modifiers});
type = chrome.accessibilityPrivate.SyntheticKeyboardEventType.KEYUP;
chrome.accessibilityPrivate.sendSyntheticKeyEvent(
{type, keyCode, modifiers});
},
/**
* Sends a synthetic mouse event.
* @param {number} x
* @param {number} y
* @param {number=} delayMs The delay between mouse press and mouse release,
* in milliseconds.
*/
simulateMouseClick: (x, y, delayMs) => {
let type = chrome.accessibilityPrivate.SyntheticMouseEventType.PRESS;
chrome.accessibilityPrivate.sendSyntheticMouseEvent({type, x, y});
let callback = () => {
type = chrome.accessibilityPrivate.SyntheticMouseEventType.RELEASE;
chrome.accessibilityPrivate.sendSyntheticMouseEvent({type, x, y});
};
if (delayMs) {
setTimeout(callback, delayMs);
} else {
callback();
}
},
/**
* Defines the key codes for specified keys.
* @enum {number}
* @const
*/
KeyCode: {
ESC: 27,
END: 35,
HOME: 36,
LEFT_ARROW: 37,
UP_ARROW: 38,
RIGHT_ARROW: 39,
DOWN_ARROW: 40,
C: 67,
V: 86,
X: 88
}
};
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
"closure_shim.js", "closure_shim.js",
"commands.js", "commands.js",
"constants.js", "constants.js",
"event_helper.js",
"focus_ring_manager.js", "focus_ring_manager.js",
"menu_manager.js", "menu_manager.js",
"navigation_manager.js", "navigation_manager.js",
......
...@@ -247,29 +247,6 @@ class NavigationManager { ...@@ -247,29 +247,6 @@ class NavigationManager {
console.log('Unrecognized scroll action: ', scrollAction); console.log('Unrecognized scroll action: ', scrollAction);
} }
/**
* Simulates a single key stroke with the given key code
* and keyboard modifiers (whether or not CTRL, ALT, SEARCH,
* SHIFT are being held).
*
* @param {number} keyCode
* @param {!chrome.accessibilityPrivate.SyntheticKeyboardModifiers} modifiers
* @public
*/
simulateKeyPress(keyCode, modifiers) {
chrome.accessibilityPrivate.sendSyntheticKeyEvent({
type: chrome.accessibilityPrivate.SyntheticKeyboardEventType.KEYDOWN,
keyCode: keyCode,
modifiers: modifiers
});
chrome.accessibilityPrivate.sendSyntheticKeyEvent({
type: chrome.accessibilityPrivate.SyntheticKeyboardEventType.KEYUP,
keyCode: keyCode,
modifiers: modifiers
});
}
/** /**
* Moves the text caret to the beginning of the current node. * Moves the text caret to the beginning of the current node.
* @public * @public
...@@ -553,14 +530,7 @@ class NavigationManager { ...@@ -553,14 +530,7 @@ class NavigationManager {
* Closes a system menu when the back button is pressed. * Closes a system menu when the back button is pressed.
*/ */
closeSystemMenu_() { closeSystemMenu_() {
chrome.accessibilityPrivate.sendSyntheticKeyEvent({ EventHelper.simulateKeyPress(EventHelper.KeyCode.ESC);
type: chrome.accessibilityPrivate.SyntheticKeyboardEventType.KEYDOWN,
keyCode: 27 // Esc
});
chrome.accessibilityPrivate.sendSyntheticKeyEvent({
type: chrome.accessibilityPrivate.SyntheticKeyboardEventType.KEYUP,
keyCode: 27 // Esc
});
} }
/** /**
......
...@@ -90,7 +90,7 @@ SAConstants.Focus.BUFFER = 4; ...@@ -90,7 +90,7 @@ SAConstants.Focus.BUFFER = 4;
* @type {number} * @type {number}
* @const * @const
*/ */
SAConstants.KEY_PRESS_DURATION_MS = 100; SAConstants.VK_KEY_PRESS_DURATION_MS = 100;
/** /**
* Preferences that are configurable in Switch Access. * Preferences that are configurable in Switch Access.
...@@ -175,22 +175,3 @@ SAConstants.EMPTY_LOCATION = { ...@@ -175,22 +175,3 @@ SAConstants.EMPTY_LOCATION = {
width: 0, width: 0,
height: 0 height: 0
}; };
/**
* Defines the key codes of all key events to be sent.
* Currently used for text navigation actions and cut/copy/paste.
* @enum {number}
* @const
*/
SAConstants.KeyCode = {
END: 35,
HOME: 36,
LEFT_ARROW: 37,
UP_ARROW: 38,
RIGHT_ARROW: 39,
DOWN_ARROW: 40,
// Key codes for X, C, V used for cut, copy, and paste synthetic key events.
C: 67,
V: 86,
X: 88
};
...@@ -54,20 +54,7 @@ class TextInputManager { ...@@ -54,20 +54,7 @@ class TextInputManager {
const x = node.location.left + Math.round(node.location.width / 2); const x = node.location.left + Math.round(node.location.width / 2);
const y = node.location.top + Math.round(node.location.height / 2); const y = node.location.top + Math.round(node.location.height / 2);
chrome.accessibilityPrivate.sendSyntheticMouseEvent({ EventHelper.simulateMouseClick(x, y, SAConstants.VK_KEY_PRESS_DURATION_MS);
type: chrome.accessibilityPrivate.SyntheticMouseEventType.PRESS,
x: x,
y: y
});
setTimeout(
() => chrome.accessibilityPrivate.sendSyntheticMouseEvent({
type: chrome.accessibilityPrivate.SyntheticMouseEventType.RELEASE,
x: x,
y: y
}),
SAConstants.KEY_PRESS_DURATION_MS);
return true; return true;
} }
...@@ -108,8 +95,7 @@ class TextInputManager { ...@@ -108,8 +95,7 @@ class TextInputManager {
* @public * @public
*/ */
cut() { cut() {
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.X, {ctrl: true});
SAConstants.KeyCode.X, {ctrl: true});
} }
/** /**
...@@ -118,8 +104,7 @@ class TextInputManager { ...@@ -118,8 +104,7 @@ class TextInputManager {
* @public * @public
*/ */
copy() { copy() {
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.C, {ctrl: true});
SAConstants.KeyCode.C, {ctrl: true});
} }
/** /**
...@@ -128,7 +113,6 @@ class TextInputManager { ...@@ -128,7 +113,6 @@ class TextInputManager {
* @public * @public
*/ */
paste() { paste() {
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.V, {ctrl: true});
SAConstants.KeyCode.V, {ctrl: true});
} }
} }
...@@ -42,8 +42,7 @@ class TextNavigationManager { ...@@ -42,8 +42,7 @@ class TextNavigationManager {
jumpToBeginning() { jumpToBeginning() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(false /* resetCursor */); this.setupDynamicSelection_(false /* resetCursor */);
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.HOME, {ctrl: true});
SAConstants.KeyCode.HOME, {ctrl: true});
} }
/** /**
...@@ -54,8 +53,7 @@ class TextNavigationManager { ...@@ -54,8 +53,7 @@ class TextNavigationManager {
jumpToEnd() { jumpToEnd() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(false /* resetCursor */); this.setupDynamicSelection_(false /* resetCursor */);
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.END, {ctrl: true});
SAConstants.KeyCode.END, {ctrl: true});
} }
/** /**
...@@ -67,8 +65,7 @@ class TextNavigationManager { ...@@ -67,8 +65,7 @@ class TextNavigationManager {
moveBackwardOneChar() { moveBackwardOneChar() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(true /* resetCursor */); this.setupDynamicSelection_(true /* resetCursor */);
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.LEFT_ARROW);
SAConstants.KeyCode.LEFT_ARROW, {});
} }
/** /**
...@@ -80,8 +77,7 @@ class TextNavigationManager { ...@@ -80,8 +77,7 @@ class TextNavigationManager {
moveForwardOneChar() { moveForwardOneChar() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(true /* resetCursor */); this.setupDynamicSelection_(true /* resetCursor */);
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.RIGHT_ARROW);
SAConstants.KeyCode.RIGHT_ARROW, {});
} }
/** /**
...@@ -94,8 +90,7 @@ class TextNavigationManager { ...@@ -94,8 +90,7 @@ class TextNavigationManager {
moveBackwardOneWord() { moveBackwardOneWord() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(false /* resetCursor */); this.setupDynamicSelection_(false /* resetCursor */);
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.LEFT_ARROW, {ctrl: true});
SAConstants.KeyCode.LEFT_ARROW, {ctrl: true});
} }
/** /**
...@@ -108,8 +103,7 @@ class TextNavigationManager { ...@@ -108,8 +103,7 @@ class TextNavigationManager {
moveForwardOneWord() { moveForwardOneWord() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(false /* resetCursor */); this.setupDynamicSelection_(false /* resetCursor */);
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.RIGHT_ARROW, {ctrl: true});
SAConstants.KeyCode.RIGHT_ARROW, {ctrl: true});
} }
/** /**
...@@ -121,7 +115,7 @@ class TextNavigationManager { ...@@ -121,7 +115,7 @@ class TextNavigationManager {
moveUpOneLine() { moveUpOneLine() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(true /* resetCursor */); this.setupDynamicSelection_(true /* resetCursor */);
this.navigationManager_.simulateKeyPress(SAConstants.KeyCode.UP_ARROW, {}); EventHelper.simulateKeyPress(EventHelper.KeyCode.UP_ARROW);
} }
/** /**
...@@ -133,8 +127,7 @@ class TextNavigationManager { ...@@ -133,8 +127,7 @@ class TextNavigationManager {
moveDownOneLine() { moveDownOneLine() {
if (this.currentlySelecting_) if (this.currentlySelecting_)
this.setupDynamicSelection_(true /* resetCursor */); this.setupDynamicSelection_(true /* resetCursor */);
this.navigationManager_.simulateKeyPress( EventHelper.simulateKeyPress(EventHelper.KeyCode.DOWN_ARROW);
SAConstants.KeyCode.DOWN_ARROW, {});
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment