/*\ title: $:/core/modules/keyboard.js type: application/javascript module-type: global Keyboard handling utilities \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; var namedKeys = { "cancel": 3, "help": 6, "backspace": 8, "tab": 9, "clear": 12, "return": 13, "enter": 13, "pause": 19, "escape": 27, "space": 32, "page_up": 33, "page_down": 34, "end": 35, "home": 36, "left": 37, "up": 38, "right": 39, "down": 40, "printscreen": 44, "insert": 45, "delete": 46, "0": 48, "1": 49, "2": 50, "3": 51, "4": 52, "5": 53, "6": 54, "7": 55, "8": 56, "9": 57, "firefoxsemicolon": 59, "firefoxequals": 61, "a": 65, "b": 66, "c": 67, "d": 68, "e": 69, "f": 70, "g": 71, "h": 72, "i": 73, "j": 74, "k": 75, "l": 76, "m": 77, "n": 78, "o": 79, "p": 80, "q": 81, "r": 82, "s": 83, "t": 84, "u": 85, "v": 86, "w": 87, "x": 88, "y": 89, "z": 90, "numpad0": 96, "numpad1": 97, "numpad2": 98, "numpad3": 99, "numpad4": 100, "numpad5": 101, "numpad6": 102, "numpad7": 103, "numpad8": 104, "numpad9": 105, "multiply": 106, "add": 107, "separator": 108, "subtract": 109, "decimal": 110, "divide": 111, "f1": 112, "f2": 113, "f3": 114, "f4": 115, "f5": 116, "f6": 117, "f7": 118, "f8": 119, "f9": 120, "f10": 121, "f11": 122, "f12": 123, "f13": 124, "f14": 125, "f15": 126, "f16": 127, "f17": 128, "f18": 129, "f19": 130, "f20": 131, "f21": 132, "f22": 133, "f23": 134, "f24": 135, "firefoxminus": 173, "semicolon": 186, "equals": 187, "comma": 188, "dash": 189, "period": 190, "slash": 191, "backquote": 192, "openbracket": 219, "backslash": 220, "closebracket": 221, "quote": 222 }; function KeyboardManager(options) { var self = this; options = options || ""; // Save the named key hashmap this.namedKeys = namedKeys; // Create a reverse mapping of code to keyname this.keyNames = []; $tw.utils.each(namedKeys,function(keyCode,name) { self.keyNames[keyCode] = name.substr(0,1).toUpperCase() + name.substr(1); }); // Save the platform-specific name of the "meta" key this.metaKeyName = $tw.platform.isMac ? "cmd-" : "win-"; this.shortcutKeysList = [], // Stores the shortcut-key descriptors this.shortcutActionList = [], // Stores the corresponding action strings this.shortcutParsedList = []; // Stores the parsed key descriptors this.shortcutPriorityList = []; // Stores the parsed shortcut priority this.lookupNames = ["shortcuts"]; this.lookupNames.push($tw.platform.isMac ? "shortcuts-mac" : "shortcuts-not-mac") this.lookupNames.push($tw.platform.isWindows ? "shortcuts-windows" : "shortcuts-not-windows"); this.lookupNames.push($tw.platform.isLinux ? "shortcuts-linux" : "shortcuts-not-linux"); this.updateShortcutLists(this.getShortcutTiddlerList()); $tw.wiki.addEventListener("change",function(changes) { self.handleShortcutChanges(changes); }); } /* Return an array of keycodes for the modifier keys ctrl, shift, alt, meta */ KeyboardManager.prototype.getModifierKeys = function() { return [ 16, // Shift 17, // Ctrl 18, // Alt 20, // CAPS LOCK 91, // Meta (left) 93, // Meta (right) 224 // Meta (Firefox) ] }; /* Parses a key descriptor into the structure: { keyCode: numeric keycode shiftKey: boolean altKey: boolean ctrlKey: boolean metaKey: boolean } Key descriptors have the following format: ctrl+enter ctrl+shift+alt+A */ KeyboardManager.prototype.parseKeyDescriptor = function(keyDescriptor,options) { var components = keyDescriptor.split(/\+|\-/), info = { keyCode: 0, shiftKey: false, altKey: false, ctrlKey: false, metaKey: false }; for(var t=0; t