Firefox SDK:如何“捕获”用户在首选项中的热键? [英] Firefox SDK: how "capture" users hotkey in preferences?

查看:139
本文介绍了Firefox SDK:如何“捕获”用户在首选项中的热键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package.json 我有:

 preferences :{
name:hotkeyPopup,
title:翻译所选文本的热键,
type:string,
value :alt-Y
}]

它看起来像 input type = text 。如何捕获用户的热键组合?这不是很酷,用户必须使用像 alt 或甚至更糟的 accel



有关热键的官方文档在这个片段窗口中可以是一个内容窗口(tab / iframe /等等)或xul窗口(nsIDOMWindow)

非常基本的非常详细,没有技巧。未经测试。

 函数enterHotkeyRecord(){
window.addEventListener('keydown',downed,false);
window.addEventListener('keyup',upped,false);
window.addEventListener('keypress',按下,false);


function exitHotkeyRecord(){
window.removeEventListener('keydown',downed,false);
window.removeEventListener('keyup',upped,false);
window.removeEventListener('keypress',按下,false);


函数按(e){
e.preventDefault();
e.stopPropagation(); (b)


函数上调(e){
e.preventDefault();
e.stopPropagation();


function downed(e){
e.preventDefault();

if(e.repeat){
//如果按住一个键多次触发,忽略它
return;
}


var key = String.fromCharCode(e.code);

var str = [];

if(e.keyCode == 27){
// user hit escape so let exit b $ b 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();


In package.json I have:

"preferences": [{
    "name": "hotkeyPopup",
    "title": "Hotkey for translating selected text",
    "type": "string",
    "value": "alt-Y"
  }]

And it looks like input type=text. How can I capture user's hotkey combination? This is not cool that user must type by hands words like alt or even worse accel.

Official documentation about hotkeys say nothing about capturing in preferences.

解决方案

In this snippet window can be a content window (tab/iframe/etc) or xul window (nsIDOMWindow)

Very basic very verbose, no tricks. Untested.

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();

这篇关于Firefox SDK:如何“捕获”用户在首选项中的热键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆