Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active March 22, 2020 16:49
Show Gist options
  • Save ddrscott/a12dd36182677e6ce2c4c1607126f923 to your computer and use it in GitHub Desktop.
Save ddrscott/a12dd36182677e6ce2c4c1607126f923 to your computer and use it in GitHub Desktop.
function simulateKey (keyCode, type, modifiers, elm) {
var evtName = (typeof(type) === "string") ? "key" + type : "keydown";
var modifier = (typeof(modifiers) === "object") ? modifier : {};
var event = document.createEvent("HTMLEvents");
event.initEvent(evtName, true, false);
event.keyCode = keyCode;
for (var i in modifiers) {
event[i] = modifiers[i];
}
if (elm) {
elm.dispatchEvent(event);
} else {
document.getElementById("game").dispatchEvent(event);
}
}
function doIt() {
simulateKey(32, "down", null, window.game);
simulateKey(32, "up", null, window.game);
setTimeout(doIt, 2);
}
window.game = document.getElementById("game");
doIt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment