Skip to content

Reclaim Your Keyboard Shortcuts in Firefox

Firefox - unlike Chrome - allows any web application to register any keyboard shortcuts (y'know the infamous CTRL + C). This might be a problem for users relying on keyboard navigation or even just general keyboard controls. This post shows a way prevent websites from capturing and muting essential commands such as CTRL + W (to close a tab).


TL;DR: use GreaseMonkey to run anti-keygrabber.user.js to prevent web apps from muting CTRL + W et al.

For sake of brevity I'll be talking about the CTRL (Control) key [Windows, Linux] and also mean CMD (Command) key [Mac].


I'm a developer. I type a lot. My mouse is often not more than a decorative paperweight. If I know how to control an app using only the keyboard, I'll do that if it is more convenient than moving my hand from keyboard to mouse. I love web apps that provide elaborate keyboard shortcuts. For a great example go to Github and press ? to find out that pressing s focuses the search box. Rich text editors often map CTRL + B to make the selected text bold, CTRL + I to make it italic. I love this kind of stuff. To me, a perfectly healthy guy, this is just convenience. To handicapped people, say a blind person, it's more than just a nice gimmick - keyboard control is a necessity.

As a frontend developer I often use tools like jsbin.com, jsfiddle.net and codepen.io. On any of these sites, pressing CTRL + T will not open a new tab for me. codepen.io even goes as far as preventing me from closing a tab by pressing CTRL + W. Remember how I said I love web apps providing keyboard shortcuts? I take it back! Some of it, at least.

The coding tools I mentioned all use CodeMirror for »bearable code editing«. Some presumably activated emacs.js keymap, others added their own custom handlers. Remy Sharp (author of jsbin.com) pointed out that you could configure the tool to use CTRL+ALT instead of just CTRL in Help > Keyboard shortcuts:

Either way, keyboard shortcut hijacking got out of control. Just fixing those tools would be a start, but doesn't reliably solve the problem for me. I want web apps to provide keyboard access, but deny them registering shortcuts that are bound to vital functions such as opening and closing tabs.

Firefox does not provide such a configuration option. In fact, this whole overriding default shortcuts is an age old discussion as @slsoftworks pointed out. Chrome users are "safe" because they considered this to be a performance issue:

Anyways, meet GreaseMonkey - a Firefox plugin that allows running arbitrary JavaScript on any website you visit. GreaseMonkey has become my best pal whenever a website annoys the hell out of me - again with this case. anti-keygrabber.user.js filters pressed keys to find combinations such as CTRL + W, prevent the web app from running their custom handlers and thus allowing the browser to do whatever it should've done in the first place.

// ==UserScript==
// @name anti key-grabber
// @description Prevent web apps from capturing and muting vital keyboard shortcuts
// @grant none
// @version 1.1
// ==/UserScript==
(function(){
var isMac = unsafeWindow.navigator.oscpu.toLowerCase().contains("mac os x");
unsafeWindow.document.addEventListener('keydown', function(e) {
  if (e.keyCode === 116) {
    // F5 should never be captured
    e.stopImmediatePropagation();
    e.stopPropagation();
    return;
  }
 
  // Mac uses the Command key, identified as metaKey
  // Windows and Linux use the Control key, identified as ctrlKey
  var modifier = isMac ? e.metaKey : e.ctrlKey;
  // abort if the proper command/control modifier isn't pressed
  if (!modifier) {
    return;
  }
 
  switch (e.keyCode) {
    case 87: // W - close window
    case 84: // T - open tab
    case 76: // L - focus awesome bar
    case 74: // J - open downloads panel
      e.stopImmediatePropagation();
      e.stopPropagation();
      return;
  }
 
  // s'more mac love
  if (!isMac) {
      return;
  }
 
  switch (e.keyCode) {
    case 37: // ⃖ - left key - switching tabs
    case 39: // ⃗ - right key - switching tabs
    case 188: // , (comma) - open settings [mac]
    case 82: // R - reload tab
      e.stopImmediatePropagation();
      e.stopPropagation();
      return;
  }
}, true);
})();

Yes… this is the sledge hammer way of doing things. If an app relies on binding these default browser shortcuts, you're shit out of luck.


Update

Mere minutes after I published this post jsbin.com is the first to fix their keyboard bindings (by throwing out the offending ones). Thanks Remy!

:)

Comments

Display comments as Linear | Threaded

No comments

The author does not allow comments to this entry