From 3e9a45a7c3d934c5c9a7bd880d81497c0a70d7ba Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 25 Sep 2019 15:33:45 -0400 Subject: [PATCH] use zsnes default keybindings for keyboard control --- main.js | 70 ++++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/main.js b/main.js index c962bbc..9e7bfe8 100644 --- a/main.js +++ b/main.js @@ -23,10 +23,7 @@ class Input { this.select = false; this.start = false; - this.upArrowPressed = false; - this.downArrowPressed = false; - this.leftArrowPressed = false; - this.rightArrowPressed = false; + this.keysPressed = {}; window.addEventListener('gamepadconnected', this.gamepadConnected); window.addEventListener('gamepaddisconnected', this.gamepadDisconnected); @@ -35,61 +32,48 @@ class Input { } keyDown(e) { - if (e.key == 'ArrowUp' || e.key == 'w') { - 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; - } + this.keysPressed[e.key] = true; } keyUp(e) { - if (e.key == 'ArrowUp' || e.key == 'w') { - 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; - } + this.keysPressed[e.key] = false; } update() { - // TODO: have a config screen instead of hard-coding the 8Bitdo SNES30 pad. - // TODO: handle connects / disconnects more correctly. - - this.up = this.upArrowPressed; - this.down = this.downArrowPressed; - this.left = this.leftArrowPressed; - this.right = this.rightArrowPressed; + // Default ZSNES keybindings. See: + // http://zsnes-docs.sourceforge.net/html/readme.htm#default_keys + this.up = this.keysPressed['ArrowUp']; + this.down = this.keysPressed['ArrowDown']; + this.left = this.keysPressed['ArrowLeft']; + this.right = this.keysPressed['ArrowRight']; + 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]; if (gamepad == null || !gamepad.connected || gamepad.axes.length < 2 || gamepad.buttons.length < 12) { 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.down |= gamepad.axes[1] > 0; this.left |= gamepad.axes[0] < 0; this.right |= gamepad.axes[0] > 0; - this.a = gamepad.buttons[0].pressed; - this.b = gamepad.buttons[1].pressed; - this.x = gamepad.buttons[3].pressed; - this.y = gamepad.buttons[4].pressed; - this.l = gamepad.buttons[6].pressed; - this.r = gamepad.buttons[7].pressed; - this.select = gamepad.buttons[10].pressed; - this.start = gamepad.buttons[11].pressed; + this.a |= gamepad.buttons[0].pressed; + this.b |= gamepad.buttons[1].pressed; + this.x |= gamepad.buttons[3].pressed; + this.y |= gamepad.buttons[4].pressed; + this.l |= gamepad.buttons[6].pressed; + this.r |= gamepad.buttons[7].pressed; + this.select |= gamepad.buttons[10].pressed; + this.start |= gamepad.buttons[11].pressed; debug(this.toString()); }