每次我在画布上移动时,mousemove 事件都会起作用并调用 onMouseMove。虽然,keydown 和 keyup 事件永远不会起作用。我单击画布并按了一些键,但没有触发任何事件。有谁知道为什么这些事件不起作用?感谢您的任何建议!如果有人好奇代码的来源,我正在关注 udacity 的 html5 课程。
InputEngineClass = Class.extend({
keyState: new Array(),
setup: function() {
document.getElementById("gameCanvas").addEventListener('mousemove', gInputEngine.onMouseMove);
document.getElementById("gameCanvas").addEventListener('keydown', gInputEngine.onKeyDown);
document.getElementById("gameCanvas").addEventListener('keyup', gInputEngine.onKeyUp);
},
onMouseMove: function (event) {
console.log("Called onMouseMove");
var posx = event.clientX;
var posy = event.clientY;
},
onKeyDown: function (event) {
console.log("KEY DOWN!!!");
keyState[event.keyID] = true;
gInputEngine.update();
},
onKeyUp: function (event) {
console.log("KEY UP!!!");
keyState[event.keyID] = true;
},
update: function() {
KeyW = 87;
if(gInputEngine.keyState[KeyW]) console.log("FORWARD!!!");
}
});
gInputEngine = new InputEngineClass();