Remappable controls and input preferences for browser games. A tested controls store with several keys and gamepad buttons per action, a "press a key" capture (Esc cancels, Backspace clears), swapping or allowing conflicts, locked actions (pause), reserved browser keys, reset per action or all, saving with bad-save protection, readable key names including the real letters on AZERTY and other la...
Download skill (.zip) Files on GitHubGame control customization
assets/rebind.js
import { createControls, holdOrToggle, loadLayoutMap, padStyle } from "./rebind.js";
const controls = createControls({ save, locked: ["pause"], defaults: {
left: { keys: ["KeyA", "ArrowLeft"], pad: [14] }, right: { keys: ["KeyD", "ArrowRight"], pad: [15] },
jump: { keys: ["Space", "KeyW"], pad: [0] }, sprint: { keys: ["ShiftLeft"], pad: [10], mode: "hold" }, pause: { keys: ["Escape", "KeyP"], pad: [9] } } });
const input = createInput(canvas, screen, controls.keyBindings()); // game-scaffolding input; rebuild on controls.on(...)
controls.useLayout(await loadLayoutMap()); // AZERTY shows "Z" for KeyW (Chrome/Edge)
controls menu item: { type: "button", label: `Jump: ${controls.label("jump")}`, action: async () => {
const r = await controls.capture("jump", 0); // window keydown → controls.feedKey(e.code)
if (r.swapped?.length) notes.push({ text: `Swapped with ${r.swapped[0]}`, kind: "warning" }); } }
gamepad: controls.capture("jump", 0, "pad"); poll buttons → controls.feedPad(index); label("jump", { device: "pad", style: padStyle(gamepad.id) })
const sprinting = holdOrToggle(controls.get("sprint").mode); each frame: sprinting(input.isDown("sprint"))
- Conflicts:
swap(default) gives the other action your old key, so nothing ends up unbound.allowkeeps both. - Locked actions (pause, menu back) can’t be changed or stolen, so players can’t lock themselves out.
- Reserved keys (Tab, F5, F11, F12, the Windows/Cmd keys) are refused: the browser owns them.
problems()lists actions with nothing bound; show a warning in the menu.- Show the bound key everywhere: tutorials, hints and button prompts use
controls.label(action).
Input preferences to offer
Remapping (keyboard and gamepad), hold or toggle for sprint, aim, crouch; sensitivity and invert Y; dead zone for sticks; touch button size and position, left-handed layout (mirror the touch controls); vibration on or off. Store the non-binding ones in game-settings-system.
Tested
Node tests: capture with swap, locked and reserved keys, Esc cancelling, Backspace clearing a slot, gamepad capture ignoring keys, labels for keys and Xbox/PlayStation buttons, key names with a layout map, controller style detection, saving and reset, the scaffold binding shape, toggle mode, and a corrupt save being filtered. Used in the UI kit demo (rebinding with a swap warning).
Pitfalls
Binding by e.key instead of e.code (breaks on other layouts and with Shift), hard-coded key names in tutorial text,
rebinding pause or Back, losing a key when two actions clash, and no way to reset.