views:

169

answers:

1

I have been tasked with developing a Firefox add-on that is capable of registering global keyboard shortcuts (ones that will work throughout all areas of Firefox) that will open up the side-bar and execute an XMLRPC request based on previously recorded input. The idea here is that there will be many potential XMLRPC requests that the user will want to execute via a keyboard shortcut.

Currently, the add-on is capable of handling pre-defined static keyboard shortcuts via the Firefox overlay. What I would like to achieve, is to allow the user to register their own dynamic custom keyboard shortcut.

There is an add-on that currently has some of this functionality, called Keyconfig. I'm not keen on having to ask users to install a second add-on to define their own shortcuts. It also seems that using the dynamic keyboard shortcut registration method in Keyconfig would require the user to close all Firefox windows before the dynamic shortcut is made available.

What I would like to know is:

  • Is an XPCOM component the best way to register dynamic keyboard shortcuts from within a Firefox add-on?
  • Is there a way to register the keyboard shortcut so that it is immediately available to all Firefox windows, without having to close the windows beforehand?
+1  A: 

I'm not able to answer "Is an XPCOM component the best way to register dynamic keyboard shortcuts from within a Firefox add-on?", but with the help of a work colleague, we have figured out a way to do the dynamic keyboard shortcuts and make them immediately usable.

window.onkeypress = callback;

Whilst being an egregious hack, as it will execute the callback for every keypress event that the chrome window processes; it does allow for the main Firefox chrome window to have dynamic keyboard shortcuts without necessitating a reload.

To get this to work with the keyboard shortcuts being defined in the sidebar, I've created a listener in the overlay JavaScript's init() method that listens to a custom event from the sidebar:

init: function() {
    var sidebarElement = document.getElementById("sidebar");

    sidebarElement.addEventListener("fooKeyboardShortcut", function shortcutKeyListener(anEvent) {
        fooOverlay.shortcutMap = sidebarElement.contentWindow.foo.getShortcutKeysMap();
    }, true);
},

The object in the sidebar will then fire off one of these events on any state change to the map, which will cause the event listener to assign the map to the object in the Firefox overlay. The mapping key is a composite hash of the following keypress event data members:

  • altKey
  • ctrlKey
  • metaKey
  • shiftKey
  • charCode

It is worth noting here, that keyCode can't be used as it seems to have the value '0' all the time; but charCode does have the correct value.

The value for each key is a callback key for the sidebar object to execute the desired XMLRPC call.

This shortcut key map is then queried for each keypress, and if there is a match; the callback key is used on a pre-registered sidebar callback

dezwart