How to forbid keyboard shortcut stealing by websites in Firefox

60

22

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?

taw

Posted 2010-07-27T00:48:11.317

Reputation: 1 515

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 – 2016-05-12T05:14:06.557

4Agreed, 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 – 2010-07-30T19:57:34.457

1

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 – 2019-05-01T17:41:05.340

1

This is being discussed at this feature request on Bugzilla.

– Mechanical snail – 2013-10-04T05:49:02.387

Answers

24

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);

Martin J.H.

Posted 2010-07-27T00:48:11.317

Reputation: 440

This script works for me to fix CTRL+TAB by adding 9 to the keycodes array. Thanks! – Mike Mueller – 2014-08-12T23:09:57.080

@JamesGecko Found out via this answer you should/could use the e.metaKey for the command key.

– riezebosch – 2015-04-01T09:08:35.027

3This 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 – 2015-10-26T23:28:09.487

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 – 2015-12-15T09:23:23.737

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 – 2015-12-15T09:59:37.500

@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. – 2015-12-15T10:02:55.623

@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. – 2015-12-15T10:10:59.823

Thank you @MartinJ.H.. I think then this is simply a bug in the browser I am using. – Douglas Held – 2015-12-15T17:23:16.407

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 – 2016-08-26T14:12:19.670

1This 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 – 2014-04-09T17:04:03.113

9

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.)

You can disable site shortcuts by following these steps:

  1. Click the (i) icon in the Location Bar
  2. Click the small arrow (>) to the right of "Connection" status item.
  3. Now, you should see More Information at the bottom. Click that, and you'll finally get to the Page Info dialog.
  4. Finally, go to the Permissions tab and adjust the Override Keyboard Shortcuts setting.

Here are some recent (May 2019) screen shots for each step:

Firefox Site Info dialog screen shot

Firefox Site Security dialog screen shot

Firefox Permissions tab screen shot

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

Posted 2010-07-27T00:48:11.317

Reputation: 622

2In 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 – 2019-01-14T07:25:02.570

Thanks @PerJohansson, I've updated the ticket. – Lambart – 2019-01-14T18:03:21.777

1What 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 – 2019-10-31T15:35:23.670

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 – 2019-12-06T03:21:59.717

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

Posted 2010-07-27T00:48:11.317

Reputation: 1 515

1Using FF 30.0 almost 4 years later and this is still an issue. I'll try Martin's Greasemonkey idea. – LGT – 2014-07-28T20:59:42.733

2Using FF 53.0 almost 8 years later and this is still an issue. – Daniel – 2017-06-07T11:40:20.720

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

Posted 2010-07-27T00:48:11.317

Reputation: 17 581

2This 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 – 2010-08-05T16:54:06.603

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

Posted 2010-07-27T00:48:11.317

Reputation: 9 743

2Javascript 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 – 2010-07-31T02:09:12.887

0

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);

Steve Horvath

Posted 2010-07-27T00:48:11.317

Reputation: 101

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

Posted 2010-07-27T00:48:11.317

Reputation: 5 195

3Flash 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 – 2010-07-27T01:22:25.663

I just tried it on Windows 7 running Firefox and I jumped directly to the address bar like it normally does. – Daisetsu – 2010-07-28T05:39:56.587

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 – 2010-07-28T22:07:22.240

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 – 2010-07-29T00:01:45.837

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 – 2013-01-18T17:19:56.853

0

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

Posted 2010-07-27T00:48:11.317

Reputation: 111

-1

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

Posted 2010-07-27T00:48:11.317

Reputation: 518

-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

diyism

Posted 2010-07-27T00:48:11.317

Reputation: 151