52

In Firefox, how can I prevent pages from overriding Firefox built-in keyboard shortcuts through Javascript on a per-key basis? Preferably on a per-site basis, too? The most frustrating override is the forward slash ('/') that's linked to "Find in page". Sites like Google search results, Twitter timelines, some wikis, and other pages steal the slash key for their own search boxes, which is completely wrong.

Since my rep lets me ask, edit, and answer questions, but not add comments, this is basically a duplicate of these other two questions that weren't properly answered:

How do a stop a website for overriding my keyboard short cuts

Firefox: don't allow websites to override the / (slash) key

Secure Shel
  • 621
  • 5
  • 4
  • 1
    I get frustrated with PHPMyAdmin overriding ctrl-... it's a good question – Highly Irregular May 02 '12 at 21:42
  • 1
    I can't stand websites that override default behaviour. A lot do it with middle-clicks as well. See [this related question](http://superuser.com/questions/468960/restore-my-browsers-middle-click-behaviour/489224) for a solution to that problem. – Cam Jackson Oct 18 '12 at 01:23
  • 1
    Does this answer your question? [How to forbid keyboard shortcut stealing by websites in Firefox](https://superuser.com/questions/168087/how-to-forbid-keyboard-shortcut-stealing-by-websites-in-firefox) – Eyal Roth Feb 16 '21 at 23:46
  • I've answered over here: https://superuser.com/a/1688490/21402 – Tobu Nov 21 '21 at 19:06

4 Answers4

19

Building on edymtt's answer, I've created a userscript which disables only specific keyboard shortcuts. You can add more shortcuts to disable by adding keycodes to the keycodes array, or restrict which sites to apply it to by replacing the @include tag with one or more patterns.

Install using greasemonkey.

// ==UserScript==
// @name           Disable keyboard shortcuts
// @description    Stop websites from hijacking keyboard shortcuts
//
// @run-at         document-start
// @include        *
// @grant          none
// ==/UserScript==

keycodes = [191] // Keycode for '/', add more keycodes to disable other key captures

document.addEventListener('keydown', function(e) {
//    alert(e.keyCode); //uncomment to find out the keycode for any given key
    if (keycodes.indexOf(e.keyCode) != -1)
    {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
    }
    return false;
});
msrd0
  • 187
  • 8
MikeFHay
  • 2,634
  • 4
  • 19
  • 23
  • 2
    Awesome! Almost complete solution for meta keys, too--- I was trying to keep Google Docs from intercepting Previous-tab (Cmd+Left) and Next-tab (Cmd+Right) and found that I also needed to set useCapture to true; i.e., document.addEventListener('keydown', yourHandlerFn, true) – chbrown Oct 06 '14 at 02:28
  • 1
    note that the value `191` depends on your keyboard layout, as far as my azerty setup is concerned (I have 16 + 58 for the alpha area and 111 for the numpad) – sylvainulg May 09 '18 at 08:08
  • 3
    and unfortunately doesn't to work anymore nowadays (tested on phabricator and twitter). custom listener is triggered but site's event is triggered anyway and 'search in page' doesn't show up. – sylvainulg May 09 '18 at 08:14
  • 1
    @sylvainulg I see that it doesn’t work on Twitter, but at least it works on GitHub still. I wonder what changed. – binki Jun 11 '18 at 23:42
  • This works everywhere I tried it except php.net. I wonder what event they are using. – qwazix Nov 09 '18 at 10:51
  • You can find the keycodes here https://keycode.info/ – itirazimvar May 01 '21 at 04:50
  • Perfect, I used this to stop github hijacking ctrl+f etc. (Keycode 17) – ashleysmithgpu Jun 07 '23 at 14:12
17

Since Firefox 58 it is possible to disable keyboard shortcuts override per web site.

"Override Keyboard Shortcuts" and many other permissions are available in "Page Info -> Permissions" (under info icon in URL bar).

Firefox Permissions example for superuser.com

Keyboard override was introduced in Firefox #380637

Alec Istomin
  • 629
  • 6
  • 10
  • 2
    Yes, after 11 years they finally did a half-assed job of fixing that ticket. Unfortunately the current implementation (still in FF 59) completely disables the `Backspace` and `Delete` keys, which really sucks. – Lambart Apr 26 '18 at 18:05
  • 8
    Also, unfortunately it doesn't prevent web pages from capturing non-shifted (i.e., no `Ctrl`, `Shift` or `Alt` key) input--which means this doesn't help at all with the slash key mentioned in the original question. – Lambart Apr 26 '18 at 18:08
  • @Lambart - good to know, I ended up not using this too, since firefox keyboard shortcuts became fixed in jira and confluence (somewhere around firefox 59) – Alec Istomin Apr 27 '18 at 06:06
  • 3
    what was fixed? JIRA and Confluence both still grab all sorts of keys that constantly disrupt my workflow. I see this as Atlassian's problem: They need to allow people to disable (and/or change) their "shortcuts". – Lambart Apr 27 '18 at 16:36
  • firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1433592 fixed atlassian products for me (there might have been atlassian fixes in same timeframe) – Alec Istomin Apr 27 '18 at 20:50
  • Ah, thanks. Interesting, but I hadn't experienced that issue. – Lambart Apr 27 '18 at 22:07
  • Did this support vanish in the last few years? I can't see this setting. :( – Mattias Bengtsson Oct 22 '22 at 23:16
  • Ok. The shortcut in the top bar was gone. To reach this panel you now need to press Ctrl-i. Unfortunately this doesn't get back Ctrl+e (end-of-line) on github.com. I still get their annoying "wrap in backticks" shortcut. – Mattias Bengtsson Oct 22 '22 at 23:34
8

With regard to Google and the Quick Find shortcut, you can install this Greasemonkey script:

http://userscripts-mirror.org/scripts/show/132237

As the description says, it "stops google from focusing the search input on every key press" -- in particular, if you press / with the keyboard focus outside the search box the Quick Find will appear, as it will on other web sites.

I have only installed it without touching the code, but I think it could be easily adapted to work with other sites and/or other shortcuts.

Christian Herenz
  • 434
  • 4
  • 16
edymtt
  • 551
  • 1
  • 4
  • 13
  • To clarify: this userscript stops ALL keyboard shortcuts on pages which it is enabled for, preserving the browser's own functionality. To apply it to other websites just add more @include tags. – MikeFHay May 15 '13 at 12:57
  • without any information about what is hiding behind the link, the answer is unfortunately useless when userscripts-mirror.org gets down, blocked by a firewall or whatever. – sylvainulg May 09 '18 at 08:19
0

Here is a more general script - you can define any number of keypress events to disable.

https://greasyfork.org/en/scripts/5819-disable-website-keyboard-hooks

// ==UserScript==
// @name           Disable website keyboard hooks
// @description    Stop websites from hijacking keyboard shortcuts.
// @author         Isaac Levy
// @run-at         document-start
// @include        *
// @grant          none
// @version        0.0.1
// @namespace      https://isaacrlevy.com
// ==/UserScript==

var keycodes = [ // Add keycodes as desired, keep sorted.
    37, 38, 39, 40 // Arrow keys.
]

var meta_keycodes = [ // Disable these when meta key is pressed.
    70
];

// Don't change below this line.

var isMac = navigator.platform.toLowerCase().indexOf('mac') >= 0;

// Create a fast lookup.
// This saves work during normal typing. Maybe unnecessary.
var keycode_offset = keycodes[0];
var keycode_arr = Array(keycodes[keycodes.length - 1] - keycode_offset)
for (var i = 0, len = keycodes.length; i < len; i++) {
    keycode_arr[keycodes[i] - keycode_offset] = true;
}

document.addEventListener('keydown', function(e) {
    //console.log(e);
    if ((isMac && e.metaKey) || (!isMac && e.ctrlKey)) {
        if (meta_keycodes.indexOf(e.keyCode) >= 0) {
            e.stopImmediatePropagation();
        }
    } else if (keycode_arr[e.keyCode - keycode_offset]) {
        e.stopImmediatePropagation();
    }
    return false;
});
phuclv
  • 26,555
  • 15
  • 113
  • 235
  • I'm sorry - but I cant get this to work. Especially I want to disable the arrow keys left and right on a site - but with this script the arrow keys still control the site??? – Christian Herenz Dec 04 '15 at 18:59