One input layer for browser games that handles keyboard, mouse, touch and gamepad through named actions ("jump", "fire", "left"), with pressed / held / released states, analog axes, dead zones, rebinding saved to storage, the last-used device for showing the right button hints, and fixes for stuck keys, page scrolling and iframe focus. Includes a drop-in input.js. Use when adding controls, game...
Download skill (.zip) Files on GitHubUniversal input system
Game code asks about actions, never devices. input.down("jump") works the same whether it came from Space, a
gamepad A button or a touch button. Drop-in module: assets/input.js (tested). It works with the game-scaffolding template.
import { createInput } from "./core/input.js";
const input = createInput(canvas, {
left: ["KeyA", "ArrowLeft", "pad:14", "axis:0-"],
right: ["KeyD", "ArrowRight", "pad:15", "axis:0+"],
up: ["KeyW", "ArrowUp", "pad:12", "axis:1-"],
down: ["KeyS", "ArrowDown", "pad:13", "axis:1+"],
jump: ["Space", "KeyZ", "pad:0"],
fire: ["KeyX", "mouse:0", "pad:2"],
pause: ["Escape", "KeyP", "pad:9"],
});
// each fixed step:
input.poll(); // reads gamepads; call first
if (input.pressed("jump")) ... // true for one step
if (input.down("fire")) ... // held
const { x, y } = input.stick(); // -1..1 from keys, d-pad or left stick (normalized, dead zone applied)
input.endStep(); // last; clears pressed/released
Touch buttons (mobile-game-controls) call input.press("jump") / input.release("jump"), so they feed the same actions.
Rules
- Binding names:
KeyboardEvent.code(KeyW, layout independent) for keys.mouse:0|1|2for mouse buttons.pad:Nfor standard gamepad buttons (0 A, 1 B, 2 X, 3 Y, 9 Start, 12–15 d-pad).axis:N±for sticks. - Edge vs level: menus and jumps use
pressed, movement usesdown/stick. Clear edges once per game step, not per frame. - Stuck keys: release everything on
blurandvisibilitychange(the module does it). - Scrolling:
preventDefaultfor game keys (arrows, space) only while the game has focus. Never block Tab or browser shortcuts. - Iframes (Bash preview, itch.io): keys only arrive after the frame gets focus. Focus the canvas on the first pointerdown and show “click to play” until then.
- Dead zone: 0.2 radial on sticks. Rescale after it, so movement is smooth from the edge of the dead zone.
- Gamepad:
navigator.getGamepads()must be polled every step. Gamepads appear only after a button press. Usemapping === "standard". - Rebinding:
input.rebind(action, index, code)+input.saveBindings()(localStorage, try/catch). Offer “reset to default”. - Button hints:
input.lastDeviceis"keyboard" | "mouse" | "touch" | "gamepad". Show “Press Space”, “Press A” or “Tap” to match. - Accessibility: allow remapping, don’t require holding several keys at once, offer toggle instead of hold for long holds.
Checklist
- Every action reachable on keyboard, touch and (if supported) gamepad
- No page scroll from arrows or space while playing. Tab still works
- Alt-tab while holding a key doesn’t leave it stuck
- Works inside an iframe after one click
- Hints switch when the player changes device