123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- "use strict";
- module.metadata = {
- "stability": "unstable"
- };
- const { Cc, Ci } = require("chrome");
- const runtime = require("../system/runtime");
- const { isString } = require("../lang/type");
- const array = require("../util/array");
- const SWP = "{{SEPARATOR}}";
- const SEPARATOR = "-"
- const INVALID_COMBINATION = "Hotkey key combination must contain one or more " +
- "modifiers and only one key";
- const MODIFIERS = exports.MODIFIERS = {
- 'accel': runtime.OS === "Darwin" ? 'meta' : 'control',
- 'meta': 'meta',
- 'control': 'control',
- 'ctrl': 'control',
- 'option': 'alt',
- 'command': 'meta',
- 'alt': 'alt',
- 'shift': 'shift'
- };
- const CODES = exports.CODES = new function Codes() {
- let nsIDOMKeyEvent = Ci.nsIDOMKeyEvent;
-
- let aliases = {
- 'subtract': '-',
- 'add': '+',
- 'equals': '=',
- 'slash': '/',
- 'backslash': '\\',
- 'openbracket': '[',
- 'closebracket': ']',
- 'quote': '\'',
- 'backquote': '`',
- 'period': '.',
- 'semicolon': ';',
- 'comma': ','
- };
-
- Object.keys(nsIDOMKeyEvent).filter(function(key) {
-
- return key.indexOf('DOM_VK') === 0;
- }).map(function(key) {
-
- return [ key, nsIDOMKeyEvent[key] ];
- }).map(function([key, value]) {
- return [ key.replace('DOM_VK_', '').replace('_', '').toLowerCase(), value ];
- }).forEach(function ([ key, value ]) {
- this[aliases[key] || key] = value;
- }, this);
- };
- const KEYS = exports.KEYS = new function Keys() {
- Object.keys(CODES).forEach(function(key) {
- this[CODES[key]] = key;
- }, this)
- }
- exports.getKeyForCode = function getKeyForCode(code) {
- return (code in KEYS) && KEYS[code];
- };
- exports.getCodeForKey = function getCodeForKey(key) {
- return (key in CODES) && CODES[key];
- };
- var normalize = exports.normalize = function normalize(hotkey, separator) {
- if (!isString(hotkey))
- hotkey = toString(hotkey, separator);
- return toString(toJSON(hotkey, separator), separator);
- };
- var toJSON = exports.toJSON = function toJSON(hotkey, separator) {
- separator = separator || SEPARATOR;
-
-
-
- hotkey = hotkey.toLowerCase().replace(separator + separator, separator + SWP);
- let value = {};
- let modifiers = [];
- let keys = hotkey.split(separator);
- keys.forEach(function(name) {
-
- if (name === SWP)
- name = separator;
- if (name in MODIFIERS) {
- array.add(modifiers, MODIFIERS[name]);
- } else {
- if (!value.key)
- value.key = name;
- else
- throw new TypeError(INVALID_COMBINATION);
- }
- });
- if (!value.key)
- throw new TypeError(INVALID_COMBINATION);
- value.modifiers = modifiers.sort();
- return value;
- };
- var toString = exports.toString = function toString(hotkey, separator) {
- let keys = hotkey.modifiers.slice();
- keys.push(hotkey.key);
- return keys.join(separator || SEPARATOR);
- };
- var isFunctionKey = exports.isFunctionKey = function isFunctionKey(key) {
- var $
- return key[0].toLowerCase() === 'f' &&
- ($ = parseInt(key.substr(1)), 0 < $ && $ < 25);
- };
|