MacOS – Prevent webpages from binding to certain keyboard shortcuts

keyboardmacossafari

I'd like to stop webpages from binding to ⌥ (Option) only shortcuts.

E.g. these can be bound to:

  • +V
  • ++K
  • ++E
  • +++X
  • etc.

But these I want to prevent being bound to (so that their default comes into effect, I have set them up as dead keys/special character keys on my keyboard to type symbols I find important):

  • +/
  • ++E
  • etc.

Best Answer

I lightly edited some code from this answer on Super User to make it prevent the capture of certain shortcuts which do use the Option key and do not use the Command key. You'll want to add the keycodes for any shortcuts you want to prevent binding to the keycodes list. I started it with the codes for the two examples in your question, / and E (it doesn't care if other modifiers are pressed or not).

You can install this to your browser using an extension such as Tampermonkey.

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

// These are the keycodes for E and /. Find others to add by uncommenting the first alert line below and pressing that key.
keycodes = [69, 191];

(window.opera ? document.body : document).addEventListener('keydown', function(e) {
    // alert(e.keyCode ); //uncomment to find more keyCodes
    if (keycodes.indexOf(e.keyCode) != -1 && e.altKey && !(e.metaKey)) {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
    // alert("Gotcha!"); //uncomment to check if it's seeing the combo
    }
    return false;
}, !window.opera);