Bash.tv → GitHub → itch.io

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 GitHub

Universal 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

Checklist