michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { emit } = require("../events"); michael@0: const { getCodeForKey, toJSON } = require("../../keyboard/utils"); michael@0: const { has } = require("../../util/array"); michael@0: const { isString } = require("../../lang/type"); michael@0: michael@0: const INITIALIZER = "initKeyEvent"; michael@0: const CATEGORY = "KeyboardEvent"; michael@0: michael@0: function Options(options) { michael@0: if (!isString(options)) michael@0: return options; michael@0: michael@0: var { key, modifiers } = toJSON(options); michael@0: return { michael@0: key: key, michael@0: control: has(modifiers, "control"), michael@0: alt: has(modifiers, "alt"), michael@0: shift: has(modifiers, "shift"), michael@0: meta: has(modifiers, "meta") michael@0: }; michael@0: } michael@0: michael@0: var keyEvent = exports.keyEvent = function keyEvent(element, type, options) { michael@0: michael@0: emit(element, type, { michael@0: initializer: INITIALIZER, michael@0: category: CATEGORY, michael@0: settings: [ michael@0: !("bubbles" in options) || options.bubbles !== false, michael@0: !("cancelable" in options) || options.cancelable !== false, michael@0: "window" in options && options.window ? options.window : null, michael@0: "control" in options && !!options.control, michael@0: "alt" in options && !!options.alt, michael@0: "shift" in options && !!options.shift, michael@0: "meta" in options && !!options.meta, michael@0: getCodeForKey(options.key) || 0, michael@0: options.key.length === 1 ? options.key.charCodeAt(0) : 0 michael@0: ] michael@0: }); michael@0: } michael@0: michael@0: exports.keyDown = function keyDown(element, options) { michael@0: keyEvent(element, "keydown", Options(options)); michael@0: }; michael@0: michael@0: exports.keyUp = function keyUp(element, options) { michael@0: keyEvent(element, "keyup", Options(options)); michael@0: }; michael@0: michael@0: exports.keyPress = function keyPress(element, options) { michael@0: keyEvent(element, "keypress", Options(options)); michael@0: }; michael@0: