Search code examples
javascriptfirefox-addonkeyboard-shortcuts

Extension to stop Firefox stealing keystrokes


I have to use Firefox with a web app that uses Emacs-like keystrokes, including ctrl-w to cut text. My Emacs muscle memory often has me hitting ctrl-w without thinking, whereupon my Firefox tab abruptly vanishes, because ctrl-w is a Firefox shortcut to close a tab. This is maddening. Unfortunately, this occurs on a network where I have no admin privileges, and cannot even get permission to install any software from outside without divine intervention. I've found no Firefox setting in about:config that can stop this -- I've googled and googled, and nothing works.

But it occurs to me that I could write a small Firefox extension that intercepts keystrokes before Firefox proper gets hold of them, and just forwards them to the app. Is this possible? I went through this post successfully. Then I tried to write an event listener for keystrokes, following onCommand, as follows,

document.addEventListener("keydown", function(event) {
    console.log("Keydown:", event);
});

window.addEventListener("load", (event) => {
    console.log("page is fully loaded");

    document.addEventListener('keydown', function(e) {
        console.log('document keydown event');
        console.log('key:', e.key);
        console.log('ctrl:', e.getModifierState('Control'));
    }, false);
});

window.addEventListener('keydown', function(e) {
    console.log('keydown event');
    console.log('key:', e.key);
    console.log('ctrl:', e.getModifierState('Control'));
}, false);

The load event shows up on the console, but nothing else. What am I missing?


Solution

  • It turns out that ctrl-w is a reserved key, which causes problems for my proposed solution above. In Firefox's address field, type in

    view-source:chrome://browser/content/browser.xhtml
    

    This shows a list of settings for Firefox. At line 289 or thereabouts, you will see

    <key id="key_close" 
         data-l10n-id="close-shortcut" 
         command="cmd_close" 
         modifiers="accel" 
         reserved="true"/>
    

    cmd_close is the command to close the current tab. Now go to

    view-source:resource:///localization/en-US/browser/browserSets.ftl
    

    and you will find the key binding for ctrl-w:

    close-shortcut =
        .key = W
    

    There is a zip file omni.ja inside the Firefox install where these settings are stored, but without admin privileges they can't be changed. (See here for a patch that makes this change.) A less drastic method from user cor-el involves adding two JavaScript files to the Firefox installation that will accomplish the same thing. Unfortunately, that also requires admin privileges.

    At this point I remembered that Firefox can be installed to a user account without admin privileges. After rooting around for a while, I found a Firefox installer. I installed it, dropped the JavaScript from cor-el into it, and voila, no more problems with ctrl-w.