Handle shortcuts with NumPad in background

Hi! I’m working on extension for Firefox and I want to make it handle keyboard shortcuts on whatever page user is.

It must be like using commands API as it shown in example, but the problem is there are no support for desired keys. In particular, I’d be like to handle combilations of Ctrl, Shift and keys from num pad. Commands API allows to handle only numbers in typewriter block. I don’t think it will be conveniently for users. And there is no support for keys “+”, “-”, “*”, “/” at all.

Does anyone have ideas?

If the commands API doen’t cut it, then you have two options:

  1. Install listeners on all web pages
  2. Use native messaging

The disadvantage of the first approach is that is quite messy and will only ever work while the user has a web page (i.e. not the browsers UI or about: pages, etc.).
The disadvantage of the second approach is, that the user needs to install a native application (which is messy) and that your key listeners are OS specific and most likely OS global. On the plus side, you can use virtually every key combination (even ones that would totally hinder normal workflows).

It shouldn’t be to hard to find examples for and implement the first approach.

I started an implementation of the native messaging approach here:


It is totally WIP, only works on Windows and is mostly undocumented. Especially, there is not even usage documentation yet. What it currently does is install Alt + Shift + (A, S, D) listeners to control playback on Spotifys inline player.
It requires NativeExt to be installed and set up (but that is documented).

If you have any further questions, feel free to ask!

I tried to set onkeydown/onkeyup handlers for window and document in background.js, but couldn’t get any reaction. What did you mean in 1st option?

And does such method conflict with the security system?

I tried to set onkeydown/onkeyup handlers for window and document in background.js, but couldn’t get any reaction. What did you mean in 1st option?

That is not surprising. The background page is never focused and will thus never receive input. If you want to go down this road, you need to use content scripts to “Install listeners on all web pages”. See the limitations above.

And does such method conflict with the security system?

If you do not filter events or don’t attach your listeners correctly, web pages could spoof the keyboard events.

With content script I managed to handle keydown event but there is new question: can I not to pass this event to browser so it wont do it’s default action? I mean zoom in/zoom out when user press Ctrl++/Ctrl+- etc.

Would be good to control it right in handler function because my shortcuts must be handled in certain cases only. I don’t want to deprive users of default browser features.

You can call .preventDefault() on the event. That should, as the name says, prevent the default action. You should try this from keydown, keypress etc. and see which works. Also, you need to call it synchronously (immediately), no no, you can ask your background script whether it want’s to handle the event first.

1 Like