Question

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");
}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top