Created
November 16, 2022 23:31
-
-
Save remarkablemark/d11276c60a108cd444ddc0c954436207 to your computer and use it in GitHub Desktop.
JavaScript dispatch keydown event programmatically
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
const KEY_CODE = { | |
ARROW_DOWN: 40, | |
BACKSPACE: 8, | |
ENTER: 13, | |
}; | |
/** | |
* Dispatch keydown event. | |
* | |
* @param {number} keyCode | |
* @returns {void} | |
*/ | |
function keydown(keyCode) { | |
const keyboardEvent = new KeyboardEvent('keydown', { | |
bubbles: true, | |
cancelable: true, | |
keyCode, | |
}); | |
document.body.dispatchEvent(keyboardEvent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment