I'm trying to set up a TamperMonkey script to reassign the Right Arrow key to F key.
I tried this code, but so far, nothing happens when I press F.
(function(){
document.addEventListener('keydown', function(e) {
// pressed F
if (e.keyCode == 70 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
keyCode == 39 // this should trigger the right arrow
}
}, false);
})();
Can someone enlighten me on this, please?
You'll want to use the
KeyboardEventconstructor and then rundispatchEventon either the entiredocumentor a specific element viadocument.querySelector,document.getElementById, etc.Run the snippet below to see this in action.
(BTW, using
KeyboardEvent.keyCodeis depricated in favor ofKeyboardEvent.key. It still works in major web browsers for backwards compatibility, but it's been officially deprecated in the standards)(Also make sure your TamperMonkey script is running at
document-endif you're wanting to run the keyboard event on a custom DOM element)However, there isn't a way to disable f from doing other things on that website. If you want to do this, you can install AutoHotKey (Wikipedia Link), create an AutoHotKey script with this one line:
https://www.autohotkey.com/docs/misc/Remap.htm#Remap
When you start this AHK script, AHK will intercept the key event before (most) programs and modify it.