Skip to content

Instantly share code, notes, and snippets.

@JokingChicken
Created July 12, 2018 21:46
Show Gist options
  • Save JokingChicken/a5a3698328ad08a86f21f87eb330fbe2 to your computer and use it in GitHub Desktop.
Save JokingChicken/a5a3698328ad08a86f21f87eb330fbe2 to your computer and use it in GitHub Desktop.
press a key in html
function openf12() {
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
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
123, // 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment