I wrote this simple "webpage" to test out how to check if a key is pressed. It should log 1 if you press the left arrow key and 2 if you press the right arrow key:
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
<script>
window.addEventListener("keydown", function (event) {
if(event.keyCode == 37 ){
console.log(1);
}
if(event.keyCode == 39 ){
console.log(2) ;
}
});
$(window).trigger("keydown", { which: 37, keyCode: 37 });
</script>
You can test it. It works fine. However, if I want to trigger this event programmatically through Javascript I fail.
I found this post and tested all methods on it on the window, document and body DOM object and the "script" JQuery objects.
What am I doing wrong?