86

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?

kluka
  • 2,164
  • 6
  • 23
  • 37
taw
  • 1,847
  • 4
  • 24
  • 28
  • 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 Jul 30 '10 at 19:57
  • See also http://superuser.com/questions/399352/how-do-i-prevent-pages-i-visit-from-overriding-selected-firefox-shortcut-keys – Mechanical snail Oct 04 '13 at 05:47
  • 2
    This is being discussed at [this feature request on Bugzilla](https://bugzilla.mozilla.org/show_bug.cgi?id=380637). – Mechanical snail Oct 04 '13 at 05: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! – Nicolas Miari May 12 '16 at 05: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: https://superuser.com/a/1317514/158390 – Lambart May 01 '19 at 17:41

11 Answers11

46

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, you can find screenshots for each step below.

  1. Press Ctrl-I (if you can) to show the Page Info window; or else right-click on an empty portion on the web page, and select View Page Info from the context menu.
  2. Select the Permissions tab
  3. Adjust the Override Keyboard Shortcuts setting.

Here are some recent (April 2020) screen shots for each step.

  1. If you use the mouse to open the Page Info window:

Firefox context menu

  1. Select the Permissions tab:

Firefox Page Info window

  1. Adjust the permissions appropriately:

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

Lambart
  • 1,262
  • 12
  • 11
  • 2
    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. – Per Johansson Jan 14 '19 at 07:25
  • Thanks @PerJohansson, I've updated the ticket. – Lambart Jan 14 '19 at 18:03
  • 12
    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 Oct 31 '19 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 Dec 06 '19 at 03:21
  • Works in Firefox 94.0 in Ubuntu. – Daniel Dec 07 '21 at 17:25
  • 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. Feb 07 '22 at 10:51
30

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);
phuclv
  • 26,555
  • 15
  • 113
  • 235
Martin J.H.
  • 539
  • 1
  • 7
  • 16
  • 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 Apr 09 '14 at 17:04
  • This script works for me to fix CTRL+TAB by adding `9` to the keycodes array. Thanks! – Mike Mueller Aug 12 '14 at 23:09
  • 2
    @JamesGecko Found out via [this](http://stackoverflow.com/a/5500536/129269) answer you should/could use the e.metaKey for the command key. – riezebosch Apr 01 '15 at 09: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 Oct 26 '15 at 23:28
  • First question: for a script reputed to work on Firefox, why does it check whether the browser is Opera? And second question: the comment says character codes for 's' and 't', but the codes are really for 'S' and 'T'. Do I need to specify the uppercase code for some reason? Thanks. – Douglas Held Dec 15 '15 at 09:23
  • I've now adapted this so that it captures the keystroke (e.g. ⌘-F) for search in Firefox. It "works" only in the sense of preventing the web application from responding to the key. But neither does Firefox get the keystroke, so it doesn't invoke a search on the page. I am confused at the upvotes for this answer. – Douglas Held Dec 15 '15 at 09:59
  • @DouglasHeld: I tested the scripts in Firefox on Windows, but I decided there is no harm in keeping them compatible with Opera. But truthfully, I don't know if current Opera-versions require the eventListener to be attached to document.body or document. You can remove that line, if you want. About the character codes: I used the commented line `alert(e.keyCode);` to find the character codes that Firefox uses when I pressed the key combinations. – Martin J.H. Dec 15 '15 at 10:02
  • @DouglasHeld: I don't know why Firefox does not see the event. Maybe the event-propagation changed in Firefox-Builds for MacOS? The script still works for me on Windows 7 + Firefox 38 ESR – Martin J.H. Dec 15 '15 at 10:10
  • Thank you @MartinJ.H.. I think then this is simply a bug in the browser I am using. – Douglas Held Dec 15 '15 at 17:23
  • Used this to suppress ctrl+# tab navigation stealing in discord's webapp. Used: keycodes = [49, 50, 51, 52, 53, 54, 55, 56, 57, 48]; – Bill Stidham Aug 26 '16 at 14:12
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.

taw
  • 1,847
  • 4
  • 24
  • 28
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).

DMA57361
  • 18,562
  • 7
  • 72
  • 96
  • 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 Aug 05 '10 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.

njd
  • 11,058
  • 3
  • 39
  • 36
  • 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 Jul 31 '10 at 02:09
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);
  • 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? – still_dreaming_1 Apr 22 '22 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. – Steve Horvath May 05 '22 at 01:08
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.

Peter
  • 121
  • 2
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].

Daisetsu
  • 5,881
  • 4
  • 33
  • 44
  • 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 Jul 27 '10 at 01:22
  • I just tried it on Windows 7 running Firefox and I jumped directly to the address bar like it normally does. – Daisetsu Jul 28 '10 at 05: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 Jul 28 '10 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 Jul 29 '10 at 00: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 Jan 18 '13 at 17:19
0

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.

Tobu
  • 2,663
  • 19
  • 22
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.

Nathaniel Saxe
  • 538
  • 2
  • 6
  • 14
-1

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

diyism
  • 209
  • 3
  • 12