I've created a function to programmatically send a KEYDOWN event.
If I open the developer console I can see that the event is successfully dispatch. I catch the event using this function:
document.addEventListener ('keydown', function () {
console.log (JSON.stringify (event.keyCode))
}, false);
But if I click on an input text field and I programatically send the key value (for example) of the letter "W", the letter does not appear in it.
What am I doing wrong?
This is the function to programmatically dispatch the event:
function sendKeyDownEvent (keyCode) {
var e = new Event ("keydown");
e.keyCode = keyCode;
e.which = e.keyCode;
e.altKey = false;
e.ctrlKey = true;
e.shiftKey = false;
e.metaKey = false;
e.bubbles = true;
document.dispatchEvent (e);
}