Skip to content

๐ŸŽฎ Simple customizable event binding for the HTML Gamepad API.

License

Notifications You must be signed in to change notification settings

omssp/gamepad.js

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

55 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

gamepad.js

๐ŸŽฎ Simple customizable event binding for the HTML Gamepad API.

Setup

$ npm install neogeek/gamepad.js
const gamepad = new Gamepad();

gamepad.start();

Usage

Pressed

const gamepad = new Gamepad();

gamepad.on('pressed', 'button_1', ({ id, gamepadIndex, value }) => {
    console.log(
        `${id} was pressed by player ${
            gamepadIndex + 1
        } with a value of ${value}!`
    );
});

gamepad.start();

Held

const gamepad = new Gamepad();

gamepad.on('held', 'button_1', ({ id, gamepadIndex, value }) => {
    console.log(
        `${id} is being held by player ${
            gamepadIndex + 1
        } with a value of ${value}!`
    );
});

gamepad.start();

Released

const gamepad = new Gamepad();

gamepad.on('released', 'button_1', ({ id, gamepadIndex, value }) => {
    console.log(
        `${id} was released by player ${
            gamepadIndex + 1
        } with a value of ${value}!`
    );
});

gamepad.start();

Axes

const gamepad = new Gamepad();

gamepad.on('axes', 0, ({ id, gamepadIndex, value }) => {
    console.log(
        `${id} was held by player ${gamepadIndex + 1} with a value of ${value}!`
    );
});

gamepad.on('axes', 1, ({ id, gamepadIndex, value }) => {
    console.log(
        `${id} was held by player ${gamepadIndex + 1} with a value of ${value}!`
    );
});

gamepad.start();

Removing Event Listeners

gamepad.off('pressed', 'button_1');

gamepad.off('held', 'button_1');

gamepad.off('released', 'button_1');

Migrating from v0.x to v1

  1. All keypad support has been removed to reduce complexity in the package.
  2. Gamepad type press is now pressed to conform to the browser standard.
  3. Gamepad type hold is now held.
  4. Gamepad type release is now released.

Support

The Gamepad browser API is very fragmented. VERY. Because of this we need your help. If you have a gamepad that isn't listed below, please visit the Gamepad.js Recorder and follow the instructions to create a controller mapping file. Either submit that in an issue or a PR. Once the mapping has been confirmed by at least one other user, it can be merged into the repo and it will be avaible in the next deploy.

Controller Browser OS Supported
PS4 Wired Chrome macOS โŒ
PS4 Wired Chrome Windows โœ…
PS4 Wired Firefox macOS โŒ
PS4 Wired Firefox Windows โœ…
PS4 Wired Microsoft Edge Windows โœ…
PS4 Wired Opera macOS โŒ
PS4 Wired Opera Windows โœ…
PS4 Wired Safari macOS โŒ
Switch Pro Wired Chrome macOS โœ…
Switch Pro Wired Chrome Windows โœ…
Switch Pro Wired Firefox macOS โŒ
Switch Pro Wired Firefox Windows โŒ
Switch Pro Wired Opera macOS โœ…
Switch Pro Wired Opera Windows โŒ
Switch Pro Wired Safari macOS โŒ
Xbox One Wired Chrome macOS โœ…
Xbox One Wired Chrome Windows โœ…
Xbox One Wired Firefox macOS โœ…
Xbox One Wired Firefox Windows โœ…
Xbox One Wired Microsoft Edge Windows โœ…
Xbox One Wired Opera macOS โœ…
Xbox One Wired Opera Windows โœ…
Xbox One Wired Safari macOS โœ…

About

๐ŸŽฎ Simple customizable event binding for the HTML Gamepad API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%