This repository has been archived by the owner on Feb 15, 2018. It is now read-only.
/
keys.js
164 lines (134 loc) · 4.23 KB
/
keys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {Ci} = require("chrome");
const prefs = require("sdk/preferences/service");
const winUtils = require("sdk/deprecated/window-utils");
const KEY_LABELS = {
"focusURLBar": "Focus URL Bar",
"focusURLBar2": "Focus URL Bar",
"key_search": "Web Search",
"key_search2": "Web Search",
"key_stop": "Stop",
"key_stop_mac": "Stop",
"goHome": "Home",
"goBackKb": "Back",
"goBackKb2": "Back 2",
"goForwardKb": "Forward",
"goForwardKb2": "Forward",
"key_close": "Close Tab",
"key_undoCloseTab": "Undo Close Tab",
"key_undoCloseWindow": "Undo Close Window",
"key_toggleAddonBar": "Toggle Add-on Bar",
"key_findPrevious": "Find Previous",
"key_findSelection": "Find Selection",
"key_devToolboxMenuItemF12": "Toggle Tools",
"key_reload": "Reload",
"key_reload2": "Reload",
"key_selectLastTab": "Select Last Tab",
"key_selectTab1": "Select Tab 1",
"key_selectTab2": "Select Tab 2",
"key_selectTab3": "Select Tab 3",
"key_selectTab4": "Select Tab 4",
"key_selectTab5": "Select Tab 5",
"key_selectTab6": "Select Tab 6",
"key_selectTab7": "Select Tab 7",
"key_selectTab8": "Select Tab 8",
"key_tabview": "Tab Groups",
"key_minimizeWindow": "Minimize Window",
"key_sanitize_mac": "Clear Recent History…",
"key_fullScreen_old": "Enter Full Screen",
"focusChatBar": "Focus Chat Bar"
};
function getActiveDocument() {
return winUtils.activeBrowserWindow.document;
}
function querySelector(sel) {
return getActiveDocument().querySelector(sel);
}
function querySelectorAll(sel) {
return getActiveDocument().querySelectorAll(sel);
}
function getLabel(element) {
let label = element.getAttribute("label");
if (label) {
return label;
}
let id = element.id;
if (id in KEY_LABELS) {
return KEY_LABELS[id];
}
// Try to find a menuitem...
let menuitem = querySelector("menuitem[key=" + id + "][label]");
return menuitem ? menuitem.getAttribute("label") : id;
}
function valueForKeyCode(keyCode) {
if (keyCode == "VK_BACK") {
keyCode = "VK_BACK_SPACE";
}
return Ci.nsIDOMKeyEvent["DOM_" + keyCode] || null;
}
const MODIFIER_KEYS = {16: "shift", 17: "control", 18: "alt", 91: "meta", 92: "meta", 224: "meta"};
function parseModifiers(attr) {
let modifiers = attr.toLowerCase().split(/[,\s]/);
let idx = modifiers.indexOf("accel");
if (idx > -1) {
modifiers[idx] = MODIFIER_KEYS[prefs.get("ui.key.accelKey")] || "control";
}
return modifiers;
}
function addCustomKeys(keys) {
keys["key_switchToNextTab"] = {
label: "Switch To Next Tab",
modifiers: ["meta", "shift"],
code: Ci.nsIDOMKeyEvent.DOM_VK_CLOSE_BRACKET,
text: "]"
};
keys["key_switchToPrevTab"] = {
label: "Switch To Previous Tab",
modifiers: ["meta", "shift"],
code: Ci.nsIDOMKeyEvent.DOM_VK_OPEN_BRACKET,
text: "["
};
keys["key_nextTabGroup"] = {
label: "Switch To Next Tab Group",
modifiers: ["control"],
code: Ci.nsIDOMKeyEvent.DOM_VK_BACK_QUOTE,
text: "`"
};
keys["key_prevTabGroup"] = {
label: "Switch To Previous Tab Group",
modifiers: ["control", "shift"],
code: Ci.nsIDOMKeyEvent.DOM_VK_BACK_QUOTE,
text: "`"
};
}
exports.get = function () {
let keys = {};
for (let key of querySelectorAll("key[id]")) {
let id = key.getAttribute("id");
let data = {id: id, label: getLabel(key), modifiers: []};
if (key.hasAttribute("modifiers")) {
data.modifiers = parseModifiers(key.getAttribute("modifiers"));
}
if (key.hasAttribute("keycode")) {
let value = key.getAttribute("keycode");
data.code = valueForKeyCode(value);
let name = value.replace(/^VK_/, "");
name = name[0] + name.substr(1).toLowerCase();
data.text = name.replace(/_[a-z]/i, str => str[1].toUpperCase());
} else {
data.text = key.getAttribute("key").toUpperCase();
data.code = data.text.charCodeAt(0);
}
// TODO Escape -> Esc
// TODO Backspace -> Delete
if (data.code && data.text) {
keys[id] = data;
}
}
// Add custom key definitions.
addCustomKeys(keys);
return keys;
};