我想用 HTML5<canvas>
元素编写一个蛇游戏演示。我尝试注册 Keyboard(onkeydown) 事件,但它不起作用。
代码:
canvas.onkeydown = divertDirection; //don't work
//canvas.addEventListener("keydown", divertDirection, false); //don't work
//根据键盘来调振蛇移动的方向
function divertDirection(ev) {
console.log(ev);
}
//register this event for widnow
window.addEventListener("keydown", divertDirection, false); //ok