如何在Firefox中动态更改快捷键?

时间:2022-09-21 05:54:12

I have a firefox extension which can be activated by a shortcut key. I want users to be able to change the key combo dynamically.

我有一个firefox扩展,可以通过快捷键激活。我希望用户能够动态更改密钥组合。

My XUL looks like this

我的XUL看起来像这样

<keyset id="ksMain">
    <key id="keyDoMyThing" modifiers="control,shift" key="e" command="cmdDoMyThing"/>
</keyset>

cmdDoMyThing is a reference to an element in a commandset. When I press ctrl+shift+e, the command fires.

cmdDoMyThing是对命令集中元素的引用。当我按下ctrl + shift + e时,命令会触发。

I tried both modifying the existing element and creating a new element using JavaScript, but while I can get the old key combo to stop working, I can't get the new one to happen. Here's an example of the code I'm using

我尝试修改现有元素并使用JavaScript创建一个新元素,但是虽然我可以让旧的键组合停止工作,但我无法让新的元素发生。这是我正在使用的代码示例

keyelem = document.createElement('key');
keyelem.setAttribute('id', 'keyDoMyThing');
keyelem.setAttribute('command', 'cmdDoMyThing');
keyelem.setAttribute('key', key);
keyelem.setAttribute('modifiers', modstr);
keyset.appendChild(keyelem);

I can use the debugger to verify that modstr is set to the proper string and key is set to the key I want to use.

我可以使用调试器来验证modstr是否设置为正确的字符串,并将键设置为我想要使用的键。

How can I make this happen the way I would like?

我怎样才能按照我想要的方式实现这一目标?

1 个解决方案

#1


Turns out I need to delete the keyset as well and create an entirely new keyset.

事实证明我还需要删除键集并创建一个全新的键集。

ksparent = keyset.parentNode
ksparent.removeChild(keyset);

keyset = document.createElement('keyset');
keyset.id = 'my-keyset';

keyelem = document.createElement('key');
keyelem.setAttribute('id', 'keyDoMyThing');
keyelem.setAttribute('command', 'cmdDoMyThing');
keyelem.setAttribute('key', key);
keyelem.setAttribute('modifiers', modstr);

keyset.appendChild(keyelem);
ksparent.appendChild(keyset);

Once this is done, the new key combo will take effect.

完成此操作后,新的密钥组合将生效。

#1


Turns out I need to delete the keyset as well and create an entirely new keyset.

事实证明我还需要删除键集并创建一个全新的键集。

ksparent = keyset.parentNode
ksparent.removeChild(keyset);

keyset = document.createElement('keyset');
keyset.id = 'my-keyset';

keyelem = document.createElement('key');
keyelem.setAttribute('id', 'keyDoMyThing');
keyelem.setAttribute('command', 'cmdDoMyThing');
keyelem.setAttribute('key', key);
keyelem.setAttribute('modifiers', modstr);

keyset.appendChild(keyelem);
ksparent.appendChild(keyset);

Once this is done, the new key combo will take effect.

完成此操作后,新的密钥组合将生效。