Skip to content

Instantly share code, notes, and snippets.

@ruiwen
Created June 29, 2014 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruiwen/d07f2f94c56572cbb40d to your computer and use it in GitHub Desktop.
Save ruiwen/d07f2f94c56572cbb40d to your computer and use it in GitHub Desktop.
function keypress(n) {
var keyboardEvent = new KeyboardEvent('keydown', {bubbles:true});
Object.defineProperty(keyboardEvent, 'charCode', {get:function(){return this.charCodeVal;}});
Object.defineProperty(keyboardEvent, 'keyCode', {get: function(){return this.charCodeVal;}});
keyboardEvent.charCodeVal = n;
document.body.dispatchEvent(keyboardEvent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment