Skip to content

Instantly share code, notes, and snippets.

@ingorichter
Created March 21, 2014 22:06
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 ingorichter/9697477 to your computer and use it in GitHub Desktop.
Save ingorichter/9697477 to your computer and use it in GitHub Desktop.
Workaround to send KeyboardEvents in Webkit
/*
* This function helps to resolve an issue with sending Keyboard Events in Webkit
*/
function sendKeyEvent(window, k) {
var oEvent = window.document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get : function () {
return this.keyCodeVal;
}
});
Object.defineProperty(oEvent, 'which', {
get : function () {
return this.keyCodeVal;
}
});
if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent("keydown", true, true, window.document.defaultView, false, false, false, false, k, k);
} else {
oEvent.initKeyEvent("keydown", true, true, window.document.defaultView, false, false, false, false, k, 0);
}
oEvent.keyCodeVal = k;
if (oEvent.keyCode !== k) {
alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
}
window.document.dispatchEvent(oEvent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment