2016-02-03 21:47:08 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2020-09-08 21:19:32 +03:00
|
|
|
"use strict";
|
2017-06-28 21:04:14 +03:00
|
|
|
/* global content, is */
|
2018-11-07 01:35:05 +03:00
|
|
|
/* eslint-disable no-restricted-globals */
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2020-09-08 21:19:32 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["event"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2020-09-08 21:22:43 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
2019-01-17 21:18:31 +03:00
|
|
|
);
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2020-09-08 21:22:43 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
element: "chrome://marionette/content/element.js",
|
|
|
|
});
|
|
|
|
|
2020-09-08 21:19:32 +03:00
|
|
|
/** Provides functionality for creating and sending DOM events. */
|
|
|
|
this.event = {};
|
|
|
|
|
2017-11-24 23:53:57 +03:00
|
|
|
const dblclickTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
|
|
|
// Max interval between two clicks that should result in a dblclick (in ms)
|
|
|
|
const DBLCLICK_INTERVAL = 640;
|
|
|
|
|
2016-02-03 21:47:08 +03:00
|
|
|
// TODO(ato): Document!
|
|
|
|
let seenEvent = false;
|
|
|
|
|
|
|
|
event.MouseEvents = {
|
|
|
|
click: 0,
|
|
|
|
dblclick: 1,
|
|
|
|
mousedown: 2,
|
|
|
|
mouseup: 3,
|
|
|
|
mouseover: 4,
|
|
|
|
mouseout: 5,
|
|
|
|
};
|
|
|
|
|
|
|
|
event.Modifiers = {
|
|
|
|
shiftKey: 0,
|
|
|
|
ctrlKey: 1,
|
|
|
|
altKey: 2,
|
|
|
|
metaKey: 3,
|
|
|
|
};
|
|
|
|
|
2017-08-09 14:51:18 +03:00
|
|
|
event.MouseButton = {
|
|
|
|
isPrimary(button) {
|
|
|
|
return button === 0;
|
|
|
|
},
|
|
|
|
isAuxiliary(button) {
|
|
|
|
return button === 1;
|
|
|
|
},
|
|
|
|
isSecondary(button) {
|
|
|
|
return button === 2;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-11-24 23:53:57 +03:00
|
|
|
event.DoubleClickTracker = {
|
|
|
|
firstClick: false,
|
|
|
|
isClicked() {
|
|
|
|
return event.DoubleClickTracker.firstClick;
|
|
|
|
},
|
|
|
|
setClick() {
|
|
|
|
if (!event.DoubleClickTracker.firstClick) {
|
|
|
|
event.DoubleClickTracker.firstClick = true;
|
|
|
|
event.DoubleClickTracker.startTimer();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resetClick() {
|
|
|
|
event.DoubleClickTracker.firstClick = false;
|
2018-01-18 23:41:23 +03:00
|
|
|
event.DoubleClickTracker.cancelTimer();
|
2017-11-24 23:53:57 +03:00
|
|
|
},
|
|
|
|
startTimer() {
|
|
|
|
dblclickTimer.initWithCallback(
|
|
|
|
event.DoubleClickTracker.resetClick,
|
|
|
|
DBLCLICK_INTERVAL,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT
|
|
|
|
);
|
|
|
|
},
|
|
|
|
cancelTimer() {
|
|
|
|
dblclickTimer.cancel();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-02-03 22:25:28 +03:00
|
|
|
// TODO(ato): Unexpose this when action.Chain#emitMouseEvent
|
2016-02-03 21:47:08 +03:00
|
|
|
// no longer emits its own events
|
2017-06-30 02:40:24 +03:00
|
|
|
event.parseModifiers_ = function(modifiers) {
|
2016-02-03 21:47:08 +03:00
|
|
|
let mval = 0;
|
2016-08-05 20:03:43 +03:00
|
|
|
if (modifiers.shiftKey) {
|
2018-03-26 21:53:53 +03:00
|
|
|
mval |= Ci.nsIDOMWindowUtils.MODIFIER_SHIFT;
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
2016-08-05 20:03:43 +03:00
|
|
|
if (modifiers.ctrlKey) {
|
2018-03-26 21:53:53 +03:00
|
|
|
mval |= Ci.nsIDOMWindowUtils.MODIFIER_CONTROL;
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
2016-08-05 20:03:43 +03:00
|
|
|
if (modifiers.altKey) {
|
2018-03-26 21:53:53 +03:00
|
|
|
mval |= Ci.nsIDOMWindowUtils.MODIFIER_ALT;
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
2016-08-05 20:03:43 +03:00
|
|
|
if (modifiers.metaKey) {
|
2018-03-26 21:53:53 +03:00
|
|
|
mval |= Ci.nsIDOMWindowUtils.MODIFIER_META;
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
2016-08-05 20:03:43 +03:00
|
|
|
if (modifiers.accelKey) {
|
2018-06-15 01:42:33 +03:00
|
|
|
if (Services.appinfo.OS === "Darwin") {
|
2018-03-26 21:53:53 +03:00
|
|
|
mval |= Ci.nsIDOMWindowUtils.MODIFIER_META;
|
2016-02-03 21:47:08 +03:00
|
|
|
} else {
|
2018-03-26 21:53:53 +03:00
|
|
|
mval |= Ci.nsIDOMWindowUtils.MODIFIER_CONTROL;
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mval;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesise a mouse event on a target.
|
|
|
|
*
|
|
|
|
* The actual client point is determined by taking the aTarget's client
|
|
|
|
* box and offseting it by offsetX and offsetY. This allows mouse clicks
|
|
|
|
* to be simulated by calling this method.
|
|
|
|
*
|
|
|
|
* If the type is specified, an mouse event of that type is
|
|
|
|
* fired. Otherwise, a mousedown followed by a mouse up is performed.
|
|
|
|
*
|
|
|
|
* @param {Element} element
|
|
|
|
* Element to click.
|
|
|
|
* @param {number} offsetX
|
|
|
|
* Horizontal offset to click from the target's bounding box.
|
|
|
|
* @param {number} offsetY
|
|
|
|
* Vertical offset to click from the target's bounding box.
|
|
|
|
* @param {Object.<string, ?>} opts
|
|
|
|
* Object which may contain the properties "shiftKey", "ctrlKey",
|
|
|
|
* "altKey", "metaKey", "accessKey", "clickCount", "button", and
|
|
|
|
* "type".
|
2018-09-18 15:21:40 +03:00
|
|
|
* @param {Window} win
|
|
|
|
* Window object.
|
2016-02-03 21:47:08 +03:00
|
|
|
*/
|
2018-09-18 15:21:40 +03:00
|
|
|
event.synthesizeMouse = function(element, offsetX, offsetY, opts, win) {
|
2016-02-03 21:47:08 +03:00
|
|
|
let rect = element.getBoundingClientRect();
|
|
|
|
event.synthesizeMouseAtPoint(
|
2018-09-18 15:21:40 +03:00
|
|
|
rect.left + offsetX,
|
|
|
|
rect.top + offsetY,
|
|
|
|
opts,
|
|
|
|
win
|
|
|
|
);
|
2016-02-03 21:47:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synthesize a mouse event at a particular point in a window.
|
|
|
|
*
|
|
|
|
* If the type of the event is specified, a mouse event of that type is
|
|
|
|
* fired. Otherwise, a mousedown followed by a mouse up is performed.
|
|
|
|
*
|
|
|
|
* @param {number} left
|
|
|
|
* CSS pixels from the left document margin.
|
|
|
|
* @param {number} top
|
|
|
|
* CSS pixels from the top document margin.
|
2017-02-18 00:49:03 +03:00
|
|
|
* @param {Object.<string, ?>} opts
|
2016-02-03 21:47:08 +03:00
|
|
|
* Object which may contain the properties "shiftKey", "ctrlKey",
|
|
|
|
* "altKey", "metaKey", "accessKey", "clickCount", "button", and
|
|
|
|
* "type".
|
2018-09-18 15:21:40 +03:00
|
|
|
* @param {Window} win
|
|
|
|
* Window object.
|
2016-02-03 21:47:08 +03:00
|
|
|
*/
|
2018-09-18 15:21:40 +03:00
|
|
|
event.synthesizeMouseAtPoint = function(left, top, opts, win) {
|
2018-09-18 16:19:39 +03:00
|
|
|
let domutils = win.windowUtils;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2016-08-05 20:05:45 +03:00
|
|
|
let button = opts.button || 0;
|
|
|
|
let clickCount = opts.clickCount || 1;
|
|
|
|
let modifiers = event.parseModifiers_(opts);
|
2017-02-18 00:49:03 +03:00
|
|
|
let pressure = "pressure" in opts ? opts.pressure : 0;
|
|
|
|
let inputSource =
|
|
|
|
"inputSource" in opts ? opts.inputSource : win.MouseEvent.MOZ_SOURCE_MOUSE;
|
|
|
|
let isDOMEventSynthesized =
|
|
|
|
"isSynthesized" in opts ? opts.isSynthesized : true;
|
2017-06-30 02:40:24 +03:00
|
|
|
let isWidgetEventSynthesized;
|
|
|
|
if ("isWidgetEventSynthesized" in opts) {
|
|
|
|
isWidgetEventSynthesized = opts.isWidgetEventSynthesized;
|
|
|
|
} else {
|
|
|
|
isWidgetEventSynthesized = false;
|
|
|
|
}
|
|
|
|
let buttons;
|
|
|
|
if ("buttons" in opts) {
|
|
|
|
buttons = opts.buttons;
|
|
|
|
} else {
|
|
|
|
buttons = domutils.MOUSE_BUTTONS_NOT_SPECIFIED;
|
|
|
|
}
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2017-02-18 00:49:03 +03:00
|
|
|
if ("type" in opts && opts.type) {
|
2016-02-03 21:47:08 +03:00
|
|
|
domutils.sendMouseEvent(
|
2017-06-30 02:40:24 +03:00
|
|
|
opts.type,
|
|
|
|
left,
|
|
|
|
top,
|
|
|
|
button,
|
|
|
|
clickCount,
|
|
|
|
modifiers,
|
|
|
|
false,
|
|
|
|
pressure,
|
|
|
|
inputSource,
|
|
|
|
isDOMEventSynthesized,
|
|
|
|
isWidgetEventSynthesized,
|
|
|
|
buttons
|
|
|
|
);
|
2016-02-03 21:47:08 +03:00
|
|
|
} else {
|
|
|
|
domutils.sendMouseEvent(
|
2017-06-30 02:40:24 +03:00
|
|
|
"mousedown",
|
|
|
|
left,
|
|
|
|
top,
|
|
|
|
button,
|
|
|
|
clickCount,
|
|
|
|
modifiers,
|
|
|
|
false,
|
|
|
|
pressure,
|
|
|
|
inputSource,
|
|
|
|
isDOMEventSynthesized,
|
|
|
|
isWidgetEventSynthesized,
|
|
|
|
buttons
|
|
|
|
);
|
2016-02-03 21:47:08 +03:00
|
|
|
domutils.sendMouseEvent(
|
2017-06-30 02:40:24 +03:00
|
|
|
"mouseup",
|
|
|
|
left,
|
|
|
|
top,
|
|
|
|
button,
|
|
|
|
clickCount,
|
|
|
|
modifiers,
|
|
|
|
false,
|
|
|
|
pressure,
|
|
|
|
inputSource,
|
|
|
|
isDOMEventSynthesized,
|
|
|
|
isWidgetEventSynthesized,
|
|
|
|
buttons
|
|
|
|
);
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
/* eslint-disable */
|
2018-09-18 15:21:40 +03:00
|
|
|
function computeKeyCodeFromChar_(char, win) {
|
2016-02-03 21:47:08 +03:00
|
|
|
if (char.length != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-09 19:17:09 +03:00
|
|
|
let KeyboardEvent = getKeyboardEvent_(win);
|
|
|
|
|
2017-04-01 09:37:30 +03:00
|
|
|
if (char in VIRTUAL_KEYCODE_LOOKUP) {
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent["DOM_" + VIRTUAL_KEYCODE_LOOKUP[char]];
|
2017-04-01 09:37:30 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 21:47:08 +03:00
|
|
|
if (char >= "a" && char <= "z") {
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_A + char.charCodeAt(0) - "a".charCodeAt(0);
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
|
|
|
if (char >= "A" && char <= "Z") {
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_A + char.charCodeAt(0) - "A".charCodeAt(0);
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
|
|
|
if (char >= "0" && char <= "9") {
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_0 + char.charCodeAt(0) - "0".charCodeAt(0);
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns US keyboard layout's keycode
|
|
|
|
switch (char) {
|
|
|
|
case "~":
|
|
|
|
case "`":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_BACK_QUOTE;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "!":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_1;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "@":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_2;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "#":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_3;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "$":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_4;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "%":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_5;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "^":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_6;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "&":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_7;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "*":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_8;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "(":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_9;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case ")":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_0;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "-":
|
|
|
|
case "_":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_SUBTRACT;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "+":
|
|
|
|
case "=":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_EQUALS;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "{":
|
|
|
|
case "[":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_OPEN_BRACKET;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "}":
|
|
|
|
case "]":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_CLOSE_BRACKET;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "|":
|
|
|
|
case "\\":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_BACK_SLASH;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case ":":
|
|
|
|
case ";":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_SEMICOLON;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "'":
|
|
|
|
case "\"":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_QUOTE;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "<":
|
|
|
|
case ",":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_COMMA;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case ">":
|
|
|
|
case ".":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_PERIOD;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "?":
|
|
|
|
case "/":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_SLASH;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
case "\n":
|
2018-02-09 19:17:09 +03:00
|
|
|
return KeyboardEvent.DOM_VK_RETURN;
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2020-03-25 22:36:54 +03:00
|
|
|
case " ":
|
|
|
|
return KeyboardEvent.DOM_VK_SPACE;
|
|
|
|
|
2016-02-03 21:47:08 +03:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
/* eslint-enable */
|
2018-11-07 01:35:05 +03:00
|
|
|
/* eslint-disable no-restricted-globals */
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given key should cause keypress event when widget
|
|
|
|
* handles the native key event. Otherwise, false.
|
|
|
|
*
|
2018-02-09 19:17:09 +03:00
|
|
|
* The key code should be one of consts of KeyboardEvent.DOM_VK_*,
|
2016-02-03 21:47:08 +03:00
|
|
|
* or a key name begins with "VK_", or a character.
|
|
|
|
*/
|
2018-09-18 15:21:40 +03:00
|
|
|
event.isKeypressFiredKey = function(key, win) {
|
|
|
|
let KeyboardEvent = getKeyboardEvent_(win);
|
2018-02-09 19:17:09 +03:00
|
|
|
|
2016-02-03 21:47:08 +03:00
|
|
|
if (typeof key == "string") {
|
|
|
|
if (key.indexOf("VK_") === 0) {
|
2018-02-09 19:17:09 +03:00
|
|
|
key = KeyboardEvent["DOM_" + key];
|
2016-02-03 21:47:08 +03:00
|
|
|
if (!key) {
|
|
|
|
throw new TypeError("Unknown key: " + key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if key generates a character, it must cause a keypress event
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (key) {
|
2018-02-09 19:17:09 +03:00
|
|
|
case KeyboardEvent.DOM_VK_SHIFT:
|
|
|
|
case KeyboardEvent.DOM_VK_CONTROL:
|
|
|
|
case KeyboardEvent.DOM_VK_ALT:
|
|
|
|
case KeyboardEvent.DOM_VK_CAPS_LOCK:
|
|
|
|
case KeyboardEvent.DOM_VK_NUM_LOCK:
|
|
|
|
case KeyboardEvent.DOM_VK_SCROLL_LOCK:
|
|
|
|
case KeyboardEvent.DOM_VK_META:
|
2016-02-03 21:47:08 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesise a key event.
|
|
|
|
*
|
|
|
|
* It is targeted at whatever would be targeted by an actual keypress
|
|
|
|
* by the user, typically the focused element.
|
|
|
|
*
|
|
|
|
* @param {string} key
|
|
|
|
* Key to synthesise. Should either be a character or a key code
|
2016-12-14 02:29:48 +03:00
|
|
|
* starting with "VK_" such as VK_RETURN, or a normalized key value.
|
2016-02-03 21:47:08 +03:00
|
|
|
* @param {Object.<string, ?>} event
|
|
|
|
* Object which may contain the properties shiftKey, ctrlKey, altKey,
|
2016-12-14 02:29:48 +03:00
|
|
|
* metaKey, accessKey, type. If the type is specified (keydown or keyup),
|
|
|
|
* a key event of that type is fired. Otherwise, a keydown, a keypress,
|
|
|
|
* and then a keyup event are fired in sequence.
|
2018-09-18 15:21:40 +03:00
|
|
|
* @param {Window} win
|
|
|
|
* Window object.
|
2016-02-03 21:47:08 +03:00
|
|
|
*
|
|
|
|
* @throws {TypeError}
|
|
|
|
* If unknown key.
|
|
|
|
*/
|
2018-09-18 15:21:40 +03:00
|
|
|
event.synthesizeKey = function(key, event, win) {
|
2017-08-30 19:38:23 +03:00
|
|
|
let TIP = getTIP_(win);
|
2016-05-10 23:07:56 +03:00
|
|
|
if (!TIP) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-30 19:38:23 +03:00
|
|
|
let KeyboardEvent = getKeyboardEvent_(win);
|
|
|
|
let modifiers = emulateToActivateModifiers_(TIP, event, win);
|
|
|
|
let keyEventDict = createKeyboardEventDictionary_(key, event, win);
|
|
|
|
let keyEvent = new KeyboardEvent("", keyEventDict.dictionary);
|
|
|
|
let dispatchKeydown =
|
2016-05-10 23:07:56 +03:00
|
|
|
!("type" in event) || event.type === "keydown" || !event.type;
|
2017-08-30 19:38:23 +03:00
|
|
|
let dispatchKeyup =
|
2017-06-30 02:40:24 +03:00
|
|
|
!("type" in event) || event.type === "keyup" || !event.type;
|
2016-05-10 23:07:56 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (dispatchKeydown) {
|
|
|
|
TIP.keydown(keyEvent, keyEventDict.flags);
|
|
|
|
if ("repeat" in event && event.repeat > 1) {
|
|
|
|
keyEventDict.dictionary.repeat = true;
|
2017-08-30 19:38:23 +03:00
|
|
|
let repeatedKeyEvent = new KeyboardEvent("", keyEventDict.dictionary);
|
|
|
|
for (let i = 1; i < event.repeat; i++) {
|
2016-05-10 23:07:56 +03:00
|
|
|
TIP.keydown(repeatedKeyEvent, keyEventDict.flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dispatchKeyup) {
|
|
|
|
TIP.keyup(keyEvent, keyEventDict.flags);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
emulateToInactivateModifiers_(TIP, modifiers, win);
|
|
|
|
}
|
|
|
|
};
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2017-08-30 19:38:23 +03:00
|
|
|
const TIPMap = new WeakMap();
|
2016-05-10 23:07:56 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
function getTIP_(win, callback) {
|
2017-08-30 19:38:23 +03:00
|
|
|
let tip;
|
2018-09-18 15:21:40 +03:00
|
|
|
|
2016-05-10 23:07:56 +03:00
|
|
|
if (TIPMap.has(win)) {
|
|
|
|
tip = TIPMap.get(win);
|
|
|
|
} else {
|
2017-08-30 19:38:23 +03:00
|
|
|
tip = Cc["@mozilla.org/text-input-processor;1"].createInstance(
|
|
|
|
Ci.nsITextInputProcessor
|
|
|
|
);
|
2016-05-10 23:07:56 +03:00
|
|
|
TIPMap.set(win, tip);
|
|
|
|
}
|
|
|
|
if (!tip.beginInputTransactionForTests(win, callback)) {
|
|
|
|
tip = null;
|
|
|
|
TIPMap.delete(win);
|
|
|
|
}
|
|
|
|
return tip;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:21:40 +03:00
|
|
|
function getKeyboardEvent_(win) {
|
2016-05-10 23:07:56 +03:00
|
|
|
if (typeof KeyboardEvent != "undefined") {
|
|
|
|
try {
|
|
|
|
// See if the object can be instantiated; sometimes this yields
|
2017-06-30 02:40:24 +03:00
|
|
|
// 'TypeError: can't access dead object' or 'KeyboardEvent is not
|
|
|
|
// a constructor'.
|
2016-05-10 23:07:56 +03:00
|
|
|
new KeyboardEvent("", {});
|
|
|
|
return KeyboardEvent;
|
|
|
|
} catch (ex) {}
|
|
|
|
}
|
|
|
|
if (typeof content != "undefined" && "KeyboardEvent" in content) {
|
|
|
|
return content.KeyboardEvent;
|
|
|
|
}
|
|
|
|
return win.KeyboardEvent;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:21:40 +03:00
|
|
|
function createKeyboardEventDictionary_(key, keyEvent, win) {
|
2017-08-30 19:38:23 +03:00
|
|
|
let result = { dictionary: null, flags: 0 };
|
|
|
|
let keyCodeIsDefined = "keyCode" in keyEvent && keyEvent.keyCode != undefined;
|
|
|
|
let keyCode =
|
2016-05-10 23:07:56 +03:00
|
|
|
keyCodeIsDefined && keyEvent.keyCode >= 0 && keyEvent.keyCode <= 255
|
|
|
|
? keyEvent.keyCode
|
|
|
|
: 0;
|
2017-08-30 19:38:23 +03:00
|
|
|
let keyName = "Unidentified";
|
2018-09-20 13:24:27 +03:00
|
|
|
|
2018-09-25 15:01:21 +03:00
|
|
|
let printable = false;
|
|
|
|
|
2016-05-10 23:07:56 +03:00
|
|
|
if (key.indexOf("KEY_") == 0) {
|
|
|
|
keyName = key.substr("KEY_".length);
|
|
|
|
result.flags |= Ci.nsITextInputProcessor.KEY_NON_PRINTABLE_KEY;
|
|
|
|
} else if (key.indexOf("VK_") == 0) {
|
2018-02-09 19:17:09 +03:00
|
|
|
keyCode = getKeyboardEvent_(win)["DOM_" + key];
|
2016-02-03 21:47:08 +03:00
|
|
|
if (!keyCode) {
|
2019-04-02 16:44:16 +03:00
|
|
|
throw new Error("Unknown key: " + key);
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
2016-05-10 23:07:56 +03:00
|
|
|
keyName = guessKeyNameFromKeyCode_(keyCode, win);
|
2017-04-01 09:37:30 +03:00
|
|
|
if (!isPrintable(keyCode, win)) {
|
2017-06-30 02:40:24 +03:00
|
|
|
result.flags |= Ci.nsITextInputProcessor.KEY_NON_PRINTABLE_KEY;
|
2017-04-01 09:37:30 +03:00
|
|
|
}
|
2016-05-10 23:07:56 +03:00
|
|
|
} else if (key != "") {
|
|
|
|
keyName = key;
|
|
|
|
if (!keyCodeIsDefined) {
|
2018-02-09 19:17:09 +03:00
|
|
|
keyCode = computeKeyCodeFromChar_(key.charAt(0), win);
|
2016-05-10 23:07:56 +03:00
|
|
|
}
|
|
|
|
if (!keyCode) {
|
|
|
|
result.flags |= Ci.nsITextInputProcessor.KEY_KEEP_KEYCODE_ZERO;
|
|
|
|
}
|
2017-04-01 09:37:30 +03:00
|
|
|
// only force printable if "raw character" and event key match, like "a"
|
|
|
|
if (!("key" in keyEvent && key != keyEvent.key)) {
|
2016-12-14 02:29:48 +03:00
|
|
|
result.flags |= Ci.nsITextInputProcessor.KEY_FORCE_PRINTABLE_KEY;
|
2018-09-25 15:01:21 +03:00
|
|
|
printable = true;
|
2016-12-14 02:29:48 +03:00
|
|
|
}
|
2016-05-10 23:07:56 +03:00
|
|
|
}
|
2018-09-20 13:24:27 +03:00
|
|
|
|
2017-08-30 19:38:23 +03:00
|
|
|
let locationIsDefined = "location" in keyEvent;
|
2016-05-10 23:07:56 +03:00
|
|
|
if (locationIsDefined && keyEvent.location === 0) {
|
|
|
|
result.flags |= Ci.nsITextInputProcessor.KEY_KEEP_KEY_LOCATION_STANDARD;
|
|
|
|
}
|
2018-09-20 13:24:27 +03:00
|
|
|
|
|
|
|
let resultKey = "key" in keyEvent ? keyEvent.key : keyName;
|
2018-09-25 15:01:21 +03:00
|
|
|
if (printable && keyEvent.shiftKey) {
|
2018-09-20 13:24:27 +03:00
|
|
|
resultKey = resultKey.toUpperCase();
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:07:56 +03:00
|
|
|
result.dictionary = {
|
2018-09-20 13:24:27 +03:00
|
|
|
key: resultKey,
|
2016-05-10 23:07:56 +03:00
|
|
|
code: "code" in keyEvent ? keyEvent.code : "",
|
|
|
|
location: locationIsDefined ? keyEvent.location : 0,
|
|
|
|
repeat: "repeat" in keyEvent ? keyEvent.repeat === true : false,
|
2017-06-30 02:40:24 +03:00
|
|
|
keyCode,
|
2016-05-10 23:07:56 +03:00
|
|
|
};
|
2018-09-20 13:24:27 +03:00
|
|
|
|
2016-05-10 23:07:56 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:21:40 +03:00
|
|
|
function emulateToActivateModifiers_(TIP, keyEvent, win) {
|
2016-05-10 23:07:56 +03:00
|
|
|
if (!keyEvent) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-08-30 19:38:23 +03:00
|
|
|
let KeyboardEvent = getKeyboardEvent_(win);
|
2016-05-10 23:07:56 +03:00
|
|
|
|
2017-08-30 19:38:23 +03:00
|
|
|
let modifiers = {
|
2016-05-10 23:07:56 +03:00
|
|
|
normal: [
|
2017-06-30 02:40:24 +03:00
|
|
|
{ key: "Alt", attr: "altKey" },
|
|
|
|
{ key: "AltGraph", attr: "altGraphKey" },
|
|
|
|
{ key: "Control", attr: "ctrlKey" },
|
|
|
|
{ key: "Fn", attr: "fnKey" },
|
|
|
|
{ key: "Meta", attr: "metaKey" },
|
|
|
|
{ key: "OS", attr: "osKey" },
|
|
|
|
{ key: "Shift", attr: "shiftKey" },
|
|
|
|
{ key: "Symbol", attr: "symbolKey" },
|
2018-06-15 01:42:33 +03:00
|
|
|
{
|
|
|
|
key: Services.appinfo.OS === "Darwin" ? "Meta" : "Control",
|
|
|
|
attr: "accelKey",
|
|
|
|
},
|
2016-05-10 23:07:56 +03:00
|
|
|
],
|
|
|
|
lockable: [
|
2017-06-30 02:40:24 +03:00
|
|
|
{ key: "CapsLock", attr: "capsLockKey" },
|
|
|
|
{ key: "FnLock", attr: "fnLockKey" },
|
|
|
|
{ key: "NumLock", attr: "numLockKey" },
|
|
|
|
{ key: "ScrollLock", attr: "scrollLockKey" },
|
|
|
|
{ key: "SymbolLock", attr: "symbolLockKey" },
|
|
|
|
],
|
2017-07-29 12:03:00 +03:00
|
|
|
};
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
for (let i = 0; i < modifiers.normal.length; i++) {
|
2016-05-10 23:07:56 +03:00
|
|
|
if (!keyEvent[modifiers.normal[i].attr]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (TIP.getModifierState(modifiers.normal[i].key)) {
|
|
|
|
continue; // already activated.
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
let event = new KeyboardEvent("", { key: modifiers.normal[i].key });
|
2016-05-10 23:07:56 +03:00
|
|
|
TIP.keydown(
|
|
|
|
event,
|
2017-07-29 12:03:00 +03:00
|
|
|
TIP.KEY_NON_PRINTABLE_KEY | TIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT
|
|
|
|
);
|
2016-05-10 23:07:56 +03:00
|
|
|
modifiers.normal[i].activated = true;
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
|
|
|
|
for (let j = 0; j < modifiers.lockable.length; j++) {
|
|
|
|
if (!keyEvent[modifiers.lockable[j].attr]) {
|
2016-05-10 23:07:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
if (TIP.getModifierState(modifiers.lockable[j].key)) {
|
2016-05-10 23:07:56 +03:00
|
|
|
continue; // already activated.
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
let event = new KeyboardEvent("", { key: modifiers.lockable[j].key });
|
2016-05-10 23:07:56 +03:00
|
|
|
TIP.keydown(
|
|
|
|
event,
|
2017-07-29 12:03:00 +03:00
|
|
|
TIP.KEY_NON_PRINTABLE_KEY | TIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT
|
|
|
|
);
|
2016-05-10 23:07:56 +03:00
|
|
|
TIP.keyup(
|
|
|
|
event,
|
2017-07-29 12:03:00 +03:00
|
|
|
TIP.KEY_NON_PRINTABLE_KEY | TIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT
|
|
|
|
);
|
2017-06-30 02:40:24 +03:00
|
|
|
modifiers.lockable[j].activated = true;
|
2016-05-10 23:07:56 +03:00
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
|
2016-05-10 23:07:56 +03:00
|
|
|
return modifiers;
|
|
|
|
}
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2018-09-18 15:21:40 +03:00
|
|
|
function emulateToInactivateModifiers_(TIP, modifiers, win) {
|
2016-05-10 23:07:56 +03:00
|
|
|
if (!modifiers) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
let KeyboardEvent = getKeyboardEvent_(win);
|
|
|
|
for (let i = 0; i < modifiers.normal.length; i++) {
|
2016-05-10 23:07:56 +03:00
|
|
|
if (!modifiers.normal[i].activated) {
|
|
|
|
continue;
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
let event = new KeyboardEvent("", { key: modifiers.normal[i].key });
|
2016-05-10 23:07:56 +03:00
|
|
|
TIP.keyup(
|
|
|
|
event,
|
2017-07-29 12:03:00 +03:00
|
|
|
TIP.KEY_NON_PRINTABLE_KEY | TIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT
|
|
|
|
);
|
2016-05-10 23:07:56 +03:00
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
for (let j = 0; j < modifiers.lockable.length; j++) {
|
|
|
|
if (!modifiers.lockable[j].activated) {
|
2016-05-10 23:07:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
if (!TIP.getModifierState(modifiers.lockable[j].key)) {
|
2016-05-10 23:07:56 +03:00
|
|
|
continue; // who already inactivated this?
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
let event = new KeyboardEvent("", { key: modifiers.lockable[j].key });
|
2016-05-10 23:07:56 +03:00
|
|
|
TIP.keydown(
|
|
|
|
event,
|
2017-07-29 12:03:00 +03:00
|
|
|
TIP.KEY_NON_PRINTABLE_KEY | TIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT
|
|
|
|
);
|
2016-05-10 23:07:56 +03:00
|
|
|
TIP.keyup(
|
|
|
|
event,
|
2017-07-29 12:03:00 +03:00
|
|
|
TIP.KEY_NON_PRINTABLE_KEY | TIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT
|
|
|
|
);
|
2016-05-10 23:07:56 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
/* eslint-disable */
|
2018-09-18 15:21:40 +03:00
|
|
|
function guessKeyNameFromKeyCode_(aKeyCode, win) {
|
2017-08-30 19:38:23 +03:00
|
|
|
let KeyboardEvent = getKeyboardEvent_(win);
|
2016-05-10 23:07:56 +03:00
|
|
|
switch (aKeyCode) {
|
|
|
|
case KeyboardEvent.DOM_VK_CANCEL:
|
|
|
|
return "Cancel";
|
|
|
|
case KeyboardEvent.DOM_VK_HELP:
|
|
|
|
return "Help";
|
|
|
|
case KeyboardEvent.DOM_VK_BACK_SPACE:
|
|
|
|
return "Backspace";
|
|
|
|
case KeyboardEvent.DOM_VK_TAB:
|
|
|
|
return "Tab";
|
|
|
|
case KeyboardEvent.DOM_VK_CLEAR:
|
|
|
|
return "Clear";
|
|
|
|
case KeyboardEvent.DOM_VK_RETURN:
|
|
|
|
return "Enter";
|
|
|
|
case KeyboardEvent.DOM_VK_SHIFT:
|
|
|
|
return "Shift";
|
|
|
|
case KeyboardEvent.DOM_VK_CONTROL:
|
|
|
|
return "Control";
|
|
|
|
case KeyboardEvent.DOM_VK_ALT:
|
|
|
|
return "Alt";
|
|
|
|
case KeyboardEvent.DOM_VK_PAUSE:
|
|
|
|
return "Pause";
|
|
|
|
case KeyboardEvent.DOM_VK_EISU:
|
|
|
|
return "Eisu";
|
|
|
|
case KeyboardEvent.DOM_VK_ESCAPE:
|
|
|
|
return "Escape";
|
|
|
|
case KeyboardEvent.DOM_VK_CONVERT:
|
|
|
|
return "Convert";
|
|
|
|
case KeyboardEvent.DOM_VK_NONCONVERT:
|
|
|
|
return "NonConvert";
|
|
|
|
case KeyboardEvent.DOM_VK_ACCEPT:
|
|
|
|
return "Accept";
|
|
|
|
case KeyboardEvent.DOM_VK_MODECHANGE:
|
|
|
|
return "ModeChange";
|
|
|
|
case KeyboardEvent.DOM_VK_PAGE_UP:
|
|
|
|
return "PageUp";
|
|
|
|
case KeyboardEvent.DOM_VK_PAGE_DOWN:
|
|
|
|
return "PageDown";
|
|
|
|
case KeyboardEvent.DOM_VK_END:
|
|
|
|
return "End";
|
|
|
|
case KeyboardEvent.DOM_VK_HOME:
|
|
|
|
return "Home";
|
|
|
|
case KeyboardEvent.DOM_VK_LEFT:
|
|
|
|
return "ArrowLeft";
|
|
|
|
case KeyboardEvent.DOM_VK_UP:
|
|
|
|
return "ArrowUp";
|
|
|
|
case KeyboardEvent.DOM_VK_RIGHT:
|
|
|
|
return "ArrowRight";
|
|
|
|
case KeyboardEvent.DOM_VK_DOWN:
|
|
|
|
return "ArrowDown";
|
|
|
|
case KeyboardEvent.DOM_VK_SELECT:
|
|
|
|
return "Select";
|
|
|
|
case KeyboardEvent.DOM_VK_PRINT:
|
|
|
|
return "Print";
|
|
|
|
case KeyboardEvent.DOM_VK_EXECUTE:
|
|
|
|
return "Execute";
|
|
|
|
case KeyboardEvent.DOM_VK_PRINTSCREEN:
|
|
|
|
return "PrintScreen";
|
|
|
|
case KeyboardEvent.DOM_VK_INSERT:
|
|
|
|
return "Insert";
|
|
|
|
case KeyboardEvent.DOM_VK_DELETE:
|
|
|
|
return "Delete";
|
|
|
|
case KeyboardEvent.DOM_VK_WIN:
|
|
|
|
return "OS";
|
|
|
|
case KeyboardEvent.DOM_VK_CONTEXT_MENU:
|
|
|
|
return "ContextMenu";
|
|
|
|
case KeyboardEvent.DOM_VK_SLEEP:
|
|
|
|
return "Standby";
|
|
|
|
case KeyboardEvent.DOM_VK_F1:
|
|
|
|
return "F1";
|
|
|
|
case KeyboardEvent.DOM_VK_F2:
|
|
|
|
return "F2";
|
|
|
|
case KeyboardEvent.DOM_VK_F3:
|
|
|
|
return "F3";
|
|
|
|
case KeyboardEvent.DOM_VK_F4:
|
|
|
|
return "F4";
|
|
|
|
case KeyboardEvent.DOM_VK_F5:
|
|
|
|
return "F5";
|
|
|
|
case KeyboardEvent.DOM_VK_F6:
|
|
|
|
return "F6";
|
|
|
|
case KeyboardEvent.DOM_VK_F7:
|
|
|
|
return "F7";
|
|
|
|
case KeyboardEvent.DOM_VK_F8:
|
|
|
|
return "F8";
|
|
|
|
case KeyboardEvent.DOM_VK_F9:
|
|
|
|
return "F9";
|
|
|
|
case KeyboardEvent.DOM_VK_F10:
|
|
|
|
return "F10";
|
|
|
|
case KeyboardEvent.DOM_VK_F11:
|
|
|
|
return "F11";
|
|
|
|
case KeyboardEvent.DOM_VK_F12:
|
|
|
|
return "F12";
|
|
|
|
case KeyboardEvent.DOM_VK_F13:
|
|
|
|
return "F13";
|
|
|
|
case KeyboardEvent.DOM_VK_F14:
|
|
|
|
return "F14";
|
|
|
|
case KeyboardEvent.DOM_VK_F15:
|
|
|
|
return "F15";
|
|
|
|
case KeyboardEvent.DOM_VK_F16:
|
|
|
|
return "F16";
|
|
|
|
case KeyboardEvent.DOM_VK_F17:
|
|
|
|
return "F17";
|
|
|
|
case KeyboardEvent.DOM_VK_F18:
|
|
|
|
return "F18";
|
|
|
|
case KeyboardEvent.DOM_VK_F19:
|
|
|
|
return "F19";
|
|
|
|
case KeyboardEvent.DOM_VK_F20:
|
|
|
|
return "F20";
|
|
|
|
case KeyboardEvent.DOM_VK_F21:
|
|
|
|
return "F21";
|
|
|
|
case KeyboardEvent.DOM_VK_F22:
|
|
|
|
return "F22";
|
|
|
|
case KeyboardEvent.DOM_VK_F23:
|
|
|
|
return "F23";
|
|
|
|
case KeyboardEvent.DOM_VK_F24:
|
|
|
|
return "F24";
|
|
|
|
case KeyboardEvent.DOM_VK_NUM_LOCK:
|
|
|
|
return "NumLock";
|
|
|
|
case KeyboardEvent.DOM_VK_SCROLL_LOCK:
|
|
|
|
return "ScrollLock";
|
|
|
|
case KeyboardEvent.DOM_VK_VOLUME_MUTE:
|
2016-05-20 18:57:18 +03:00
|
|
|
return "AudioVolumeMute";
|
2016-05-10 23:07:56 +03:00
|
|
|
case KeyboardEvent.DOM_VK_VOLUME_DOWN:
|
2016-05-20 18:52:03 +03:00
|
|
|
return "AudioVolumeDown";
|
2016-05-10 23:07:56 +03:00
|
|
|
case KeyboardEvent.DOM_VK_VOLUME_UP:
|
2016-05-20 18:55:48 +03:00
|
|
|
return "AudioVolumeUp";
|
2016-05-10 23:07:56 +03:00
|
|
|
case KeyboardEvent.DOM_VK_META:
|
|
|
|
return "Meta";
|
|
|
|
case KeyboardEvent.DOM_VK_ALTGR:
|
|
|
|
return "AltGraph";
|
|
|
|
case KeyboardEvent.DOM_VK_ATTN:
|
|
|
|
return "Attn";
|
|
|
|
case KeyboardEvent.DOM_VK_CRSEL:
|
|
|
|
return "CrSel";
|
|
|
|
case KeyboardEvent.DOM_VK_EXSEL:
|
|
|
|
return "ExSel";
|
|
|
|
case KeyboardEvent.DOM_VK_EREOF:
|
|
|
|
return "EraseEof";
|
|
|
|
case KeyboardEvent.DOM_VK_PLAY:
|
|
|
|
return "Play";
|
|
|
|
default:
|
|
|
|
return "Unidentified";
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
/* eslint-enable */
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicate that an event with an original target and type is expected
|
|
|
|
* to be fired, or not expected to be fired.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
/* eslint-disable */
|
2016-02-03 21:47:08 +03:00
|
|
|
function expectEvent_(expectedTarget, expectedEvent, testName) {
|
|
|
|
if (!expectedTarget || !expectedEvent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
seenEvent = false;
|
|
|
|
|
|
|
|
let type;
|
|
|
|
if (expectedEvent.charAt(0) == "!") {
|
|
|
|
type = expectedEvent.substring(1);
|
|
|
|
} else {
|
|
|
|
type = expectedEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
let handler = ev => {
|
|
|
|
let pass = (!seenEvent && ev.originalTarget == expectedTarget && ev.type == type);
|
|
|
|
is(pass, true, `${testName} ${type} event target ${seenEvent ? "twice" : ""}`);
|
|
|
|
seenEvent = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
expectedTarget.addEventListener(type, handler);
|
|
|
|
return handler;
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
/* eslint-enable */
|
2018-11-07 01:35:05 +03:00
|
|
|
/* eslint-disable no-restricted-globals */
|
2016-02-03 21:47:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the event was fired or not. The provided event handler will
|
|
|
|
* be removed.
|
|
|
|
*/
|
|
|
|
function checkExpectedEvent_(
|
|
|
|
expectedTarget,
|
|
|
|
expectedEvent,
|
|
|
|
eventHandler,
|
|
|
|
testName
|
|
|
|
) {
|
|
|
|
if (eventHandler) {
|
|
|
|
let expectEvent = expectedEvent.charAt(0) != "!";
|
|
|
|
let type = expectEvent;
|
|
|
|
if (!type) {
|
|
|
|
type = expectedEvent.substring(1);
|
|
|
|
}
|
|
|
|
expectedTarget.removeEventListener(type, eventHandler);
|
|
|
|
|
|
|
|
let desc = `${type} event`;
|
|
|
|
if (!expectEvent) {
|
|
|
|
desc += " not";
|
|
|
|
}
|
|
|
|
is(seenEvent, expectEvent, `${testName} ${desc} fired`);
|
|
|
|
}
|
|
|
|
|
|
|
|
seenEvent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to event.synthesizeMouse except that a test is performed to
|
|
|
|
* see if an event is fired at the right target as a result.
|
|
|
|
*
|
|
|
|
* To test that an event is not fired, use an expected type preceded by
|
|
|
|
* an exclamation mark, such as "!select". This might be used to test that
|
|
|
|
* a click on a disabled element doesn't fire certain events for instance.
|
|
|
|
*
|
|
|
|
* @param {Element} target
|
|
|
|
* Synthesise the mouse event on this target.
|
|
|
|
* @param {number} offsetX
|
|
|
|
* Horizontal offset from the target's bounding box.
|
|
|
|
* @param {number} offsetY
|
|
|
|
* Vertical offset from the target's bounding box.
|
|
|
|
* @param {Object.<string, ?>} ev
|
|
|
|
* Object which may contain the properties shiftKey, ctrlKey, altKey,
|
|
|
|
* metaKey, accessKey, type.
|
|
|
|
* @param {Element} expectedTarget
|
|
|
|
* Expected originalTarget of the event.
|
|
|
|
* @param {DOMEvent} expectedEvent
|
|
|
|
* Expected type of the event, such as "select".
|
|
|
|
* @param {string} testName
|
|
|
|
* Test name when outputing results.
|
2018-09-18 15:21:40 +03:00
|
|
|
* @param {Window} win
|
|
|
|
* Window object.
|
2016-02-03 21:47:08 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
event.synthesizeMouseExpectEvent = function(
|
2016-02-03 21:47:08 +03:00
|
|
|
target,
|
|
|
|
offsetX,
|
|
|
|
offsetY,
|
|
|
|
ev,
|
|
|
|
expectedTarget,
|
|
|
|
expectedEvent,
|
2018-09-18 15:21:40 +03:00
|
|
|
testName,
|
2019-07-05 12:01:24 +03:00
|
|
|
win
|
|
|
|
) {
|
2016-02-03 21:47:08 +03:00
|
|
|
let eventHandler = expectEvent_(expectedTarget, expectedEvent, testName);
|
2018-09-18 15:21:40 +03:00
|
|
|
event.synthesizeMouse(target, offsetX, offsetY, ev, win);
|
2016-02-03 21:47:08 +03:00
|
|
|
checkExpectedEvent_(expectedTarget, expectedEvent, eventHandler, testName);
|
|
|
|
};
|
|
|
|
|
2018-09-20 13:24:27 +03:00
|
|
|
const MODIFIER_KEYCODES_LOOKUP = {
|
2016-02-03 21:47:08 +03:00
|
|
|
VK_SHIFT: "shiftKey",
|
|
|
|
VK_CONTROL: "ctrlKey",
|
|
|
|
VK_ALT: "altKey",
|
|
|
|
VK_META: "metaKey",
|
|
|
|
};
|
|
|
|
|
|
|
|
const VIRTUAL_KEYCODE_LOOKUP = {
|
|
|
|
"\uE001": "VK_CANCEL",
|
|
|
|
"\uE002": "VK_HELP",
|
|
|
|
"\uE003": "VK_BACK_SPACE",
|
|
|
|
"\uE004": "VK_TAB",
|
|
|
|
"\uE005": "VK_CLEAR",
|
|
|
|
"\uE006": "VK_RETURN",
|
|
|
|
"\uE007": "VK_RETURN",
|
|
|
|
"\uE008": "VK_SHIFT",
|
|
|
|
"\uE009": "VK_CONTROL",
|
|
|
|
"\uE00A": "VK_ALT",
|
|
|
|
"\uE00B": "VK_PAUSE",
|
|
|
|
"\uE00C": "VK_ESCAPE",
|
|
|
|
"\uE00D": "VK_SPACE", // printable
|
|
|
|
"\uE00E": "VK_PAGE_UP",
|
|
|
|
"\uE00F": "VK_PAGE_DOWN",
|
|
|
|
"\uE010": "VK_END",
|
|
|
|
"\uE011": "VK_HOME",
|
|
|
|
"\uE012": "VK_LEFT",
|
|
|
|
"\uE013": "VK_UP",
|
|
|
|
"\uE014": "VK_RIGHT",
|
|
|
|
"\uE015": "VK_DOWN",
|
|
|
|
"\uE016": "VK_INSERT",
|
|
|
|
"\uE017": "VK_DELETE",
|
|
|
|
"\uE018": "VK_SEMICOLON",
|
|
|
|
"\uE019": "VK_EQUALS",
|
|
|
|
"\uE01A": "VK_NUMPAD0",
|
|
|
|
"\uE01B": "VK_NUMPAD1",
|
|
|
|
"\uE01C": "VK_NUMPAD2",
|
|
|
|
"\uE01D": "VK_NUMPAD3",
|
|
|
|
"\uE01E": "VK_NUMPAD4",
|
|
|
|
"\uE01F": "VK_NUMPAD5",
|
|
|
|
"\uE020": "VK_NUMPAD6",
|
|
|
|
"\uE021": "VK_NUMPAD7",
|
|
|
|
"\uE022": "VK_NUMPAD8",
|
|
|
|
"\uE023": "VK_NUMPAD9",
|
|
|
|
"\uE024": "VK_MULTIPLY",
|
|
|
|
"\uE025": "VK_ADD",
|
|
|
|
"\uE026": "VK_SEPARATOR",
|
|
|
|
"\uE027": "VK_SUBTRACT",
|
|
|
|
"\uE028": "VK_DECIMAL",
|
|
|
|
"\uE029": "VK_DIVIDE",
|
|
|
|
"\uE031": "VK_F1",
|
|
|
|
"\uE032": "VK_F2",
|
|
|
|
"\uE033": "VK_F3",
|
|
|
|
"\uE034": "VK_F4",
|
|
|
|
"\uE035": "VK_F5",
|
|
|
|
"\uE036": "VK_F6",
|
|
|
|
"\uE037": "VK_F7",
|
|
|
|
"\uE038": "VK_F8",
|
|
|
|
"\uE039": "VK_F9",
|
|
|
|
"\uE03A": "VK_F10",
|
|
|
|
"\uE03B": "VK_F11",
|
|
|
|
"\uE03C": "VK_F12",
|
2018-09-20 12:34:19 +03:00
|
|
|
"\uE03D": "VK_META",
|
|
|
|
"\uE050": "VK_SHIFT",
|
|
|
|
"\uE051": "VK_CONTROL",
|
|
|
|
"\uE052": "VK_ALT",
|
|
|
|
"\uE053": "VK_META",
|
|
|
|
"\uE054": "VK_PAGE_UP",
|
|
|
|
"\uE055": "VK_PAGE_DOWN",
|
|
|
|
"\uE056": "VK_END",
|
|
|
|
"\uE057": "VK_HOME",
|
|
|
|
"\uE058": "VK_LEFT",
|
|
|
|
"\uE059": "VK_UP",
|
|
|
|
"\uE05A": "VK_RIGHT",
|
|
|
|
"\uE05B": "VK_DOWN",
|
|
|
|
"\uE05C": "VK_INSERT",
|
|
|
|
"\uE05D": "VK_DELETE",
|
2016-02-03 21:47:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function getKeyCode(c) {
|
|
|
|
if (c in VIRTUAL_KEYCODE_LOOKUP) {
|
|
|
|
return VIRTUAL_KEYCODE_LOOKUP[c];
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:21:40 +03:00
|
|
|
function isPrintable(c, win) {
|
2017-04-01 09:37:30 +03:00
|
|
|
let KeyboardEvent = getKeyboardEvent_(win);
|
|
|
|
let NON_PRINT_KEYS = [
|
|
|
|
KeyboardEvent.DOM_VK_CANCEL,
|
|
|
|
KeyboardEvent.DOM_VK_HELP,
|
|
|
|
KeyboardEvent.DOM_VK_BACK_SPACE,
|
|
|
|
KeyboardEvent.DOM_VK_TAB,
|
|
|
|
KeyboardEvent.DOM_VK_CLEAR,
|
|
|
|
KeyboardEvent.DOM_VK_SHIFT,
|
|
|
|
KeyboardEvent.DOM_VK_CONTROL,
|
|
|
|
KeyboardEvent.DOM_VK_ALT,
|
|
|
|
KeyboardEvent.DOM_VK_PAUSE,
|
|
|
|
KeyboardEvent.DOM_VK_EISU,
|
|
|
|
KeyboardEvent.DOM_VK_ESCAPE,
|
|
|
|
KeyboardEvent.DOM_VK_CONVERT,
|
|
|
|
KeyboardEvent.DOM_VK_NONCONVERT,
|
|
|
|
KeyboardEvent.DOM_VK_ACCEPT,
|
|
|
|
KeyboardEvent.DOM_VK_MODECHANGE,
|
|
|
|
KeyboardEvent.DOM_VK_PAGE_UP,
|
|
|
|
KeyboardEvent.DOM_VK_PAGE_DOWN,
|
|
|
|
KeyboardEvent.DOM_VK_END,
|
|
|
|
KeyboardEvent.DOM_VK_HOME,
|
|
|
|
KeyboardEvent.DOM_VK_LEFT,
|
|
|
|
KeyboardEvent.DOM_VK_UP,
|
|
|
|
KeyboardEvent.DOM_VK_RIGHT,
|
|
|
|
KeyboardEvent.DOM_VK_DOWN,
|
|
|
|
KeyboardEvent.DOM_VK_SELECT,
|
|
|
|
KeyboardEvent.DOM_VK_PRINT,
|
|
|
|
KeyboardEvent.DOM_VK_EXECUTE,
|
|
|
|
KeyboardEvent.DOM_VK_PRINTSCREEN,
|
|
|
|
KeyboardEvent.DOM_VK_INSERT,
|
|
|
|
KeyboardEvent.DOM_VK_DELETE,
|
|
|
|
KeyboardEvent.DOM_VK_WIN,
|
|
|
|
KeyboardEvent.DOM_VK_CONTEXT_MENU,
|
|
|
|
KeyboardEvent.DOM_VK_SLEEP,
|
|
|
|
KeyboardEvent.DOM_VK_F1,
|
|
|
|
KeyboardEvent.DOM_VK_F2,
|
|
|
|
KeyboardEvent.DOM_VK_F3,
|
|
|
|
KeyboardEvent.DOM_VK_F4,
|
|
|
|
KeyboardEvent.DOM_VK_F5,
|
|
|
|
KeyboardEvent.DOM_VK_F6,
|
|
|
|
KeyboardEvent.DOM_VK_F7,
|
|
|
|
KeyboardEvent.DOM_VK_F8,
|
|
|
|
KeyboardEvent.DOM_VK_F9,
|
|
|
|
KeyboardEvent.DOM_VK_F10,
|
|
|
|
KeyboardEvent.DOM_VK_F11,
|
|
|
|
KeyboardEvent.DOM_VK_F12,
|
|
|
|
KeyboardEvent.DOM_VK_F13,
|
|
|
|
KeyboardEvent.DOM_VK_F14,
|
|
|
|
KeyboardEvent.DOM_VK_F15,
|
|
|
|
KeyboardEvent.DOM_VK_F16,
|
|
|
|
KeyboardEvent.DOM_VK_F17,
|
|
|
|
KeyboardEvent.DOM_VK_F18,
|
|
|
|
KeyboardEvent.DOM_VK_F19,
|
|
|
|
KeyboardEvent.DOM_VK_F20,
|
|
|
|
KeyboardEvent.DOM_VK_F21,
|
|
|
|
KeyboardEvent.DOM_VK_F22,
|
|
|
|
KeyboardEvent.DOM_VK_F23,
|
|
|
|
KeyboardEvent.DOM_VK_F24,
|
|
|
|
KeyboardEvent.DOM_VK_NUM_LOCK,
|
|
|
|
KeyboardEvent.DOM_VK_SCROLL_LOCK,
|
|
|
|
KeyboardEvent.DOM_VK_VOLUME_MUTE,
|
|
|
|
KeyboardEvent.DOM_VK_VOLUME_DOWN,
|
|
|
|
KeyboardEvent.DOM_VK_VOLUME_UP,
|
|
|
|
KeyboardEvent.DOM_VK_META,
|
|
|
|
KeyboardEvent.DOM_VK_ALTGR,
|
|
|
|
KeyboardEvent.DOM_VK_ATTN,
|
|
|
|
KeyboardEvent.DOM_VK_CRSEL,
|
|
|
|
KeyboardEvent.DOM_VK_EXSEL,
|
|
|
|
KeyboardEvent.DOM_VK_EREOF,
|
|
|
|
KeyboardEvent.DOM_VK_PLAY,
|
|
|
|
KeyboardEvent.DOM_VK_RETURN,
|
|
|
|
];
|
|
|
|
return !NON_PRINT_KEYS.includes(c);
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:21:40 +03:00
|
|
|
event.sendKeyDown = function(keyToSend, modifiers, win) {
|
2016-02-03 21:47:08 +03:00
|
|
|
modifiers.type = "keydown";
|
2018-09-18 15:21:40 +03:00
|
|
|
event.sendSingleKey(keyToSend, modifiers, win);
|
2016-02-03 21:47:08 +03:00
|
|
|
delete modifiers.type;
|
|
|
|
};
|
|
|
|
|
2018-09-18 15:21:40 +03:00
|
|
|
event.sendKeyUp = function(keyToSend, modifiers, win) {
|
2016-02-03 21:47:08 +03:00
|
|
|
modifiers.type = "keyup";
|
2018-09-18 15:21:40 +03:00
|
|
|
event.sendSingleKey(keyToSend, modifiers, win);
|
2016-02-03 21:47:08 +03:00
|
|
|
delete modifiers.type;
|
|
|
|
};
|
|
|
|
|
2016-12-14 02:29:48 +03:00
|
|
|
/**
|
|
|
|
* Synthesize a key event for a single key.
|
|
|
|
*
|
|
|
|
* @param {string} keyToSend
|
|
|
|
* Code point or normalized key value
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {Object.<string, boolean>} modifiers
|
2016-12-14 02:29:48 +03:00
|
|
|
* Object with properties used in KeyboardEvent (shiftkey, repeat, ...)
|
2017-06-30 02:40:24 +03:00
|
|
|
* as well as, the event |type| such as keydown. All properties
|
|
|
|
* are optional.
|
2018-09-18 15:21:40 +03:00
|
|
|
* @param {Window} win
|
|
|
|
* Window object.
|
2016-12-14 02:29:48 +03:00
|
|
|
*/
|
2018-09-18 15:21:40 +03:00
|
|
|
event.sendSingleKey = function(keyToSend, modifiers, win) {
|
2018-09-20 13:24:27 +03:00
|
|
|
let keyCode = getKeyCode(keyToSend);
|
|
|
|
if (keyCode in MODIFIER_KEYCODES_LOOKUP) {
|
|
|
|
// For |sendKeysToElement| and legacy actions we assume that if
|
|
|
|
// |keyToSend| is a raw code point (like "\uE009") then |modifiers| does
|
|
|
|
// not already have correct value for corresponding |modName| attribute
|
|
|
|
// (like ctrlKey), so that value needs to be flipped.
|
|
|
|
let modName = MODIFIER_KEYCODES_LOOKUP[keyCode];
|
2016-02-03 21:47:08 +03:00
|
|
|
modifiers[modName] = !modifiers[modName];
|
|
|
|
}
|
2018-09-20 13:24:27 +03:00
|
|
|
event.synthesizeKey(keyCode, modifiers, win);
|
2016-02-03 21:47:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 00:37:43 +03:00
|
|
|
* @param {string} keyString
|
2016-02-03 21:47:08 +03:00
|
|
|
* @param {Element} element
|
2018-09-18 15:21:40 +03:00
|
|
|
* @param {Window} win
|
2016-02-03 21:47:08 +03:00
|
|
|
*/
|
2018-09-18 15:21:40 +03:00
|
|
|
event.sendKeysToElement = function(keyString, el, win) {
|
2017-11-09 22:39:51 +03:00
|
|
|
// make Object.<modifier, false> map
|
|
|
|
let modifiers = Object.create(event.Modifiers);
|
|
|
|
for (let modifier in event.Modifiers) {
|
|
|
|
modifiers[modifier] = false;
|
|
|
|
}
|
2016-02-03 21:47:08 +03:00
|
|
|
|
2017-11-09 22:39:51 +03:00
|
|
|
for (let i = 0; i < keyString.length; i++) {
|
|
|
|
let c = keyString.charAt(i);
|
2018-09-18 15:21:40 +03:00
|
|
|
event.sendSingleKey(c, modifiers, win);
|
2016-02-03 21:47:08 +03:00
|
|
|
}
|
|
|
|
};
|
2016-08-05 20:06:38 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.sendEvent = function(eventType, el, modifiers = {}, opts = {}) {
|
2016-08-05 20:06:38 +03:00
|
|
|
opts.canBubble = opts.canBubble || true;
|
|
|
|
|
|
|
|
let doc = el.ownerDocument || el.document;
|
|
|
|
let ev = doc.createEvent("Event");
|
|
|
|
|
2017-07-25 21:15:41 +03:00
|
|
|
ev.shiftKey = modifiers.shift;
|
|
|
|
ev.metaKey = modifiers.meta;
|
|
|
|
ev.altKey = modifiers.alt;
|
|
|
|
ev.ctrlKey = modifiers.ctrl;
|
2016-08-05 20:06:38 +03:00
|
|
|
|
|
|
|
ev.initEvent(eventType, opts.canBubble, true);
|
|
|
|
el.dispatchEvent(ev);
|
|
|
|
};
|
2016-08-05 20:07:12 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.mouseover = function(el, modifiers = {}, opts = {}) {
|
2016-08-25 16:16:10 +03:00
|
|
|
return event.sendEvent("mouseover", el, modifiers, opts);
|
2016-08-05 20:07:12 +03:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.mousemove = function(el, modifiers = {}, opts = {}) {
|
2016-08-25 16:16:10 +03:00
|
|
|
return event.sendEvent("mousemove", el, modifiers, opts);
|
2016-08-05 20:07:12 +03:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.mousedown = function(el, modifiers = {}, opts = {}) {
|
2016-08-25 16:16:10 +03:00
|
|
|
return event.sendEvent("mousedown", el, modifiers, opts);
|
2016-08-05 20:07:12 +03:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.mouseup = function(el, modifiers = {}, opts = {}) {
|
2016-08-25 16:16:10 +03:00
|
|
|
return event.sendEvent("mouseup", el, modifiers, opts);
|
2016-08-05 20:07:12 +03:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.click = function(el, modifiers = {}, opts = {}) {
|
2016-08-25 16:16:10 +03:00
|
|
|
return event.sendEvent("click", el, modifiers, opts);
|
2016-08-05 20:07:12 +03:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.change = function(el, modifiers = {}, opts = {}) {
|
2016-08-25 16:16:10 +03:00
|
|
|
return event.sendEvent("change", el, modifiers, opts);
|
2016-08-05 20:07:12 +03:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
event.input = function(el, modifiers = {}, opts = {}) {
|
2016-08-25 16:16:10 +03:00
|
|
|
return event.sendEvent("input", el, modifiers, opts);
|
2016-08-05 20:07:12 +03:00
|
|
|
};
|