simple-web-game/static/js/keyboard.js

39 lines
973 B
JavaScript

function handleKeys(event) {
var keyCode;
if(event == null){
keyCode = window.event.keyCode;
}
else{
keyCode = event.keyCode;
}
// up
if (keyCode == 38 || keyCode == 87 || keyCode == 75) {
console.log("up");
me.position.y += 1;
conn.send(JSON.stringify(me.position));
}
// down
else if(keyCode == 40 || keyCode == 83 || keyCode == 74) {
console.log("down");
me.position.y -= 1;
conn.send(JSON.stringify(me.position));
}
// right
else if(keyCode == 39 || keyCode == 68 || keyCode == 76) {
console.log("right");
me.position.x += 1;
conn.send(JSON.stringify(me.position));
}
// left
else if(keyCode == 37 || keyCode == 65 || keyCode == 72) {
console.log("left");
me.position.x -= 1;
conn.send(JSON.stringify(me.position));
}
else if( keyCode == 27) {
console.log("escape");
}
}