Files

97 lines
3.5 KiB
Markdown
Raw Permalink Normal View History

2016-06-24 13:40:46 -04:00
This guide will describe how inputs from the user (keyboard, mouse, etc.) are routed through InputWritr and how the DeviceLayr Gamepad can be used on top of it.
# InputWritr
## Events and Triggers
InputWritr is a module that automates interactions with user inputs and events.
Inputs are stored as both character codes (e.g. 37) and as a string representation of themselves (e.g. "left").
Triggers are what tie inputs with events and are specific actions on the inputs (e.g. press).
Events can be added using `addEvent` which takes in a trigger name, key code, and the function to run when the event is triggered.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
inputWriter.addEvent("onKeyDown", 37, () => console.log("left button pressed"););
2016-06-24 13:40:46 -04:00
```
2017-01-27 01:19:31 -08:00
Events can be removed with `removeEvent`, which takes in the trigger and key code.
2016-06-24 13:40:46 -04:00
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
inputWriter.removeEvent("onKeyDown", 37);
2016-06-24 13:40:46 -04:00
```
Events can be triggered by any number of inputs.
Aliases are additional inputs that allow for an event to be triggered from multiple user sources.
They can be added and removed using `addAliasValues` and `removeAliasValues` which both take in the input and an array of key character codes as arguments.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
inputWriter.addAliasValues("left", [65, 74, 49]); // keys a, j, and 1 respectively
inputWriter.removeAliasValues("left", [65, 74, 49]);
2016-06-24 13:40:46 -04:00
```
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
const inputWriter = new InputWritr({
2016-06-24 13:40:46 -04:00
"aliases": {
"left": [65, 37], // a, left button
},
"triggers": {
"onKeyDown": {
"left": () => console.log("left button was pressed");
}
}
});
```
## Running Events
To run events, use `callEvent` and `makePipe`.
`makePipe` takes in a trigger and code label to get the alias from the event and returns a function to run the triggered event.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
const leftKeyPipe: () => void = inputWriter.makePipe("onKeyDown", "left");
2016-06-24 13:40:46 -04:00
leftKeyPipe();
```
`callEvent` takes in the event function/trigger and a key code, then directly runs the event.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
inputWriter.callEvent("onKeyDown", "left");
2016-06-24 13:40:46 -04:00
```
More can be read about InputWritr on its [Readme](https://github.com/FullScreenShenanigans/InputWritr/blob/master/README.md).
# DeviceLayr
DeviceLayr is a module for GamePad API bindings which allow for the use of devices like controllers.
DeviceLayr has its own input and trigger mappings in addition to the mappings for InputWritr.
Connected devices are detected and registered with `checkNavigatorGamepads` which automatically adds them to the list of added gamepads once called and returns the number
of gamepads added.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
const numAdded = deviceLayer.checkNavigatorGamepapds();
console.log(${numAdded}` gamepads were added.`);
2016-06-24 13:40:46 -04:00
```
The inputs for gamepads are joysticks, buttons, and controller triggers.
Joysticks have an x and y axis, which have a negative, netural, and positive status.
These statuses signal which direction the joystick is being tilted.
Aliases for gamepad triggers are binary signals for whether an active change was made to the status of the gamepad (e.g. pressing a button versus releasing).
`activateAllGamepadTriggers` checks the status of all registered gamepads and calls the equivalent InputWritr event if any triggers have occurred.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
deviceLayer.activateAllGamepadTriggers();
2016-06-24 13:40:46 -04:00
```
To clear the status of all joysticks and buttons, use `clearAllGamepadTriggers`.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:36:43 -05:00
deviceLayer.clearAllGamepadTriggers();
2016-06-24 13:40:46 -04:00
```
2016-11-26 01:36:43 -05:00
More can be read about DeviceLayr on its [Readme](https://github.com/FullScreenShenanigans/DeviceLayr/blob/master/README.md).