Hexr Control
Keyboard/Gamepad Aggregator
(Please note that mouse needs to be handled separately)
This module uses the Gamepad API and allows binding keys to specific axes and buttons on the gamepad, then exposes an Update() method which will return the current state of the gamepad
## Defaults
These are the by default bindings...
- Left Joystick: WASD
- Right Joystick : CURSOR KEYS
- A : SPACE
- B : ESCAPE
- X : ENTER
- Y : CTRL
- L1 : Z
- R1 : X
Any use of the keyboard overrides a gamepad. The D-Pad is bound to the Left Stick, so it behaves like 'WASD', overriding the actual left stick (but not WASD if the user is actually pressing keys)
Gamepad only works when the user actually has a gamepad plugged in and presses a button.
Gamepad API is flakey as fuck. In Chrome on Mac OSX, you'll find that if you enable a gamepad in a tab and reload the page, it will never detect that gamepad again. You have to open your game in a new tab to get it to pick up the gamepad.
API
var hexrControl = require('hexr-control');
// set truthy flag for whether or not to listen for gamepads..
var controller = hexrControl(enableGamepad);
// override the defaults..
controller.bind(buttonID, keyId);
// get the current state, make sure to pass this to whatever needs it on your game tick..
var state = controller.update();
Button Ids
When binding keys you need a buttonId and a KeyId.
Valid Button IDs are as follows:
(please note full gamepad support is not yet implemented)
A
B
X
Y
L1
R1
LS
RS
START
BACKKey Ids
KeyIds are just strings. This module uses the keycode module on npm. Whatever is a valid string for it is a valid keyId for this.
controller.bind('START', 's'); // binds the 's' key to the 'START' button.## State object.
When you call update you get a state object back. This has the following structure:
{
left : { x : number, y : number }
right : { x : number, y : number }
A : boolean,
B : boolean,
X : boolean,
Y : boolean,
L1 : boolean,
R1 : boolean,
START : boolean,
BACK : boolean,
LS : boolean,
RS : boolean
}It is incomplete because this is just some sort of basic stub for now. My best guess as to what might be actually useful for a 2d platform game that's primarily meant to have keyboard controls (but would be nice to have gamepad support as well).
As a result, the y axis is inverted for the gamepad so that pushing the stick upwards = +1, downwards -1. You'll have to implement inverting this yourself.
Licence
MIT