Intercept Shortcuts
-
@luetage Yeah, that is something that annoys me too.
In the hotkey options UI that I made for an extension (and reused for another), I used a function to format some keys to be displayed according to my preferences.
Probably overkill and unnecessary for this user script.
The Function
function formateHotkeys(set1) { let replaceTable = { Control: "Ctrl", Arrowup: "↑", Arrowright: "→", Arrowdown: "↓", Arrowleft: "←", " ": " Space", // Needed 2 spaces so every space in output string doesn't get changed to "Space" Pageup: "PgUp", Pagedown: "PgDn", Delete: "Del", }; let keyString = [...set1].map((c) => c.slice(0, 1).toUpperCase() + c.slice(1).toLowerCase()).join(" + "); keyString = keyString.replace( /Control|Arrowup|Arrowright|Arrowdown|Arrowleft|\s\s|Pageup|Pagedown|Delete/g, function (match) { return replaceTable[match]; } ); // Bug-Fix: if the hotkey was only a space, then the replace doesn't work properly keyString = keyString === " " ? "Space" : keyString; return keyString; }
So a set like this: "
Set(2) {"Control", "ArrowUp"}
" would be made into a string like this: "Ctrl + ↑" -
@luetage Sorry, I need a bit of help: how would I blacklist a multi-key shortcut such as
Ctrl+,
? Also wondering how to update the version you provided for me to use keys instead of keycodes if that would make it more user readable. -
@valiowk Please share your current code and tell me exactly what you want, then I will take a look at it, it’s not a big deal. Btw, if you open DevTools on a page and uncomment the line with
console.log
you can test out keys yourself. If you make itconsole.log(e.key)
, you get the names instead of the codes, just as nomadic showed. -
@luetage I don't have any current code; I was just trying to take the code you gave me and replace the numbers in the
const keycodes
array (actually I was trying to modify the code so that it used keys instead of keycodes the way you edited the main post). The problem is that I don't even know what to replace a number inconst keycodes
by if I want to blacklist a multi-key shortcut: I tried replacing a number by something like44 && e.ctrlKey
and variants on that but Tampermonkey tells me that's not the way to go (it looked wrong even without Tampermonkey telling me so).EDIT: Also, I tried installing your script in the main post in Tampermonkey (to uncomment the line with
console.log
to test out keys myself) but it gives me an error "Invalid UserScript. Sry!"... -
@valiowk Try again, should work now, nomadic just made me aware of a mistake (arrow + direction instead of direction + arrow), maybe Tampermonkey didn’t like that. Tell me whether it works.
And yeah, if you want me to write a version for you I’ll do it. But I need you to list the sites it should work on, if not all sites and all the shortcuts you don’t want the sites to register.
-
@luetage For now I'd just like to blacklist
Ctrl+,
on mail.google.com so that I can use it as a browser shortcut since I never use that Gmail shortcut. I think I'd be able to figure out how to add additional shortcuts on my own once I know how to input a weird shortcut like this (that uses a comma—I don't know if comma can be entered as,
or requires special code). (I know how to use@match
and all that.)EDIT: Tampermonkey is still giving me an error after I used your updated code...
-
@valiowk Try to install it without Tampermonkey.
// ==UserScript== // @name Intercept Shortcuts // @homepageURL https://github.com/luetage/intercept-shortcuts // @description Stops websites from hijacking keyboard shortcuts. // @version 0.9 // @match https://mail.google.com/* // @run-at document-start { 'use strict'; const keycodes = ['CapsLock']; //blacklist document.addEventListener('keydown', e => { console.log(e.key); if (e.key === ',' && e.ctrlKey || keycodes.indexOf(e.key) !== -1) { console.log('trigger'); e.cancelBubble = true; e.stopImmediatePropagation(); } return false; }) }
The blacklist isn’t needed for now, but I kept it and put in capslock. Adjust to your needs. I also left the console logs running and added one so you can check whether a shortcut is successfully being intercepted.
-
@luetage It works (without Tampermonkey), thanks!
-
Why not use
.code
? -
@potmeklecbohdan Because I have no idea what that means.
-
@luetage https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#:~:text=KeyboardEvent.code
For space it gives
"Space"
-
@potmeklecbohdan Makes sense, I’ll probably switch to it myself. As for the public version not so sure, could confuse people who try to adapt the script.
-
@luetage
It was very helpful! -
Hmm, seems to work for some websites and not for others
For example, isn't working for me on Google Docs (docs disabled Ctrl+S as a way of showing that it saves automatically
)
On YouTube, it disabled most single-key shortcuts, but for some reason 'F' wasn't. (I don't actually need 'F', just an example)
-
@legobuilder26 Good find, I noticed
F
on youtube myself. Hard to tell why it doesn’t get blocked, because the script triggers on keydown. It’s very reliable on most other pages though, haven’t found further examples. Don’t use google docs myself, doesctrl-s
trigger there? Anyway that’s all google related it seems… -
@luetage Ctrl+S does nothing when on a Google Doc, because Google wants to brag about their autosave I guess. The userscript does not change this behavior.
-
@legobuilder26 Google should never receive the shortcut and therefore
ctrl-s
should work as browser shortcut. Albeit it may be that Google’s script to irrevocably block the shortcut triggers sooner than this userscript. In my opinion it would best, if Vivaldi had some inbuilt functionality to kill all website shortcuts. The browser and therefore the enduser should have the decision, not some stupid website.