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

keyboard shortcuts

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.

This article was first seen on ComTek's "TekBits" Technology News


Printer Friendly, PDF & Email

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest © 2021 by ComTek Computer Services Inc.