104

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,294
  • 6
  • 26
  • 38
taw
  • 2,037

12 Answers12

66

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

gcb
  • 5,442
  • 13
  • 62
  • 86
Lambart
  • 1,609
33

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
  • 30,396
  • 15
  • 136
  • 260
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
  • 2,037
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,793
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,426
3

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,781
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);
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
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
  • 6,171
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.

0

Building on the answer by @Martin J.H. above, the below userscript was created, because Outlook Web was hijacking the Alt+arrow (and later just the Alt) keys on MacOS. It should provide a headstart template for creating your own userscript.

Just uncomment the alert block in the handler function (possibly changing it to console.log, to make it less annoying) to check for the eventCodes that you need and add them to the array at the beginning of the file.

You will of course need a userscript manager like Greasemonkey, Tampermonkey (below is tested with this) or Violentmonkey. Greasyfork has a list of browsers and available plugins.

// ==UserScript==
// @name           Fix Outlook Mac Hotkeys Hijacking
// @description    Disables Outlook from highjacking Mac keyboard shortcuts
// @copyright      2024, phonique (https://github.com/phonique/userscript-stop-outlook-hijack/)
// @homepageURL    https://github.com/phonique/userscript-stop-outlook-hijack
// @version        0.1.0
// @run-at         document-start
// @match          https://outlook.office.com/*
// @grant          none
// @license        MPL-2.0
// ==/UserScript==

// event codes for arrow keys (add your own here). var eventCodes = ["ArrowLeft","ArrowRight","ArrowUp","ArrowDown"];

// Possible keyboard events: keypress, keyup, keydown // currently Outlook seems to only require keydown and keyup. // Only keypress is not enough to disable Alt-key hijacking.

// We enable useCapture here, because we want to capture the event while // it's trickling down the DOM and then ignore other Listeners that hijack // our default Alt functionality with the cancelBubble() and // stopImmediatePropagation() calls. // see here: https://stackoverflow.com/questions/5657292/why-is-false-used-after-this-simple-addeventlistener-function/25591239#25591239

window.addEventListener('keydown', handler, true); // useCapture true, i.e. we are capturing the event while it's trickling down window.addEventListener('keyup', handler, true); // useCapture true // we don't need this for Outlook, but you might for your usecase // window.addEventListener('keypress', handler, true);

function handler (e) { // alert("eventCode: " + e.code + " eventKey: " + e.key); // debug here

 // If you want to completely disable Alt hijacking, not just in textboxes
 // uncomment the next and also comment out (or remove) the line following it.
 // if (e.code === &quot;AltRight&quot; || e.code === &quot;AltLeft&quot; || e.key === &quot;Alt&quot;) {
 if ((e.code === &quot;AltRight&quot; || e.code === &quot;AltLeft&quot; || e.key === &quot;Alt&quot;) &amp;&amp; typeof(document.activeElement.role) !== 'undefined' &amp;&amp; document.activeElement.role == 'textbox') {
     e.cancelBubble = true;
     e.stopImmediatePropagation();
 }
 // including previous UX-fixes
 if (eventCodes.indexOf(e.code) != -1 &amp;&amp; e.shiftKey &amp;&amp; e.altKey) {
     e.cancelBubble = true;
     e.stopImmediatePropagation();
 }
 if (eventCodes.indexOf(e.code) != -1 &amp;&amp; e.altKey) {
     e.cancelBubble = true;
     e.stopImmediatePropagation();
 }
 if (eventCodes.indexOf(e.code) != -1 &amp;&amp; e.shiftKey &amp;&amp; e.metaKey) {
     e.cancelBubble = true;
     e.stopImmediatePropagation();
 }
 if (eventCodes.indexOf(e.code) != -1 &amp;&amp; e.shiftKey &amp;&amp; e.ctrlKey) {
     e.cancelBubble = true;
     e.stopImmediatePropagation();
 }

}

Geotti
  • 3
-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
  • 205