Skip to content

Instantly share code, notes, and snippets.

@Havoc24k
Last active August 29, 2015 13:57
Show Gist options
  • Save Havoc24k/9742873 to your computer and use it in GitHub Desktop.
Save Havoc24k/9742873 to your computer and use it in GitHub Desktop.
Set sequence and interval, paste in web console and run.
var intervalID;
var left = 37;
var up = 38;
var right = 39;
var down = 40;
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
var i = 0;
// set interval of execution and sequence of keystrokes
var interval = 500;
var sequence = [up, left, down, right];
// clear the previous run
window.clearInterval(intervalID);
// set interval of execution
intervalID = window.setInterval(function () {
var keyCodeArg = sequence[i];
keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
keyCodeArg, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);
if (i < sequence.length - 1) {
i = i + 1;
} else {
i = 0;
}
}, interval);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment