Last active
March 22, 2020 16:49
-
-
Save ddrscott/a12dd36182677e6ce2c4c1607126f923 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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