# Use with AI coding agents

> Prompts, project rules and a checklist for adding controller support to a web game with Claude Code, Cursor, Codex, GitHub Copilot or any coding agent that can read a URL.

Source: https://gamepadly.com/sdk/start/ai-agents

Coding agents build playable web games in minutes, but they almost never add controller support. Point one at
[`llms.txt`](https://gamepadly.com/sdk/llms.txt), a single page written for agents, and it can add it in one pass: which API to use in
which situation, copy-ready recipes, the mistakes to avoid, and a test that runs without a controller.

## The prompt

```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.
```

This works for most games.

## Prompts for specific goals

### Keyboard game, minimal change

```text
Read https://gamepadly.com/sdk/llms.txt. Make this keyboard game playable on a controller with the @gamepadly/sdk bridge (Recipe A). Do not change the game's input code. Check where it listens for keydown: if it is an element such as a canvas, pass it as keyTarget.
```

### New game

```text
Read https://gamepadly.com/sdk/llms.txt. Build the input layer of this game with createActions() from @gamepadly/sdk so every action works on controller, keyboard and mouse.
```

### Console-style feel

```text
Read https://gamepadly.com/sdk/llms.txt. Add haptics to hits, shots and pickups, and a "Connect controller" button that enables DualSense adaptive triggers through WebHID. The game must still work without it.
```

### Racing game

```text
Read https://gamepadly.com/sdk/llms.txt. Map steering to the left stick and gas / brake to R2 / L2 as analog values, add the 'abs' trigger effect to the brake, and an engine rumble with createContinuous('engine').
```

### Tests only

```text
Read https://gamepadly.com/sdk/llms.txt. Write Vitest tests for this game's controller support with createMockPlayerHandle() (Recipe D). They must run in Node without a browser.
```

## What the agent reads

| File | Contents |
| :--- | :--- |
| [`/sdk/llms.txt`](https://gamepadly.com/sdk/llms.txt) | The agent guide: decision table, four recipes, haptic and button reference, rules and a "done when" checklist |
| Any page + `.md` | Every docs page as Markdown, for example [`/sdk/start/add-controller.md`](https://gamepadly.com/sdk/start/add-controller.md) |
| [`/sdk/llms-small.txt`](https://gamepadly.com/sdk/llms-small.txt) | All guides in one file, without the API reference |
| [`/sdk/llms-full.txt`](https://gamepadly.com/sdk/llms-full.txt) | Every page, including the full API reference |

Every code block in these files is type-checked against the SDK source on each change, and the test recipes are
run, so an agent copying them gets code that compiles against the current release.

## Keep the rules in your project

If the agent will work on the game again later, add this to `AGENTS.md`, `CLAUDE.md`, `.cursor/rules` or your
agent's equivalent so it keeps the controller support intact:

```markdown
## Controller support (@gamepadly/sdk)
- Reference: https://gamepadly.com/sdk/llms.txt
- One hub per page (`createGamepadly()`); dispose the hub, bridge, action maps and loops on teardown.
- Keyboard and mouse must keep working. The controller is added, not substituted.
- Bind buttons by position (`south`, `east`, ...); show labels with `getButtonLabel()`.
- Call `hub.connectWebHID()` only from a click. WebHID features are optional upgrades.
- Haptics go on game events (hit, shot, pickup), not on every key press.
- Controller tests use `createMockPlayerHandle()` and must keep passing.
```

## Check the agent's work

The job is done when:

- Keyboard and mouse still work exactly as before.
- With a controller, movement, the main actions and pause work, and prompts show the right button labels.
- A "Press any button" hint shows until a controller is detected.
- Two or three game events trigger haptics.
- Teardown disposes everything, and a test with `createMockPlayerHandle()` passes.

To try it by hand, open the game with a controller connected. To see what your controller reports, use the
[online controller tester](https://gamepadly.com/).

## Common mistakes

| Symptom | Cause | Fix |
| :--- | :--- | :--- |
| The controller does nothing | The game listens on an element such as its canvas, and events dispatched on `document` never reach it | Pass the element as `keyTarget` ([details](https://gamepadly.com/sdk/start/add-controller#2-find-how-your-game-reads-the-keyboard)) |
| Nothing until a button is pressed | Browsers hide controllers until the player presses a button | Show a "Press any button" hint |
| The WebHID picker never opens | `connectWebHID()` was called on page load | Call it from a click handler |
| Trigger effects never apply | They were set before WebHID connected | Re-apply in `hub.onCapabilitiesChange` |
| Rumble keeps going or two hubs fight | A hub was created per component or twice in React StrictMode | One hub per page, disposed in the effect cleanup |
| Test sets `state.buttons.south` and nothing happens | Mock state uses base names | Use `state.buttons.cross` (south), `circle` (east), `square` (west), `triangle` (north) |

More symptoms and fixes are in [Troubleshooting](https://gamepadly.com/sdk/troubleshooting).
