Skip to content

Instantly share code, notes, and snippets.

@KonradLinkowski
Created September 25, 2018 07:51
Show Gist options
  • Save KonradLinkowski/658bab2060f455ca847593fd75549ca0 to your computer and use it in GitHub Desktop.
Save KonradLinkowski/658bab2060f455ca847593fd75549ca0 to your computer and use it in GitHub Desktop.
Gist for creating keyboard event.
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