# Controller support for web games, in one prompt

Source: https://gamepadly.com/sdk

`@gamepadly/sdk` is an open-source JavaScript library that adds Xbox, PlayStation and Switch controller support to web games, with haptics and DualSense adaptive triggers. Give your coding agent one prompt, or add it yourself in about ten lines.

- [Add a controller in 5 minutes](https://gamepadly.com/sdk/start/add-controller)
- [Use with an AI agent](https://gamepadly.com/sdk/start/ai-agents)
- [GitHub](https://github.com/gamepadly/gamepadly-sdk)

## Paste this into your coding agent

```text
Read https://gamepadly.com/sdk/llms.txt, then add controller support to this game with @gamepadly/sdk. Keep keyboard and mouse working, add haptics to the main game events, and write a test that runs without a controller.
```

Works with any agent that can read a URL, such as Claude Code, Cursor, Codex or GitHub Copilot.
[`llms.txt`](https://gamepadly.com/sdk/llms.txt) is one page written for agents: which API to use in which situation,
copy-ready recipes, the mistakes to avoid, and a test template. [More prompts](https://gamepadly.com/sdk/start/ai-agents).

## Or add it yourself

### Existing keyboard game
The bridge presses the keys your game already listens for. The game's code does not change.

```ts
import { createGamepadly } from '@gamepadly/sdk';
import { createKeyMouseBridge } from '@gamepadly/sdk/bridge';

const hub = createGamepadly();
const bridge = createKeyMouseBridge({ player: hub, profile: 'platformer' });
bridge.start(); // D-pad and left stick → arrow keys, A / Cross → Space, ...

// Then make the game's own events felt:
bridge.triggerHaptic('heavy_impact', { pan: -0.5 });
```

[Full walkthrough →](https://gamepadly.com/sdk/start/add-controller)

### New game
Bind each action to controller, keyboard and mouse at once.

```ts
import { createGamepadly } from '@gamepadly/sdk';

const hub = createGamepadly();
const p1 = hub.player(0);
const actions = p1.createActions({
  actions: { jump: ['south', 'Space'], fire: ['r2', 'MouseLeft'] },
  axes: { moveX: { stick: 'lx', negativeKeys: ['KeyA'], positiveKeys: ['KeyD'] } },
});

// In your game loop:
actions.update(p1.input.getState(), performance.now());
if (actions.justPressed('fire')) p1.haptics.playPreset('recoil');
```

[Start a new game →](https://gamepadly.com/sdk/start/quick-start)

### Single HTML file
No build step: load the SDK from a CDN.

```html
<script type="module">
  import { createGamepadly, createKeyMouseBridge } from 'https://esm.sh/@gamepadly/sdk@0.9.0/browser';

  const hub = createGamepadly();
  createKeyMouseBridge({ player: hub, profile: 'arcade-4way' }).start();
</script>
```

[Installation options →](https://gamepadly.com/sdk/start/installation)

Using a framework? See the recipes for [Three.js](https://gamepadly.com/sdk/recipes/three-js), [Phaser](https://gamepadly.com/sdk/recipes/phaser) and
[React](https://gamepadly.com/sdk/recipes/react).

## The right motor on every controller

One call, `p1.haptics.playPreset('recoil')`, plays on whatever the controller has and returns
which output it used, so you can log it or assert on it in tests:

| Controller | What the player feels | Returns |
| :--- | :--- | :--- |
| DualSense with optional 4-channel audio (Windows, USB) | Voice-coil haptics, panned left and right | `'4ch_vcm'` |
| Switch Pro Controller, connected with one click (WebHID) | HD Rumble | `'hd_rumble'` |
| Xbox Series controller in Chrome or Edge | Trigger motors and rumble | `'impulse_triggers'` |
| DualSense and other controllers | Dual-motor rumble | `'dual_rumble'` |
| No controller | Nothing; the game keeps running | `'none'` |

Want to feel the difference? Plug a controller into the [online controller tester](https://gamepadly.com/), built on this SDK.

## What you get

### Every controller, one API
    Xbox, PlayStation, Switch Pro and generic gamepads read the same way. Buttons are named by position:
    `south` is A on Xbox, Cross on PlayStation and B on Nintendo. Up to four players.

### Console features in the browser
    One permission click in desktop Chrome or Edge unlocks DualSense adaptive triggers, gyro, touchpad and
    lightbar, and Switch Pro HD Rumble. Everything falls back on other controllers.

### Tests without a controller
    A mock controller you press in code. Tests run in Vitest or Jest in plain Node, so coding agents and CI
    can check controller support.

### Small and safe
    17.2 KB gzipped for the core, zero dependencies, MIT. Rumble and triggers reset when
    the page closes, and the SDK never writes controller firmware.

## Browser and controller support

Controller input and rumble work in any modern browser that exposes the controller through the
Gamepad API, with no prompt. Adaptive triggers, gyro, touchpad and HD Rumble need WebHID, which
desktop Chrome and Edge support. Verified on real hardware so far: **Windows with Chrome and Edge**.
Other platforms are implemented but not yet verified; the [compatibility matrix](https://gamepadly.com/sdk/compatibility)
tracks every combination.

## Examples

Each example is one HTML file you can open, read and copy. Every one has a simulation mode, so you can try it without a controller.

- [Keyboard game → controller](https://gamepadly.github.io/gamepadly-sdk/examples/bridge-arcade.html)
- [Haptics playground](https://gamepadly.github.io/gamepadly-sdk/examples/playground.html)
- [Custom controller tester](https://gamepadly.github.io/gamepadly-sdk/examples/diagnostics.html)

## FAQ

### What is @gamepadly/sdk?

An open-source (MIT) JavaScript and TypeScript library that adds controller support to web games: one API for Xbox, PlayStation, Switch Pro and other gamepads, haptics that use the best motor each controller has, DualSense adaptive triggers, and a bridge that makes an existing keyboard game playable on a controller without changing its code. It has no runtime dependencies.

### How do I add controller support to a game made with an AI coding agent?

Give the agent this prompt: “Read https://gamepadly.com/sdk/llms.txt, then add controller support to this game with @gamepadly/sdk.” The [llms.txt](https://gamepadly.com/sdk/llms.txt) file tells the agent which API to use, how to keep keyboard controls working, and how to write a test that runs without a controller.

### Do players need to install anything?

No. Controller input and rumble use the browser’s Gamepad API with no prompt. DualSense adaptive triggers, gyro and touchpad and Switch Pro HD Rumble need one permission dialog (WebHID) in desktop Chrome or Edge. Only the optional DualSense speaker and 4-channel haptics on Windows can use a companion app, and games never require it.

### Which controllers and browsers are supported?

Xbox controllers (including impulse triggers in Chrome and Edge), DualSense and DualSense Edge, the Switch Pro Controller, and any other controller the browser exposes through the Gamepad API. Verified on real hardware so far: Windows with Chrome and Edge. macOS, Linux, Firefox, Safari and mobile browsers are implemented but not yet verified. See the [compatibility matrix](https://gamepadly.com/sdk/compatibility).

### Does it work with Three.js, Phaser, PixiJS or React?

Yes. The SDK does not depend on a framework: you read input once per frame from whatever loop you already have. In React, create the hub in `useEffect` and call `dispose()` in its cleanup.

### How do I test controller support without a controller?

`createMockPlayerHandle()` from `@gamepadly/sdk/game` is a fake controller whose buttons and sticks you set in code. Tests run in Vitest or Jest in plain Node. See [the test example](https://gamepadly.com/sdk/start/add-controller#7-test-it-without-a-controller).

### Is it free?

Yes. MIT licensed, no account, no API key, no server. It runs entirely in the player’s browser.
