Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | module.metadata = { |
michael@0 | 8 | "stability": "unstable" |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | const { observer: keyboardObserver } = require("./observer"); |
michael@0 | 12 | const { getKeyForCode, normalize, isFunctionKey, |
michael@0 | 13 | MODIFIERS } = require("./utils"); |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * Register a global `hotkey` that executes `listener` when the key combination |
michael@0 | 17 | * in `hotkey` is pressed. If more then one `listener` is registered on the same |
michael@0 | 18 | * key combination only last one will be executed. |
michael@0 | 19 | * |
michael@0 | 20 | * @param {string} hotkey |
michael@0 | 21 | * Key combination in the format of 'modifier key'. |
michael@0 | 22 | * |
michael@0 | 23 | * Examples: |
michael@0 | 24 | * |
michael@0 | 25 | * "accel s" |
michael@0 | 26 | * "meta shift i" |
michael@0 | 27 | * "control alt d" |
michael@0 | 28 | * |
michael@0 | 29 | * Modifier keynames: |
michael@0 | 30 | * |
michael@0 | 31 | * - **shift**: The Shift key. |
michael@0 | 32 | * - **alt**: The Alt key. On the Macintosh, this is the Option key. On |
michael@0 | 33 | * Macintosh this can only be used in conjunction with another modifier, |
michael@0 | 34 | * since `Alt+Letter` combinations are reserved for entering special |
michael@0 | 35 | * characters in text. |
michael@0 | 36 | * - **meta**: The Meta key. On the Macintosh, this is the Command key. |
michael@0 | 37 | * - **control**: The Control key. |
michael@0 | 38 | * - **accel**: The key used for keyboard shortcuts on the user's platform, |
michael@0 | 39 | * which is Control on Windows and Linux, and Command on Mac. Usually, this |
michael@0 | 40 | * would be the value you would use. |
michael@0 | 41 | * |
michael@0 | 42 | * @param {function} listener |
michael@0 | 43 | * Function to execute when the `hotkey` is executed. |
michael@0 | 44 | */ |
michael@0 | 45 | exports.register = function register(hotkey, listener) { |
michael@0 | 46 | hotkey = normalize(hotkey); |
michael@0 | 47 | hotkeys[hotkey] = listener; |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Unregister a global `hotkey`. If passed `listener` is not the one registered |
michael@0 | 52 | * for the given `hotkey`, the call to this function will be ignored. |
michael@0 | 53 | * |
michael@0 | 54 | * @param {string} hotkey |
michael@0 | 55 | * Key combination in the format of 'modifier key'. |
michael@0 | 56 | * @param {function} listener |
michael@0 | 57 | * Function that will be invoked when the `hotkey` is pressed. |
michael@0 | 58 | */ |
michael@0 | 59 | exports.unregister = function unregister(hotkey, listener) { |
michael@0 | 60 | hotkey = normalize(hotkey); |
michael@0 | 61 | if (hotkeys[hotkey] === listener) |
michael@0 | 62 | delete hotkeys[hotkey]; |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * Map of hotkeys and associated functions. |
michael@0 | 67 | */ |
michael@0 | 68 | const hotkeys = exports.hotkeys = {}; |
michael@0 | 69 | |
michael@0 | 70 | keyboardObserver.on("keydown", function onKeypress(event, window) { |
michael@0 | 71 | let key, modifiers = []; |
michael@0 | 72 | let isChar = "isChar" in event && event.isChar; |
michael@0 | 73 | let which = "which" in event ? event.which : null; |
michael@0 | 74 | let keyCode = "keyCode" in event ? event.keyCode : null; |
michael@0 | 75 | |
michael@0 | 76 | if ("shiftKey" in event && event.shiftKey) |
michael@0 | 77 | modifiers.push("shift"); |
michael@0 | 78 | if ("altKey" in event && event.altKey) |
michael@0 | 79 | modifiers.push("alt"); |
michael@0 | 80 | if ("ctrlKey" in event && event.ctrlKey) |
michael@0 | 81 | modifiers.push("control"); |
michael@0 | 82 | if ("metaKey" in event && event.metaKey) |
michael@0 | 83 | modifiers.push("meta"); |
michael@0 | 84 | |
michael@0 | 85 | // If it's not a printable character then we fall back to a human readable |
michael@0 | 86 | // equivalent of one of the following constants. |
michael@0 | 87 | // http://mxr.mozilla.org/mozilla-central/source/dom/interfaces/events/nsIDOMKeyEvent.idl |
michael@0 | 88 | key = getKeyForCode(keyCode); |
michael@0 | 89 | |
michael@0 | 90 | // If only non-function (f1 - f24) key or only modifiers are pressed we don't |
michael@0 | 91 | // have a valid combination so we return immediately (Also, sometimes |
michael@0 | 92 | // `keyCode` may be one for the modifier which means we do not have a |
michael@0 | 93 | // modifier). |
michael@0 | 94 | if (!key || (!isFunctionKey(key) && !modifiers.length) || key in MODIFIERS) |
michael@0 | 95 | return; |
michael@0 | 96 | |
michael@0 | 97 | let combination = normalize({ key: key, modifiers: modifiers }); |
michael@0 | 98 | let hotkey = hotkeys[combination]; |
michael@0 | 99 | |
michael@0 | 100 | if (hotkey) { |
michael@0 | 101 | try { |
michael@0 | 102 | hotkey(); |
michael@0 | 103 | } catch (exception) { |
michael@0 | 104 | console.exception(exception); |
michael@0 | 105 | } finally { |
michael@0 | 106 | // Work around bug 582052 by preventing the (nonexistent) default action. |
michael@0 | 107 | event.preventDefault(); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | }); |