gamepad-event-dispatcher
v0.0.2
Published
A lightweight JavaScript module that provides a simple event-based interface to the browser gamepad API.
Downloads
2
Maintainers
Readme
gamepad-event-dispatcher
gamepad-event-dispatcher is a lightweight event-based wrapper for the browser gamepad API.
Installation
Install the module using npm:
npm install gamepad-event-dispatcher
Usage
Import the module:
import { gamepadEvents } from '/node_modules/gamepad-event-dispatcher/src/index.js';
Attach listeners as required:
gamepadEvents.addEventListener("a", e => console.log("You pressed the A button"));
gamepadEvents.addEventListener("left", e => console.log("You pressed the Left D-PAD button"));
Get 'velocity' values from triggers and joystick directions with the 'detail.value' property of the event:
gamepadEvents.addEventListener("righttrigger", e => console.log(e.detail.value));
Full list of default input names:
"a", "b", "x", "y",
"leftbumper", "rightbumper", "lefttrigger", "righttrigger",
"view", "menu",
"leftjoypressed", "rightjoypressed",
"up", "down", "left", "right",
"leftjoyleft", "leftjoydown", "leftjoyright", "leftjoyup",
"rightjoyleft", "rightjoydown", "rightjoyright", "rightjoyup"
Advanced Usage
Update input names by importing the config module and using 'setGamepadInput':
import { gamepadEventsConfig } from '/node_modules/gamepad-event-dispatcher/src/index.js';
gamepadEventsConfig.setGamepadInput(3, "triangle");
This will would allow the following code to be used:
gamepadEvents.addEventListener("triangle", e => console.log("You pressed the triangle button"));