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");
}
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top