99

Many websites, especially everything involving rich text editing (this site is guilty as well), steal keyboard shortcuts normally used to control Firefox and make them do something else instead. It is totally infuriating when I press something like Cmd-number, Cmd-L, Cmd-T, or Cmd-K and it doesn't do what I want it to. Can I make it stop?

Actually, it would probably be for the best if I could forbid stealing of all Cmd-* shortcuts. I've never seen them used for anything useful. Is it possible?

5
  • 8
    Agreed, it's infuriating. FogBugz has a very good implementation of keyboard shortcuts. CTRL-; enters shortcut mode and highlights all the commands visible on the screen with the available shortcuts. Every shortcut is a combo, so new case is CTRL-; N and edit is CTRL-; E. Very easy to get used to and zero conflicts. I wish more sites would use something like this 'cause it's easier for the user and doesn't override browser shortcuts. Too bad SuperUser doesn't do this since it's from same people as FogBugz.
    – Sam
    Commented Jul 30, 2010 at 19:57
  • See also superuser.com/questions/399352/… Commented Oct 4, 2013 at 5:47
  • 2
    This is being discussed at this feature request on Bugzilla. Commented Oct 4, 2013 at 5:49
  • I visit lots of intranet pages at work that somehow mess up cmd+N, so I can never open new browser windows from the keyboard! So annoying! Commented May 12, 2016 at 5:14
  • 2
    After ~12 years, Mozilla has stabilized a pretty reasonable fix for this. It's well-hidden and imperfect, but it might save your sanity. Please see my answer here: superuser.com/a/1317514/158390
    – Lambart
    Commented May 1, 2019 at 17:41

11 Answers 11

32

Thanks to Greasemonkey's new @run-at property, this is now possible!

I took inspiration from this script and this script to combine them into a Userscript that sucessfully intercepts the keyboard shortcuts Ctrl+T and Ctrl+S. I tested in in Firefox 17 ESR and Firefox 25.

// ==UserScript==
// @name           Disable Ctrl+s and Ctrl+t interceptions
// @description    Stop websites from highjacking keyboard shortcuts
//
// @run-at         document-start
// @include        *
// @grant          none
// ==/UserScript==

// Keycode for 's' and 't'. Add more to disable other ctrl+X interceptions
keycodes = [83, 84];  

(window.opera ? document.body : document).addEventListener('keydown', function(e) {
    // alert(e.keyCode ); //uncomment to find more keyCodes
    if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
    // alert("Gotcha!"); //ucomment to check if it's seeing the combo
    }
    return false;
}, !window.opera);
11
  • 2
    This was very helpful. OS X users should swap out e.ctrlKey for e.cmdKey and e.cmdKey && e.shiftKey to reclaim most of their browser shortcuts.
    – JamesGecko
    Commented Apr 9, 2014 at 17:04
  • This script works for me to fix CTRL+TAB by adding 9 to the keycodes array. Thanks! Commented Aug 12, 2014 at 23:09
  • 2
    @JamesGecko Found out via this answer you should/could use the e.metaKey for the command key.
    – riezebosch
    Commented Apr 1, 2015 at 9:08
  • 3
    This code works! Keep in mind that it filters not only Ctrl+Key, but also Ctrl+Alt+Key, Ctrl+Shift+Key and Ctrl+Alt+Shift+Key, because it only checks for the state of the Ctrl modifier.
    – RomanSt
    Commented Oct 26, 2015 at 23:28
  • 1
    Used this to suppress ctrl+# tab navigation stealing in discord's webapp. Used: keycodes = [49, 50, 51, 52, 53, 54, 55, 56, 57, 48]; Commented Aug 26, 2016 at 14:12
58

11 years after the bug was filed, Mozilla finally got to work on this popular feature request, and it seems to be working okay now (tested in Firefox 66.0.3/Ubuntu).

(Thanks to @PerJohansson for pointing out that they've made the setting much more difficult to find since FF 59.)

I've just updated this answer with some easier steps, tested with Firefox 75.

NOTE that if you'd like to disable shortcuts for all sites by default, navigate to about:config, and set the value of permissions.default.shortcuts to 2. Thanks to @inetknght for the tip, which is also documented here: https://support.mozilla.org/en-US/questions/1241294#answer-1175070

For disabling shortcuts on a per-site basis:

  1. Press Ctrli (or Tools > Page Info menu) to show the Page Info window
  2. Select the Permissions tab Firefox Page Info window
  3. Adjust the Override Keyboard Shortcuts setting. Firefox Permissions tab

Mozilla has more information on the Page Info window here:

https://support.mozilla.org/en-US/kb/firefox-page-info-window

And if you're interested in the history of this fix, here are the related Mozilla tickets: https://bugzilla.mozilla.org/show_bug.cgi?id=380637 and https://bugzilla.mozilla.org/show_bug.cgi?id=1445942

9
  • 3
    In Firefox 64 (unsure where it appeared), you have to Click "Connection" => "More information" to access this idalog box. The permissions cogwheel goes into the Preferences instead which is not the right place. Commented Jan 14, 2019 at 7:25
  • Thanks @PerJohansson, I've updated the ticket.
    – Lambart
    Commented Jan 14, 2019 at 18:03
  • 13
    What is the about:config option to change the default? I want to force all sites to not be able to hook into my keyboard
    – inetknght
    Commented Oct 31, 2019 at 15:35
  • I'm using Firefox 71.0 on macOS Catalina with this option blocked,this site still can eat my Ctrl-B as bold text,which I want to move my cursor back.
    – Renkai
    Commented Dec 6, 2019 at 3:21
  • 3
    @inetknght the about:config option to change the default is permissions.default.shortcuts. Set it to 2 to disallow websites overriding any shortcuts unless manually allowed via "Page Info".
    – V.S.
    Commented Feb 7, 2022 at 10:51
5

Extensive research shows that as of current version of Firefox (3.6.x) this is impossible - all key binding conflicts are resolved with priorities: System > Website > Firefox - a rather stupid order that is. None of addons I've tried seems to be able to fix it.

Possibly it might become doable in future versions, but right now the answer is - Impossible.

2
  • 1
    Using FF 30.0 almost 4 years later and this is still an issue. I'll try Martin's Greasemonkey idea.
    – LGT
    Commented Jul 28, 2014 at 20:59
  • 2
    Using FF 53.0 almost 8 years later and this is still an issue.
    – Daniel
    Commented Jun 7, 2017 at 11:40
5

Since the issues seems to be JavaScript keyboard events stealing keypresses, would it not be possible to build a JavaScript script (to be used via Greasemonkey) that unbinds these all keyboard events, thus returning the proper usage of each shortcut to the browser?

I'm not sure how feasible this is, but someone with more JavaScript / Greasemonkey experiance may be able to help (might be worth asking on SO).

1
  • 2
    This works through onKeyPress mechanism - Firefox sends every keypress to website first, and only looks at it afterwards if it was not canceled or intercepted. Some Greasemonkey magic that would intercept keypresses before website and somehow run firefox functions directly might be possible, but it's far from obvious.
    – taw
    Commented Aug 5, 2010 at 16:54
4

The problem is that any page can run Javascript that sets up an event handler to grab keypress events, and Firefox's javascript controls aren't sufficiently fine-grained to stop it without breaking other javascript features.

The only way to prevent this is to disable Javascript (Tools -> Options, [Content] tab, uncheck the Enable JavaScript). Or you can disable Javascript on a per-site basis with an extension like NoScript.

Firefox lets you prevent certain uses of Javascript, like moving/resizing windows, changing or disabling the context menu, etc; but there's nothing to prevent web-sites intercepting keyboard events.

Maybe there's an extension which gives this level of control - I'm not aware of one.
There's Javascript Options, but that extension is no longer being updated.

1
  • 2
    Javascript Options and a few other extensions I tried don't support this. Blocking all javascript would make the web pretty much unusable, this isn't really an option.
    – taw
    Commented Jul 31, 2010 at 2:09
1

After much testings on various browsers, it is easier to intercept the keys when they are down (not pressed) because some of this "App integrated keys" are difficult to intercept with the "keypress" event.

I came up with this script that is sort of cross browser compatible (I didn't test for Microsoft's IE). Notice that the browsers return different codes for some keys. In my case I wanted to prevent Ctrl+P.

The key "P" on chrome is seen as e.keyCode == 80, on opera, it is e.charCode == 16, while on firefox it is e.charCode == 112

$(document).on('keydown', function(e) {
    if(e.ctrlKey && (e.key == "p" || e.charCode == 16 || e.charCode == 112 || e.keyCode == 80) ){
        alert("Please use the Print PDF button below for a better rendering on the document");
        e.cancelBubble = true;
        e.preventDefault();

        e.stopImmediatePropagation();
    }  
});

I used jQuery.

1

If you want to disable any ctrl-key being taken over by the webpage, just filter for the all the letter's codes from a-z (building on the previously accepted and working answer)

// ==UserScript==
// @name           Disable Ctrl+key interceptions
// @description    Stop websites from highjacking keyboard shortcuts
//
// @run-at         document-start
// @include        *
// @grant          none
// ==/UserScript==

(window.opera ? document.body : document).addEventListener('keydown', function(e) {
    //alert(e.keyCode ); //uncomment to find more keyCodes
    if( e.ctrlKey && e.keyCode>=65 && e.keyCode<=90 ) {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
    }
    return false;
}, !window.opera);
2
  • What is the point of checking for keyCode >= 65 and <= 90? I don't actually know what it is for. Also, if the idea is to always prevent the website from hijacking any ctrl based shortcut, would just having if (e.ctrlKey) be enough? Commented Apr 22, 2022 at 15:18
  • @still_dreaming_1 If you just want the control key off (incl. ctlr+shift, ctrl+alt etc) use the top solution. 65-90 is A-Z in ascii; log out the keypresses to see what they are if you need to extend it, or look up an ascii table. Commented May 5, 2022 at 1:08
1

To prevent all sites from overriding shortcuts (outside of site-specific permission overrides), set permissions.default.shortcuts to 2 in about:config or prefs.js.

You can still allow sites you trust to take over shortcuts on a case by case basis, see this other answer for how to override.

The preference defaults to 0 (the unspecified value for this and other permissions, the actual default is set somewhere in code and allows shortcut stealing), 1 (allow shortcut stealing), or 2 (disallow). 3 (prompt) isn't a valid value for this site permission.

0

It is likely that third party plugins are taking the focus from the main browser window. In that case the keyboard input (except interrupts) will get intercepted by the plugin. If you don't like this you can always remove the offending plugin(s) [I would assume it is likely flash].

5
  • 3
    Flash does it too, but plain Javascript can steal shortcuts. Start a new question here, and press Cmd-L (Ctrl-L on non-Macs) while in the question text window. Instead of going to url bar as it should, shortcut will be stolen and you'll see some insert hyperlink dialog. It used to be rare, but too many websites started doing it recently.
    – taw
    Commented Jul 27, 2010 at 1:22
  • I just tried it on Windows 7 running Firefox and I jumped directly to the address bar like it normally does.
    – Daisetsu
    Commented Jul 28, 2010 at 5:39
  • Sorry, I wasn't clear - this only happens when rich textbox dialog is selected. Did you do that? I tested in on OSX with Firefox, Opera, Safari, and Chrome. In all four normally Cmd-L jumps to url bar (and Ctrl-L does nothing). When editing question body both Cmd-L and Ctrl-L show insert hyperlink dialog instead.
    – taw
    Commented Jul 28, 2010 at 22:07
  • Looks like you are right. I have no idea how to prevent this. :( It's an interesting question now so I'm upvoting your question. If nobody answers it I'll throw a bounty on it.
    – Daisetsu
    Commented Jul 29, 2010 at 0:01
  • The problem is not with plugins. Plugins are a separate problem by themselves. And a different problem, after all. In plugins, all the focus is stolen by the plugin, because it's a separate entity. With JavaScript, the firefox UI is JavaScript and has its own bindings, but sites can define bindings that are at the same level. So far, there is still no mechanism to prevent this.
    – njsg
    Commented Jan 18, 2013 at 17:19
0

Perhaps you can use Autohotkey or Autoit, one of those programs and if you can do hotkey combos and link them to the firefox functions, say

Ctrl-; T to new tab

Ctrl-; N to new window, and so on.

I don't know how to use Autohotkey or Autoit, so someone else will have to verify that this could work, I only offer this as a potential idea.

-2

Firefox current version enables us to "disable javascript to hijack context menu":

Tools/Options/Content/Enable Javascript Advanced/Disable or replace context menus

But there is no feature to "disable javascript to hijack keyboard shortcuts".

ps. I hate twitter website, its keyboard shortcuts conflict with my system-based keyboard shortcuts: J, K, L, I

I've made feature request on bugzilla.mozilla.org, please comment there: https://bugzilla.mozilla.org/show_bug.cgi?id=775002

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .