How to prevent websites from hijacking keyboard shortcuts
Keyboard shortcuts are a great way of speeding up certain operations in the web browser of choice. Ctrl-t opens a new tab, Ctr-s saves the displayed content to the local system, and Ctrl-l moves the cursor to the browser's address bar.
Sometimes however these shortcuts won't work and the core reason for this is that the website you are on hijacked it. Without going into too many technicalities: websites can override shortcuts by using JavaScript.
A basic example is Gmail where the shortcut /, which should open the on-page search in Firefox, moves the cursor to the site's search field instead.
A long standing bug on Bugzilla addresses the issue in regards to Firefox, but there is no solution in sight. Considering that all browsers share the issue, it is fair to say that this is something that browser developers need to address rather sooner than later.
There are methods to block websites from overriding shortcuts.
Disabling JavaScript
This is without doubt the easiest option of them all, but also the one that may impact how sites are displayed in the browser. Since most websites hijack shortcuts by using JavaScript, disabling JavaScript prevents them from doing so.
Depending on which browser you use you may either disable JavaScript in the browser's options, or use an extension to do so.
I like NoScript for Firefox for this for example as it blocks scripts on most sites automatically after installation. If you find NoScript to invasive, you can try YesScript which allows all but the ones you block.
Chrome users can use ScriptBlock for example to do the same thing.
Disabling JavaScript can render some site functionality or sometimes even the whole site unusable.
Turning it off on the site
Some websites offer controls to turn the keyboard shortcut handling on or off. On Gmail for instance, it is possible to turn off keyboard shortcuts so that nothing is hijacked anymore by the site.
It needs to be noted that this is not a universal solution though as most sites don't offer preferences this this. To do so on Gmail, simply open this link which leads to the general settings. Scroll down until you find keyboard shortcuts listed there and switch the preference from on to off.
Try an alternative
Sometimes web browsers offer multiple shortcuts for the same functionality. Firefox users can open the on-page search with Ctrl-f, F3, or / for example and if one of the shortcuts is blocked, you can try to use a different one if it exists to still use it.
Most of the time though, multiple shortcuts may not be provided.
This method is problematic for another reason: you may not want to use a different shortcut just for a specific website. If you always use Ctrl-f to open the search in the browser, you may not want to use F3 instead.
Scripts and add-ons
Scripts and add-ons may have been created to resolve some of the issues. The Firefox add-on free-control for example blocks websites from overtaking Ctrl-PageUP and Ctrl-PageDOWN shortcuts, while a userscript posted on the Superuser website can handle any Ctrl-powered shortcut.
For the later to work for other shortcuts besides Ctrl-s and Ctrl-t, you need to manipulate the script by adding keycodes that you find on this page to it.
Now You: Is there another method to block websites from stealing keyboard shortcuts? Please share it in the comment section below.
In Firefox:
Goto about:config
Find “permissions.default.shortcuts”
Set value to 2
No more hijacking
Indeed!
“permissions.default.shortcuts” : 0 (default), 1=allow, 2=block
The user can add site exceptions from Firefox’s ‘Page Info’ (ctrl+i) / Permissions / Override Keyboard Shortcuts
Good thing you pointed this out, I had the setting and had left it to // default
The site exception makes blocking obvious.
I’m going to remap Find… Chrome on Mac from CONTROL-F to something else because other websites keep stealing it CONTROL-F and it just seems easier for me to remember a different keyboard shortcut.
Thank you for YesScript. I was looking for some plugin that could disables scripts only on specified websites.
Can shortcut keys programmed on Phrase Express be stolen too? Thank you.
What bothers me the most, though this appears on very few sites, are context menu and text selection disabled. Bookmarklets are available to restore both. I’ve made ad add-on which delivers a toolbar button with one javascript for both annoyances : click and both annoyances are off. Maybe do similar bookmarklets exist for keyboard shortcuts hijacking.
Generally speaking, how not to be annoyed by domains who have the nerve to seize a user’s browser tools for themselves? Whatever bothers a user’s move on a website (for legitimate reasons) is moreover counter-productive.
The most annoying hotkey hijack by far is Youtube! I’ve seen several bug posts relating to that for Firefox, spanning many years back. But no solution so far.
this. afaik this is because, as soon as you click the play button or anything else in the player, it’s the flash plugin that’s in focus, so all shortcuts get sent to flash instead of firefox. really annoying.
edit: it seems that opting for the html5 player gets rid of that annoyance. at least in my case the html5 player is completely buggy and useless though, so that’s not an option for me atm.
Even with the html5 interface, Ctrl+F will fullscreen the current video instead of Firefox’s usual behaviour (Find).
I’ve only noticed this annoyance in recent weeks, so it might be new.
Yeah, Flash steals focus. And has been doing so forever. One more good reason to wish for the demise of Flash I guess.