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 { Cc, Ci } = require("chrome"); michael@0: const runtime = require("../system/runtime"); michael@0: const { isString } = require("../lang/type"); michael@0: const array = require("../util/array"); michael@0: michael@0: michael@0: const SWP = "{{SEPARATOR}}"; michael@0: const SEPARATOR = "-" michael@0: const INVALID_COMBINATION = "Hotkey key combination must contain one or more " + michael@0: "modifiers and only one key"; michael@0: michael@0: // Map of modifier key mappings. michael@0: const MODIFIERS = exports.MODIFIERS = { michael@0: 'accel': runtime.OS === "Darwin" ? 'meta' : 'control', michael@0: 'meta': 'meta', michael@0: 'control': 'control', michael@0: 'ctrl': 'control', michael@0: 'option': 'alt', michael@0: 'command': 'meta', michael@0: 'alt': 'alt', michael@0: 'shift': 'shift' michael@0: }; michael@0: michael@0: // Hash of key:code pairs for all the chars supported by `nsIDOMKeyEvent`. michael@0: // This is just a copy of the `nsIDOMKeyEvent` hash with normalized names. michael@0: // @See: http://mxr.mozilla.org/mozilla-central/source/dom/interfaces/events/nsIDOMKeyEvent.idl michael@0: const CODES = exports.CODES = new function Codes() { michael@0: let nsIDOMKeyEvent = Ci.nsIDOMKeyEvent; michael@0: // Names that will be substituted with a shorter analogs. michael@0: let aliases = { michael@0: 'subtract': '-', michael@0: 'add': '+', michael@0: 'equals': '=', michael@0: 'slash': '/', michael@0: 'backslash': '\\', michael@0: 'openbracket': '[', michael@0: 'closebracket': ']', michael@0: 'quote': '\'', michael@0: 'backquote': '`', michael@0: 'period': '.', michael@0: 'semicolon': ';', michael@0: 'comma': ',' michael@0: }; michael@0: michael@0: // Normalizing keys and copying values to `this` object. michael@0: Object.keys(nsIDOMKeyEvent).filter(function(key) { michael@0: // Filter out only key codes. michael@0: return key.indexOf('DOM_VK') === 0; michael@0: }).map(function(key) { michael@0: // Map to key:values michael@0: return [ key, nsIDOMKeyEvent[key] ]; michael@0: }).map(function([key, value]) { michael@0: return [ key.replace('DOM_VK_', '').replace('_', '').toLowerCase(), value ]; michael@0: }).forEach(function ([ key, value ]) { michael@0: this[aliases[key] || key] = value; michael@0: }, this); michael@0: }; michael@0: michael@0: // Inverted `CODES` hash of `code:key`. michael@0: const KEYS = exports.KEYS = new function Keys() { michael@0: Object.keys(CODES).forEach(function(key) { michael@0: this[CODES[key]] = key; michael@0: }, this) michael@0: } michael@0: michael@0: exports.getKeyForCode = function getKeyForCode(code) { michael@0: return (code in KEYS) && KEYS[code]; michael@0: }; michael@0: exports.getCodeForKey = function getCodeForKey(key) { michael@0: return (key in CODES) && CODES[key]; michael@0: }; michael@0: michael@0: /** michael@0: * Utility function that takes string or JSON that defines a `hotkey` and michael@0: * returns normalized string version of it. michael@0: * @param {JSON|String} hotkey michael@0: * @param {String} [separator=" "] michael@0: * Optional string that represents separator used to concatenate keys in the michael@0: * given `hotkey`. michael@0: * @returns {String} michael@0: * @examples michael@0: * michael@0: * require("keyboard/hotkeys").normalize("b Shift accel"); michael@0: * // 'control shift b' -> on windows & linux michael@0: * // 'meta shift b' -> on mac michael@0: * require("keyboard/hotkeys").normalize("alt-d-shift", "-"); michael@0: * // 'alt shift d' michael@0: */ michael@0: var normalize = exports.normalize = function normalize(hotkey, separator) { michael@0: if (!isString(hotkey)) michael@0: hotkey = toString(hotkey, separator); michael@0: return toString(toJSON(hotkey, separator), separator); michael@0: }; michael@0: michael@0: /* michael@0: * Utility function that splits a string of characters that defines a `hotkey` michael@0: * into modifier keys and the defining key. michael@0: * @param {String} hotkey michael@0: * @param {String} [separator=" "] michael@0: * Optional string that represents separator used to concatenate keys in the michael@0: * given `hotkey`. michael@0: * @returns {JSON} michael@0: * @examples michael@0: * michael@0: * require("keyboard/hotkeys").toJSON("accel shift b"); michael@0: * // { key: 'b', modifiers: [ 'control', 'shift' ] } -> on windows & linux michael@0: * // { key: 'b', modifiers: [ 'meta', 'shift' ] } -> on mac michael@0: * michael@0: * require("keyboard/hotkeys").normalize("alt-d-shift", "-"); michael@0: * // { key: 'd', modifiers: [ 'alt', 'shift' ] } michael@0: */ michael@0: var toJSON = exports.toJSON = function toJSON(hotkey, separator) { michael@0: separator = separator || SEPARATOR; michael@0: // Since default separator is `-`, combination may take form of `alt--`. To michael@0: // avoid misbehavior we replace `--` with `-{{SEPARATOR}}` where michael@0: // `{{SEPARATOR}}` can be swapped later. michael@0: hotkey = hotkey.toLowerCase().replace(separator + separator, separator + SWP); michael@0: michael@0: let value = {}; michael@0: let modifiers = []; michael@0: let keys = hotkey.split(separator); michael@0: keys.forEach(function(name) { michael@0: // If name is `SEPARATOR` than we swap it back. michael@0: if (name === SWP) michael@0: name = separator; michael@0: if (name in MODIFIERS) { michael@0: array.add(modifiers, MODIFIERS[name]); michael@0: } else { michael@0: if (!value.key) michael@0: value.key = name; michael@0: else michael@0: throw new TypeError(INVALID_COMBINATION); michael@0: } michael@0: }); michael@0: michael@0: if (!value.key) michael@0: throw new TypeError(INVALID_COMBINATION); michael@0: michael@0: value.modifiers = modifiers.sort(); michael@0: return value; michael@0: }; michael@0: michael@0: /** michael@0: * Utility function that takes object that defines a `hotkey` and returns michael@0: * string representation of it. michael@0: * michael@0: * _Please note that this function does not validates data neither it normalizes michael@0: * it, if you are unsure that data is well formed use `normalize` function michael@0: * instead. michael@0: * michael@0: * @param {JSON} hotkey michael@0: * @param {String} [separator=" "] michael@0: * Optional string that represents separator used to concatenate keys in the michael@0: * given `hotkey`. michael@0: * @returns {String} michael@0: * @examples michael@0: * michael@0: * require("keyboard/hotkeys").toString({ michael@0: * key: 'b', michael@0: * modifiers: [ 'control', 'shift' ] michael@0: * }, '+'); michael@0: * // 'control+shift+b michael@0: * michael@0: */ michael@0: var toString = exports.toString = function toString(hotkey, separator) { michael@0: let keys = hotkey.modifiers.slice(); michael@0: keys.push(hotkey.key); michael@0: return keys.join(separator || SEPARATOR); michael@0: }; michael@0: michael@0: /** michael@0: * Utility function takes `key` name and returns `true` if it's function key michael@0: * (F1, ..., F24) and `false` if it's not. michael@0: */ michael@0: var isFunctionKey = exports.isFunctionKey = function isFunctionKey(key) { michael@0: var $ michael@0: return key[0].toLowerCase() === 'f' && michael@0: ($ = parseInt(key.substr(1)), 0 < $ && $ < 25); michael@0: };