Keyboard, mouse and gamepad input.

Two distinct mechanisms live here, and picking the right one matters:

  • Key mappings (bind) register a command the player can rebind in the game's own settings menu. They work while the game window has focus, cost nothing per frame, and are the right default.
  • Control polling (isPressed and friends) reads the game's own controls each frame. Necessary for held keys, analogue axes and anything that has to react mid-frame — but it only works inside a per-frame loop.
Controls.bind("openInventory", "Open inventory", "i", () -> inventory.open());

Thread.everyFrame(() -> {
    if (Controls.isJustPressed(Context)) interact();
});

Static variables

@:value(0)staticinlineread onlyDEFAULT_PAD:Int = 0

Pad index 0 — keyboard and the primary gamepad. Every method here defaults to it.

Static methods

staticbind(commandName:String, description:String, defaultKey:String, onPressed:() ‑> Void, ?onReleased:() ‑> Void):Void

Binds a key to an action, showing up in the player's keybinding settings so they can change it.

Parameters:

commandName

A unique command name; must not collide with another resource's.

description

The label shown in the settings menu.

defaultKey

The default binding, e.g. "i", "F5", "MOUSE1".

onPressed

Runs when the key goes down.

onReleased

Runs when it comes back up, if given.

@:value({ padIndex : DEFAULT_PAD })staticinlinedisable(control:Control, padIndex:Int = DEFAULT_PAD):Void

Stops the game reacting to a control. Only lasts one frame, so call it from a per-frame loop for as long as the control should stay disabled.

@:value({ padIndex : DEFAULT_PAD })staticinlinedisableAll(padIndex:Int = DEFAULT_PAD):Void

Disables every control for one frame. Also one-frame — loop it.

@:value({ padIndex : DEFAULT_PAD })staticinlineenable(control:Control, padIndex:Int = DEFAULT_PAD):Void

@:value({ padIndex : DEFAULT_PAD })staticinlineenableAll(padIndex:Int = DEFAULT_PAD):Void

@:value({ padIndex : DEFAULT_PAD })staticinlineisDisabledJustPressed(control:Control, padIndex:Int = DEFAULT_PAD):Bool

@:value({ padIndex : DEFAULT_PAD })staticinlineisDisabledPressed(control:Control, padIndex:Int = DEFAULT_PAD):Bool

As isPressed, but also true for a control you disabled this frame.

Disabling a control stops the game reacting to it while leaving it readable by script — the standard way to repurpose a key without the player's character also acting on it.

@:value({ padIndex : DEFAULT_PAD })staticinlineisJustPressed(control:Control, padIndex:Int = DEFAULT_PAD):Bool

Whether a control went down this frame.

@:value({ padIndex : DEFAULT_PAD })staticinlineisJustReleased(control:Control, padIndex:Int = DEFAULT_PAD):Bool

Whether a control came up this frame.

@:value({ padIndex : DEFAULT_PAD })staticinlineisPressed(control:Control, padIndex:Int = DEFAULT_PAD):Bool

Whether a control is currently held.

@:value({ padIndex : DEFAULT_PAD })staticinlinevalue(control:Control, padIndex:Int = DEFAULT_PAD):Float

An analogue control's value, -1 to 1.

@:value({ padIndex : DEFAULT_PAD })staticwhileHeld(control:Control, ?body:(elapsedMs:Int) ‑> Void, padIndex:Int = DEFAULT_PAD):Int

Runs body every frame while control is held, and resolves once it is released. Returns how long it was held, in milliseconds.

Useful for hold-to-act interactions:

Thread.create(() -> {
    var heldMs = Controls.whileHeld(Context, elapsed -> Ui.showHelp('Hold... $elapsed'));
    if (heldMs > 2000) forceOpen();
});