npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gamepad-ts

v1.1.1

Published

gamepad contoller in typescript

Downloads

3

Readme

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]) => {}); // Left stick moved event (x, y)
pad.on('RStick', (pos: [number, number]) => {}); // Right stick moved event (x, y)
pad.on('dpad', (pos: [number, number]) => {}); // Digital Pad change event (x, y)
pad.on('LStickBt', (pressed: boolean) => {}); // Left stick press
pad.on('RStickBt', (pressed: boolean) => {}); // Right stick press
pad.on('X', (pressed: boolean) => {}); // nintendo / xbox X Button
pad.on('A', (pressed: boolean) => {}); // nintendo / xbox A Button
pad.on('B', (pressed: boolean) => {}); // nintendo / xbox B Button
pad.on('Y', (pressed: boolean) => {}); // nintendo / xbox Y Button
pad.on('+', (pressed: boolean) => {}); // nintendo plus Button
pad.on('-', (pressed: boolean) => {}); // nintendo less Button
pad.on('L', (pressed: boolean) => {}); // nintendo L Button
pad.on('R', (pressed: boolean) => {}); // nintendo R Button
pad.on('ZL', (pressed: boolean) => {}); // nintendo Left Z
pad.on('ZR', (pressed: boolean) => {}); // nintendo Rigth Z
  • 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]) => {}); // Left stick moved event (x, y)
pad.on('RStick', (pos: [number, number]) => {}); // Right stick moved event (x, y)
pad.on('dpad', (pos: [number, number]) => {}); // Digital Pad change event (x, y)
pad.on('LStickBt', (pressed: boolean) => {}); // Left stick press
pad.on('RStickBt', (pressed: boolean) => {}); // Right stick press
pad.on('X', (pressed: boolean) => {}); // nintendo / xbox X Button
pad.on('A', (pressed: boolean) => {}); // nintendo / xbox A Button
pad.on('B', (pressed: boolean) => {}); // nintendo / xbox B Button
pad.on('Y', (pressed: boolean) => {}); // nintendo / xbox Y Button
pad.on('start', (pressed: boolean) => {}); // xbox start
pad.on('back', (pressed: boolean) => {}); // xbox back
pad.on('LB', (pressed: boolean) => {}); // xbox Left Bumper
pad.on('RB', (pressed: boolean) => {}); // xbox Right Bumper
pad.on('LT', (pressed: boolean) => {}); // xbox Left Trigger
pad.on('RT', (pressed: boolean) => {}); // xbox Rigth trigger
  • 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]) => {}); // Left stick moved event (x, y)
pad.on('RStick', (pos: [number, number]) => {}); // Right stick moved event (x, y)
pad.on('dpad', (pos: [number, number]) => {}); // Digital Pad change event (x, y)
pad.on('LStickBt', (pressed: boolean) => {}); // Left stick press
pad.on('RStickBt', (pressed: boolean) => {}); // Right stick press
pad.on('square', (pressed: boolean) => {}); // Playastation Button
pad.on('cross', (pressed: boolean) => {}); // Playastation Button
pad.on('circle', (pressed: boolean) => {}); // Playastation Button
pad.on('triangle', (pressed: boolean) => {}); // Playastation Button
pad.on('share', (pressed: boolean) => {}); // Playstation share
pad.on('options', (pressed: boolean) => {}); // Playstation options
pad.on('L1', (pressed: boolean) => {}); // playstaton Left Button
pad.on('R1', (pressed: boolean) => {}); // playstaton Right Button
pad.on('L2', (pressed: boolean) => {}); // playstaton Left trigger
pad.on('R2', (pressed: boolean) => {}); // playstaton Right trigger
  • 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]) => {}); // Left stick moved event (x, y)
pad.on('RStick', (pos: [number, number]) => {}); // Right stick moved event (x, y)
pad.on('dpad', (pos: [number, number]) => {}); // Digital Pad change event (x, y)
pad.on('LStickBt', (pressed: boolean) => {}); // Left stick press
pad.on('RStickBt', (pressed: boolean) => {}); // Right stick press
pad.on('BL', (pressed: boolean) => {}); // Button left
pad.on('BD', (pressed: boolean) => {}); // Button down
pad.on('BR', (pressed: boolean) => {}); // Button Rigth
pad.on('BT', (pressed: boolean) => {}); // Button Top
pad.on('BMR', (pressed: boolean) => {}); // Button Middle Rigth
pad.on('BML', (pressed: boolean) => {}); // Button Middle Left
pad.on('L1', (pressed: boolean) => {}); // generic / playstaton Left Bumper
pad.on('R1', (pressed: boolean) => {}); // generic / playstaton Right Bumper
pad.on('L2', (pressed: boolean) => {}); // generic / playstaton Left trigger
pad.on('R2', (pressed: boolean) => {}); // generic / playstaton Right trigger

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();
/*
 * A,B,X,Y buttons
 */
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));
/*
 * Setup
 */
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.