Why can't I add an 'onkeydown' event to a DIV element?

PDS OWNER CALIN (Calin Baenen) on December 02, 2019

I am working on a game and I am trying to add movement but when I try to add an onkeydown event onto my div element, it doesn't work. <div id=... [Read Full]
markdown guide


onkeypress is only triggered for the 'active' field when a key is pressed.
You need to enable your element to receive focus. The trick is to add attr tabindex to #gameEvent.


Why does it need to be tied to a <div>? Why don't you just make an event listener to window?

window.onkeypress = ...
code of conduct - report abuse