Pregunta

So I am making a game and I want everything to be dynamic so I can use it to make other things. I'm trying to set the event listener callback to a custom callback. I'm just testing it with keydown for now, but when I push my keys, nothing outputs in the console:

Here is the registerKeyListener function:

function registerKeyListener(id, type, callback){
    document.getElementById(id).addEventListener(type, callback, false);
}

Here is how I call it:

registerKeyListener("game", "keyDown", move);

Where move is:

function move(){
    console.log("move function called");
}
¿Fue útil?

Solución

Your function works. The event is keydown, not keyDown.

See demo of your code.

Though there can be events with upper case letters, all the usual ones are lower case only. Check here for reference: https://developer.mozilla.org/en-US/docs/Web/Reference/Events

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top