I am looking for a way to simulate a keystroke on a keyboard (e.g. hints). I looked around and I basically found these two questions:
The problem is that they both use the KeyboardEvent.initKeyboardEvent() event, which is deprecated according to MDN . Is there any other way to accomplish the same thing without this deprecated function?
I would like to know this because I am creating a script for YouTube using the Chrome TamperMonkey extension. This script will, when [space] is pressed, call K. K - YouTube / Play / Pause button. I have a [space] listener that works fine with the code below:
document.addEventListener("keydown", function(e) { if(e.keyCode==32) { e.preventDefault(); } }, false);
Also I'm really looking for an approach using pure JavaScript.
source share