How do I prevent pages I visit from overriding selected Firefox shortcut keys?

40

13

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

Posted 2012-03-11T01:40:41.000

Reputation: 501

1I get frustrated with PHPMyAdmin overriding ctrl-<left arrow>... it's a good question – Highly Irregular – 2012-05-02T21:42:43.133

1

I can't stand websites that override default behaviour. A lot do it with middle-clicks as well. See this related question for a solution to that problem.

– Cam Jackson – 2012-10-18T01:23:23.120

Answers

12

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

MikeFHay

Posted 2012-03-11T01:40:41.000

Reputation: 2 334

2Awesome! 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 – 2014-10-06T02:28:09.557

1note 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 – 2018-05-09T08:08:05.173

2and 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 – 2018-05-09T08:14:36.747

1@sylvainulg I see that it doesn’t work on Twitter, but at least it works on GitHub still. I wonder what changed. – binki – 2018-06-11T23:42:49.173

This works everywhere I tried it except php.net. I wonder what event they are using. – qwazix – 2018-11-09T10:51:35.560

8

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

Posted 2012-03-11T01:40:41.000

Reputation: 499

1Yes, 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 – 2018-04-26T18:05:53.703

6Also, 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 – 2018-04-26T18:08:00.643

@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 – 2018-04-27T06:06:02.000

2what 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 – 2018-04-27T16:36:04.077

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 – 2018-04-27T20:50:21.693

Ah, thanks. Interesting, but I hadn't experienced that issue. – Lambart – 2018-04-27T22:07:18.447

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.

edymtt

Posted 2012-03-11T01:40:41.000

Reputation: 551

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 – 2018-05-09T08:19:56.060

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 – 2013-05-15T12:57:13.103

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

vackamole

Posted 2012-03-11T01:40:41.000

Reputation: 1

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 – 2015-12-04T18:59:07.693

-3

YesScript add-on was the hassle free solution for me. It prevents sites from running JavaScript but only the sites you choose. Most of times these sites hijack keyboard shortcut by JavaScript. I found about it on this article form Ghacks.net.

Phoenix Brothers

Posted 2012-03-11T01:40:41.000

Reputation: 1