A simple SDK for handling gamepad input in the browser using the navigator.Gamepad
API. This SDK provides an easy way to detect button presses, releases, axis movements, and gamepad connection/disconnection events.
- Detect gamepad button presses and releases.
- Track axis changes (analog stick movements).
- Listen for gamepad connection and disconnection events.
- Works with multiple gamepads.
You can install the SDK via NPM:
npm install @pindakaasman/gamepad-sdk
Here’s a basic example of how to use the Gamepad SDK in your project:
const GamepadSDK = require('@pindakaasman/gamepad-sdk');
// Initialize the SDK
const myGamepad = new GamepadSDK();
// Listen for button presses
myGamepad.addEventListener('buttonpress', (event) => {
console.log(`Button ${event.buttonIndex} was pressed on gamepad: ${event.gamepad.id}`);
});
// Listen for axis changes
myGamepad.addEventListener('axischange', (event) => {
console.log(`Axis ${event.axisIndex} changed to ${event.axisValue} on gamepad: ${event.gamepad.id}`);
});
// Listen for gamepad connection
myGamepad.addEventListener('connect', (event) => {
console.log(`Gamepad connected: ${event.gamepad.id}`);
});
// Listen for gamepad disconnection
myGamepad.addEventListener('disconnect', (event) => {
console.log(`Gamepad disconnected: ${event.gamepad.id}`);
});
Triggered when a button is pressed.
-
gamepad
: The gamepad object. -
button
: The button object. -
buttonIndex
: The index of the pressed button.
Triggered when a button is released.
-
gamepad
: The gamepad object. -
button
: The button object. -
buttonIndex
: The index of the released button.
Triggered when a gamepad axis (such as an analog stick) changes position.
-
gamepad
: The gamepad object. -
axisValue
: The value of the axis (ranging from -1.0 to 1.0). -
axisIndex
: The index of the axis that changed.
Triggered when a gamepad is connected.
-
gamepad
: The connected gamepad object.
Triggered when a gamepad is disconnected.
-
gamepad
: The disconnected gamepad object.
The Gamepad API is still an experimental feature in some browsers. Ensure that you check browser compatibility before using this SDK in production.
- Chrome
- Firefox
- Edge
- Opera
This SDK is licensed under the ISC License.
If you'd like to contribute to the project or work on it locally, follow these instructions to set up your development environment:
Ensure that you have the following installed:
-
Clone the repository:
git clone git@github.com:RamonGebben/Gamepad-SDK.git cd Gamepad-SDK
-
Install dependencies: After cloning the repo, run the following command to install all project dependencies:
npm install
You can start developing the SDK locally by editing the source code directly in the repository. If you're adding new features or fixing bugs, ensure everything works as expected.
If you add tests to the project, you can run them using:
npm test
We use npm's versioning to manage the package versions. To bump the version of the package and publish to NPM:
-
Bump the version:
- For a patch version:
npm version patch
- For a minor version:
npm version minor
- For a major version:
npm version major
- For a patch version:
-
Publish to NPM (requires NPM authentication):
npm publish
This repository is configured to automatically publish to NPM whenever changes are pushed to the main
branch. The GitHub Actions workflow handles the build, test, and publish process.
For more details, see the .github/workflows/npm-publish.yml
file.
Feel free to fork this repository, make your changes, and submit a pull request. Contributions are always welcome!