melonjs-input
What it does
It explains how to map keys to named actions, respond to mouse and touch, and handle gamepads in a melonJS game. It is for game developers building controls.
When to use it
- βWhen a developer wants movement keys to work the same way as a gamepad.
- βWhen a developer needs a jump or fire key to trigger once per press.
- βWhen a developer's clickable object does not respond to pointer input.
Skill link
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-input/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βMake the arrow keys move my character and make the space bar jump only once per press.β
5 new skills a week. 1 minute to read.
How it works
The skill tells the AI to bind keys to action names, then check those actions in the update loop, locking one-time actions so they fire once per press.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Input: keyboard, pointer and gamepad
Keyboard goes through named actions
Do not poll key codes. Bind a key to an action string, then poll the action β this is what lets gamepad and virtual controls map onto the same handling.
import { input } from "melonjs";
input.bindKey(input.KEY.LEFT, "left");
input.bindKey(input.KEY.RIGHT, "right");
input.bindKey(input.KEY.X, "jump", true); // true = locked: one press per keydown
// in update()
if (input.isKeyPressed("left")) { /* β¦ */ }
if (input.isKeyPressed("jump")) { /* fires once per press, not per frame */ }
The third argument to bindKey is lock (default false). Without it,
isKeyPressed stays true for as long as the key is held β correct for movement,
wrong for jump or fire. A locked action stays consumed until the key is released
or you call input.unlockKey("jump"). Use input.keyStatus(action) when you
want the raw held/not-held state regardless of locking.
A poll can miss a press entirely
isKeyPressed is sampled once per frame, and keyup decrements the key's
counter the moment it arrives β locked or not. A press that goes down and up
between two frames therefore leaves the counter back at zero and is never
seen. At 60fps that is anything under ~16ms: rare from a human finger, routine
from a synthetic event, and more likely the slower the frame rate.
For a screen where a missed press is a dead end β a splash, a "press any key", a notice to acknowledge β listen for the event instead, which cannot be missed:
import { event } from "melonjs";
// action is the bound action name, or undefined for an unbound key
const onKey = (action) => { if (action === "menuSelect") continue(); };
event.on(event.KEYDOWN, onKey); // in onResetEvent
event.off(event.KEYDOWN, onKey); // in onDestroyEvent β it does not self-remove
KEYDOWN fires for every key, bound or not, so matching on action is what
narrows it; it also repeats while the key is held, so guard anything that must
happen once. Keep polling for movement and for a menu cursor, where one step
per press and a key held across a state change are the things that matter.
The full signature is bindKey(keyCode, action, lock?, preventDefault?);
preventDefault defaults to input.preventDefault (itself true). Pass the
fourth argument per binding, or change the global default with
input.setPreventDefault(false) β assigning input.preventDefault = false
does not work, it is a read-only module binding.
Pointer events need isKinematic = false
This is the trap. Renderable.isKinematic defaults to true, and both the
broadphase and the pointer dispatcher skip any candidate whose isKinematic is
still true. The handler registers successfully and simply never fires.
class Button extends Renderable {
constructor(x, y, w, h) {
super(x, y, w, h);
this.isKinematic = false; // β required
}
onActivateEvent() {
input.registerPointerEvent("pointerdown", this, this.onDown.bind(this));
}
onDeactivateEvent() {
input.releasePointerEvent("pointerdown", this);
}
}
Container, Camera2d, Draggable, DropTarget and the UI* elements set it
to false for you, and so does attaching a physics body (Body's constructor
sets ancestor.isKinematic = false) β so plain Renderable and Sprite
subclasses with no body are the ones that catch people.
Register in onActivateEvent and release in onDeactivateEvent, not in the
constructor β pooled objects are reused and would otherwise accumulate handlers.
app.viewport works as a whole-screen region when you want global clicks. Note
it is asked before everything else, not as a fallback after nothing matched:
the dispatcher appends it to the candidate list and walks that list from the
end. So a viewport handler must not return false unless it really means to
swallow every pointer event in the game.
The accepted event names are exactly "pointerdown", "pointerup",
"pointermove", "pointercancel", "pointerenter", "pointerover",
"pointerleave" and "wheel". Anything else β "click", "mousedown",
"touchstart" β makes both registerPointerEvent and releasePointerEvent
throw invalid event type. melonJS maps the canonical name onto whichever
mouse/touch events the device actually supports, so you always register the
pointer name.
Which object gets the event
The dispatcher collects the regions nea
FAQ
Why does my jump fire many times?
Without locking, a held key counts as pressed each frame, so the skill recommends locking one-time actions.
Why do pointer events not reach my object?
The skill mentions a kinematic setting that can silently block pointer events.
Does it work with gamepads?
Yes. The skill says gamepads can map onto the same action names.
Details
Original description by the author
Use this skill for keyboard, pointer, mouse, touch and gamepad input in melonJS. Covers the bindKey action indirection, registerPointerEvent on regions, the isKinematic requirement that silently blocks pointer events, which of several overlapping objects receives an event and how to consume it, world vs screen pointer coordinates, and gamepad mapping onto the same actions. Triggers on: input, bindKey, isKeyPressed, KEY, registerPointerEvent, releasePointerEvent, pointerdown, pointerup, pointermove, POINTERMOVE, gamepad, bindGamepad, triggerKeyEvent, virtual gamepad, touch controls, joystick, g
repo: melonjs/melonJS
Popularity
- GitHub stars
- 6.4k
- Ranking position
- #14204
- License
- MIT
- Updated
- 11/2/2026
Comments
- β¦
More skills from this repository
melonjs-20-migration
6.4kHelps your agent write melonJS game code for version 20 and avoid outdated methods from older versions, such as the required async startup.
melonjs-3d-assets
6.4kHelps your agent load 3D models such as glTF, GLB and OBJ files into a melonJS browser game, with scale and lighting options.
melonjs-3d
6.4kHelps your agent build 3D or 2.5D scenes in a melonJS browser game, covering cameras, meshes, lights and depth, using the engine's own axis rules.
melonjs-audio
6.4kHelps your agent add sound effects and music to a melonJS game, including audio setup, background tracks, volume control and spatial sound.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.