1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/dom/events/keys.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,63 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +module.metadata = { 1.11 + "stability": "unstable" 1.12 +}; 1.13 + 1.14 +const { emit } = require("../events"); 1.15 +const { getCodeForKey, toJSON } = require("../../keyboard/utils"); 1.16 +const { has } = require("../../util/array"); 1.17 +const { isString } = require("../../lang/type"); 1.18 + 1.19 +const INITIALIZER = "initKeyEvent"; 1.20 +const CATEGORY = "KeyboardEvent"; 1.21 + 1.22 +function Options(options) { 1.23 + if (!isString(options)) 1.24 + return options; 1.25 + 1.26 + var { key, modifiers } = toJSON(options); 1.27 + return { 1.28 + key: key, 1.29 + control: has(modifiers, "control"), 1.30 + alt: has(modifiers, "alt"), 1.31 + shift: has(modifiers, "shift"), 1.32 + meta: has(modifiers, "meta") 1.33 + }; 1.34 +} 1.35 + 1.36 +var keyEvent = exports.keyEvent = function keyEvent(element, type, options) { 1.37 + 1.38 + emit(element, type, { 1.39 + initializer: INITIALIZER, 1.40 + category: CATEGORY, 1.41 + settings: [ 1.42 + !("bubbles" in options) || options.bubbles !== false, 1.43 + !("cancelable" in options) || options.cancelable !== false, 1.44 + "window" in options && options.window ? options.window : null, 1.45 + "control" in options && !!options.control, 1.46 + "alt" in options && !!options.alt, 1.47 + "shift" in options && !!options.shift, 1.48 + "meta" in options && !!options.meta, 1.49 + getCodeForKey(options.key) || 0, 1.50 + options.key.length === 1 ? options.key.charCodeAt(0) : 0 1.51 + ] 1.52 + }); 1.53 +} 1.54 + 1.55 +exports.keyDown = function keyDown(element, options) { 1.56 + keyEvent(element, "keydown", Options(options)); 1.57 +}; 1.58 + 1.59 +exports.keyUp = function keyUp(element, options) { 1.60 + keyEvent(element, "keyup", Options(options)); 1.61 +}; 1.62 + 1.63 +exports.keyPress = function keyPress(element, options) { 1.64 + keyEvent(element, "keypress", Options(options)); 1.65 +}; 1.66 +