Created
September 25, 2018 07:51
-
-
Save KonradLinkowski/658bab2060f455ca847593fd75549ca0 to your computer and use it in GitHub Desktop.
Gist for creating keyboard event.
This file contains 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 createKeyboardEvent(name, key, altKey, ctrlKey, shiftKey, metaKey, bubbles) { | |
var e = new Event(name) | |
e.key = key | |
e.keyCode = e.key.charCodeAt(0) | |
e.which = e.keyCode | |
e.altKey = altKey | |
e.ctrlKey = ctrlKey | |
e.shiftKey = shiftKey | |
e.metaKey = metaKey | |
e.bubbles = bubbles | |
return e | |
} | |
var name = 'keydown' | |
var key = 'a' | |
var event = createKeyboardEvent(name, key, false, false, false, false, true) | |
document.addEventListener(name, () => {}) | |
document.dispatchEvent(event) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment