Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GreaseMonkey: Prevent Web Applications From Grabbing Certain HotKeys
// ==UserScript==
// @name anti key-grabber
// @description Prevent web apps from capturing and muting vital keyboard shortcuts
// @grant none
// @version 1.1
// ==/UserScript==
(function(){
var isMac = unsafeWindow.navigator.oscpu.toLowerCase().contains("mac os x");
unsafeWindow.document.addEventListener('keydown', function(e) {
if (e.keyCode === 116) {
// F5 should never be captured
e.stopImmediatePropagation();
return;
}
// Mac uses the Command key, identified as metaKey
// Windows and Linux use the Control key, identified as ctrlKey
var modifier = isMac ? e.metaKey : e.ctrlKey;
// abort if the proper command/control modifier isn't pressed
if (!modifier) {
return;
}
switch (e.keyCode) {
case 87: // W - close window
case 84: // T - open tab
case 76: // L - focus awesome bar
case 74: // J - open downloads panel
e.stopImmediatePropagation();
return;
}
// s'more mac love
if (!isMac) {
return;
}
switch (e.keyCode) {
case 188: // , (comma) - open settings [mac]
case 82: // R - reload tab
e.stopImmediatePropagation();
return;
}
}, true);
})();
@rodneyrehm

This comment has been minimized.

Copy link
Owner Author

rodneyrehm commented Mar 22, 2013

@gsauthof

This comment has been minimized.

Copy link

gsauthof commented Apr 12, 2014

Thank you very much, sir! Finally a greasemonkey access key disable script that works!

Using Firefox 27 I've tried 3 different about:config settings (Accessibility.accesskeycausesactivation, Ui.key.contentAccess andUi.key.generalAccessKey), 3 greasemonkey scripts (for specific sites or supposed general solutions) and 1 JS bookmarklet (http://meta.stackoverflow.com/questions/2980/is-there-a-way-to-disable-the-hotkeys) for stackoverflow. Nothing worked.

Especially, the overriding of Ctrl+I on stackoverflow drives me insane - because this overwrites the Vimperator shortcut for opening an external editor. It's a shame that Firefox does not provide means to configure which shortcuts a webpage is able to set.

With your script it is just adding a case 73: to also exclude Ctrl+I from being grabbed by a webpage. Awesome.

@VengefulAncient

This comment has been minimized.

Copy link

VengefulAncient commented Mar 19, 2015

Thanks a lot for this script, you saved me hours of rage... I can't stand YouTube stealing Ctrl+T all the time. Just had to add keyCode 27 so that Google Search doesn't stop me from loading the page with Esc (it usually steals it and interprets as focus on search field).

@Hund

This comment has been minimized.

Copy link

Hund commented Oct 31, 2015

Is it possible to add support for the Escape key? I cant leave input mode in Vimperator on sites like Facebook without closing every conversation first.

@protrolium

This comment has been minimized.

Copy link

protrolium commented Dec 26, 2015

thanks @rodneyrehm. I modified this script slightly by adding case 190: // . period and case 191 // '/' but it doesn't seem to work; Google still hijacks these key presses. I am using Firefox 43.0.1 with Vimperator/Greasemonkey.

@VVelox

This comment has been minimized.

Copy link

VVelox commented Jul 30, 2018

Hmm.... This not appear to be working in 60.0.2.

@lindsayad

This comment has been minimized.

Copy link

lindsayad commented Dec 4, 2019

I'm experiencing the same issue right here on github where I want to disable github's built in behavior of ctrl-b inserting bold formatting, which prevents me from simply moving point like I'm used to with my emacs keybindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.