Firefox SDK:"capture" 用户如何在首选项中使用热键?
Firefox SDK: how "capture" users hotkey in preferences?
在 package.json
我有:
"preferences": [{
"name": "hotkeyPopup",
"title": "Hotkey for translating selected text",
"type": "string",
"value": "alt-Y"
}]
看起来像 input type=text
。如何捕获用户的热键组合?用户必须手动输入 alt
甚至更糟的 accel
.
这样的词,这并不酷
Official documentation about hotkeys 不用说在首选项中捕获。
在此代码段中 window 可以是内容 window (tab/iframe/etc) 或 xul window (nsIDOMWindow)
非常基础非常冗长,没有技巧。未经测试。
function enterHotkeyRecord() {
window.addEventListener('keydown', downed, false);
window.addEventListener('keyup', upped, false);
window.addEventListener('keypress', pressed, false);
}
function exitHotkeyRecord() {
window.removeEventListener('keydown', downed, false);
window.removeEventListener('keyup', upped, false);
window.removeEventListener('keypress', pressed, false);
}
function pressed(e) {
e.preventDefault();
e.stopPropagation();
}
function upped(e) {
e.preventDefault();
e.stopPropagation();
}
function downed(e) {
e.preventDefault();
if (e.repeat) {
// if hold down a key it fires multiple times so ignore it
return;
}
var key = String.fromCharCode(e.code);
var str = [];
if (e.keyCode == 27) {
// user hit escape so lets exit
enterHotkeyRecord();
return;
}
if (e.altKey) {
str.push('Alt');
}
if (e.shiftKey) {
str.push('Shift');
}
if (e.metaKey) {
str.push('Meta');
}
if (e.ctrlKey) {
str.push('Ctrl');
}
str.push(key);
console.log('you pressed:', str.join(' + '));
}
enterHotkeyRecord();
在 package.json
我有:
"preferences": [{
"name": "hotkeyPopup",
"title": "Hotkey for translating selected text",
"type": "string",
"value": "alt-Y"
}]
看起来像 input type=text
。如何捕获用户的热键组合?用户必须手动输入 alt
甚至更糟的 accel
.
Official documentation about hotkeys 不用说在首选项中捕获。
在此代码段中 window 可以是内容 window (tab/iframe/etc) 或 xul window (nsIDOMWindow)
非常基础非常冗长,没有技巧。未经测试。
function enterHotkeyRecord() {
window.addEventListener('keydown', downed, false);
window.addEventListener('keyup', upped, false);
window.addEventListener('keypress', pressed, false);
}
function exitHotkeyRecord() {
window.removeEventListener('keydown', downed, false);
window.removeEventListener('keyup', upped, false);
window.removeEventListener('keypress', pressed, false);
}
function pressed(e) {
e.preventDefault();
e.stopPropagation();
}
function upped(e) {
e.preventDefault();
e.stopPropagation();
}
function downed(e) {
e.preventDefault();
if (e.repeat) {
// if hold down a key it fires multiple times so ignore it
return;
}
var key = String.fromCharCode(e.code);
var str = [];
if (e.keyCode == 27) {
// user hit escape so lets exit
enterHotkeyRecord();
return;
}
if (e.altKey) {
str.push('Alt');
}
if (e.shiftKey) {
str.push('Shift');
}
if (e.metaKey) {
str.push('Meta');
}
if (e.ctrlKey) {
str.push('Ctrl');
}
str.push(key);
console.log('you pressed:', str.join(' + '));
}
enterHotkeyRecord();