Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Last active April 30, 2022 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/ca52cf287078eb72aee81bf2c0344e0c to your computer and use it in GitHub Desktop.
Save whoisryosuke/ca52cf287078eb72aee81bf2c0344e0c to your computer and use it in GitHub Desktop.
<div id="output" style="background:green;min-height:40px">
</div>
var output = document.getElementById("output");
output.prepend("'KeyboardEvent' constructor status: " + (/\[native\s+code\]/i.test(KeyboardEvent + "") ? "NATIVE" : "POLYFILL"))
document.addEventListener("keydown", function(e) {
output.insertAdjacentHTML("afterbegin",
"<b>char:&nbsp;</b>" + e.char + "&nbsp;|&nbsp;" +
"<b>key:</b>&nbsp;" + e.key + "&nbsp;|&nbsp;" +
"<b>shiftKey:</b>&nbsp;" + e.shiftKey + "&nbsp;|&nbsp;" +
"<b>keyCode:</b>&nbsp;" + e.keyCode + "&nbsp;|&nbsp;" +
"<b>location:</b>&nbsp;" + e.location + "<br/>") ;
e.preventDefault();
});
function simulateKeyEvent(character) {
var keyEvent = new KeyboardEvent("keydown", {key : character, char : character, shiftKey: true});
document.dispatchEvent(keyEvent);
console.log('run event')
}
simulateKeyEvent('a');
simulateKeyEvent('b');
simulateKeyEvent('c');
// You should see c, b, and a in the list in that order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment