use zsnes default keybindings for keyboard control
This commit is contained in:
parent
aed6bdd877
commit
3e9a45a7c3
70
main.js
70
main.js
@ -23,10 +23,7 @@ class Input {
|
|||||||
this.select = false;
|
this.select = false;
|
||||||
this.start = false;
|
this.start = false;
|
||||||
|
|
||||||
this.upArrowPressed = false;
|
this.keysPressed = {};
|
||||||
this.downArrowPressed = false;
|
|
||||||
this.leftArrowPressed = false;
|
|
||||||
this.rightArrowPressed = false;
|
|
||||||
|
|
||||||
window.addEventListener('gamepadconnected', this.gamepadConnected);
|
window.addEventListener('gamepadconnected', this.gamepadConnected);
|
||||||
window.addEventListener('gamepaddisconnected', this.gamepadDisconnected);
|
window.addEventListener('gamepaddisconnected', this.gamepadDisconnected);
|
||||||
@ -35,61 +32,48 @@ class Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
keyDown(e) {
|
keyDown(e) {
|
||||||
if (e.key == 'ArrowUp' || e.key == 'w') {
|
this.keysPressed[e.key] = true;
|
||||||
this.upArrowPressed = true;
|
|
||||||
}
|
|
||||||
if (e.key == 'ArrowDown' || e.key == 's') {
|
|
||||||
this.downArrowPressed = true;
|
|
||||||
}
|
|
||||||
if (e.key == 'ArrowLeft' || e.key == 'a') {
|
|
||||||
this.leftArrowPressed = true;
|
|
||||||
}
|
|
||||||
if (e.key == 'ArrowRight' || e.key == 'd') {
|
|
||||||
this.rightArrowPressed = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
keyUp(e) {
|
keyUp(e) {
|
||||||
if (e.key == 'ArrowUp' || e.key == 'w') {
|
this.keysPressed[e.key] = false;
|
||||||
this.upArrowPressed = false;
|
|
||||||
}
|
|
||||||
if (e.key == 'ArrowDown' || e.key == 's') {
|
|
||||||
this.downArrowPressed = false;
|
|
||||||
}
|
|
||||||
if (e.key == 'ArrowLeft' || e.key == 'a') {
|
|
||||||
this.leftArrowPressed = false;
|
|
||||||
}
|
|
||||||
if (e.key == 'ArrowRight' || e.key == 'd') {
|
|
||||||
this.rightArrowPressed = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
// TODO: have a config screen instead of hard-coding the 8Bitdo SNES30 pad.
|
// Default ZSNES keybindings. See:
|
||||||
// TODO: handle connects / disconnects more correctly.
|
// http://zsnes-docs.sourceforge.net/html/readme.htm#default_keys
|
||||||
|
this.up = this.keysPressed['ArrowUp'];
|
||||||
this.up = this.upArrowPressed;
|
this.down = this.keysPressed['ArrowDown'];
|
||||||
this.down = this.downArrowPressed;
|
this.left = this.keysPressed['ArrowLeft'];
|
||||||
this.left = this.leftArrowPressed;
|
this.right = this.keysPressed['ArrowRight'];
|
||||||
this.right = this.rightArrowPressed;
|
this.start = this.keysPressed['Enter'];
|
||||||
|
this.select = this.keysPressed['Shift'];
|
||||||
|
this.a = this.keysPressed['x'];
|
||||||
|
this.b = this.keysPressed['z'];
|
||||||
|
this.x = this.keysPressed['s'];
|
||||||
|
this.y = this.keysPressed['a'];
|
||||||
|
this.l = this.keysPressed['d'];
|
||||||
|
this.r = this.keysPressed['c'];
|
||||||
|
|
||||||
const gamepad = navigator.getGamepads()[0];
|
const gamepad = navigator.getGamepads()[0];
|
||||||
if (gamepad == null || !gamepad.connected || gamepad.axes.length < 2 ||
|
if (gamepad == null || !gamepad.connected || gamepad.axes.length < 2 ||
|
||||||
gamepad.buttons.length < 12) {
|
gamepad.buttons.length < 12) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// TODO: have a config screen instead of hard-coding the 8Bitdo SNES30 pad.
|
||||||
|
// TODO: handle connects / disconnects more correctly.
|
||||||
this.up |= gamepad.axes[1] < 0;
|
this.up |= gamepad.axes[1] < 0;
|
||||||
this.down |= gamepad.axes[1] > 0;
|
this.down |= gamepad.axes[1] > 0;
|
||||||
this.left |= gamepad.axes[0] < 0;
|
this.left |= gamepad.axes[0] < 0;
|
||||||
this.right |= gamepad.axes[0] > 0;
|
this.right |= gamepad.axes[0] > 0;
|
||||||
this.a = gamepad.buttons[0].pressed;
|
this.a |= gamepad.buttons[0].pressed;
|
||||||
this.b = gamepad.buttons[1].pressed;
|
this.b |= gamepad.buttons[1].pressed;
|
||||||
this.x = gamepad.buttons[3].pressed;
|
this.x |= gamepad.buttons[3].pressed;
|
||||||
this.y = gamepad.buttons[4].pressed;
|
this.y |= gamepad.buttons[4].pressed;
|
||||||
this.l = gamepad.buttons[6].pressed;
|
this.l |= gamepad.buttons[6].pressed;
|
||||||
this.r = gamepad.buttons[7].pressed;
|
this.r |= gamepad.buttons[7].pressed;
|
||||||
this.select = gamepad.buttons[10].pressed;
|
this.select |= gamepad.buttons[10].pressed;
|
||||||
this.start = gamepad.buttons[11].pressed;
|
this.start |= gamepad.buttons[11].pressed;
|
||||||
debug(this.toString());
|
debug(this.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user