javascript - Simulating keyboard press in a way that isn't deprecated? -


i looking way simulate keyboard press (like titled suggests). have looked around , have found these 2 questions:

the issue both use keyboardevent.initkeyboardevent() event according mdn deprecated. there different way of accomplishing same thing without deprecated function?

i know because creating script youtube using chrome's tampermonkey extension. script will, when [space] pressed, trigger k. k youtube's toggle play/pause button. have [space] listener working code below:

document.addeventlistener("keydown", function(e) {     if(e.keycode==32) {         e.preventdefault();     } }, false); 

also looking pure javascript approach.

if jquery build event.

https://stackoverflow.com/a/3368599/3257830

if want create event, initialize object dispatch event.

https://developer.mozilla.org/en-us/docs/web/api/event/event

document.addeventlistener("keypress", function(e) {    alert(e.which);  });  var e = new event("keypress");  e.which = 65;  e.keycode = 65;  document.dispatchevent(e);
<p id="r">alerts on event</p>


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

post - imageshack API cURL -

dataset - MPAndroidchart returning no chart Data available -