I took the basic Pong code on the Internet and tried to add keystrokes, the code is here: http://cssdeck.com/labs/ping-pong-game-tutorial-with-html5-canvas-and-sounds
I added the following:
canvas.addEventListener("keydown", handlekeydown, true);
After this existing code:
canvas.addEventListener("mousemove", trackPosition, true);
canvas.addEventListener("mousedown", btnClick, true);
And I also added:
function handlekeydown(e) {
console.log("debug");
console.log("keycode: "+e.keyCode);
}
But the function is never called, although I try to press various keys. Why is this? I'm sure Canvas is in focus.
source
share