gamepad-ts
Gamepad-ts is a heavily typed Typescript gamepad contol library.
It will detect and connect an USB contoller, and send corresponding events.
This code had been test with DS4Windows
usage
- it you want to use your gamepad as a
nintendo
contoler:
import { NintendoController } from 'gamepad-ts'
const pad = new NintendoController();
pad.on('LStick', (pos: [number, number]) => {});
pad.on('RStick', (pos: [number, number]) => {});
pad.on('dpad', (pos: [number, number]) => {});
pad.on('LStickBt', (pressed: boolean) => {});
pad.on('RStickBt', (pressed: boolean) => {});
pad.on('X', (pressed: boolean) => {});
pad.on('A', (pressed: boolean) => {});
pad.on('B', (pressed: boolean) => {});
pad.on('Y', (pressed: boolean) => {});
pad.on('+', (pressed: boolean) => {});
pad.on('-', (pressed: boolean) => {});
pad.on('L', (pressed: boolean) => {});
pad.on('R', (pressed: boolean) => {});
pad.on('ZL', (pressed: boolean) => {});
pad.on('ZR', (pressed: boolean) => {});
- it you want to use your game pad as a
xbox
contoler"
import { XBoxController } from 'gamepad-ts'
const pad = new XBoxController();
pad.on('LStick', (pos: [number, number]) => {});
pad.on('RStick', (pos: [number, number]) => {});
pad.on('dpad', (pos: [number, number]) => {});
pad.on('LStickBt', (pressed: boolean) => {});
pad.on('RStickBt', (pressed: boolean) => {});
pad.on('X', (pressed: boolean) => {});
pad.on('A', (pressed: boolean) => {});
pad.on('B', (pressed: boolean) => {});
pad.on('Y', (pressed: boolean) => {});
pad.on('start', (pressed: boolean) => {});
pad.on('back', (pressed: boolean) => {});
pad.on('LB', (pressed: boolean) => {});
pad.on('RB', (pressed: boolean) => {});
pad.on('LT', (pressed: boolean) => {});
pad.on('RT', (pressed: boolean) => {});
- it you want to use your game pad as a
playstation
contoler"
import { PlayStationController } from 'gamepad-ts'
const pad = new PlayStationController();
pad.on('LStick', (pos: [number, number]) => {});
pad.on('RStick', (pos: [number, number]) => {});
pad.on('dpad', (pos: [number, number]) => {});
pad.on('LStickBt', (pressed: boolean) => {});
pad.on('RStickBt', (pressed: boolean) => {});
pad.on('square', (pressed: boolean) => {});
pad.on('cross', (pressed: boolean) => {});
pad.on('circle', (pressed: boolean) => {});
pad.on('triangle', (pressed: boolean) => {});
pad.on('share', (pressed: boolean) => {});
pad.on('options', (pressed: boolean) => {});
pad.on('L1', (pressed: boolean) => {});
pad.on('R1', (pressed: boolean) => {});
pad.on('L2', (pressed: boolean) => {});
pad.on('R2', (pressed: boolean) => {});
- it you want to use your game pad as a
generic
contoler"
import { GenericController } from 'gamepad-ts'
const pad = new GenericController();
pad.on('LStick', (pos: [number, number]) => {});
pad.on('RStick', (pos: [number, number]) => {});
pad.on('dpad', (pos: [number, number]) => {});
pad.on('LStickBt', (pressed: boolean) => {});
pad.on('RStickBt', (pressed: boolean) => {});
pad.on('BL', (pressed: boolean) => {});
pad.on('BD', (pressed: boolean) => {});
pad.on('BR', (pressed: boolean) => {});
pad.on('BT', (pressed: boolean) => {});
pad.on('BMR', (pressed: boolean) => {});
pad.on('BML', (pressed: boolean) => {});
pad.on('L1', (pressed: boolean) => {});
pad.on('R1', (pressed: boolean) => {});
pad.on('L2', (pressed: boolean) => {});
pad.on('R2', (pressed: boolean) => {});
Note:
X
Y
A
B
are differently mapped on Nintendo and XBox, So it's inportant to choose the correct mapping.- Analogic pad and digital pad values are all [0-255] value, so any resting pad value will be [128, 128]
usage code
import PlayStationController from './src/index';
const ctrl = new PlayStationController();
ctrl.on('LStick', (pos) => console.log(`L: ${pos[0]}, ${pos[1]}`));
ctrl.on('RStick', (pos) => console.log(`R: ${pos[0]}, ${pos[1]}`));
ctrl.on('dpad', (pos) => console.log(`dPad: ${pos[0]}, ${pos[1]}`));
ctrl.on('square', (pressed: boolean) => console.log('square: ' + pressed));
ctrl.on('cross', (pressed: boolean) => console.log('cross: ' + pressed));
ctrl.on('circle', (pressed: boolean) => console.log('circle: ' + pressed));
ctrl.on('triangle', (pressed: boolean) => console.log('triangle: ' + pressed));
ctrl.on('connected', () => console.log('Xbox controller connected'));
ctrl.on('error', (err) => console.error(err));
ctrl.start();
TODO
- add calibration code to add new devices.
- add Gyroscope.