Modules

Gamepad

moduleSource

Available on macOS, Linux, Windows

#import "Gamepad";

The state of the first connected game controller, read through SDL.

Call update_gamepad once per frame, then look at the global gamepad: buttons is indexed by Gamepad_Code, and the sticks and triggers are floats. With gamepad.generate_events set, button changes also arrive as events in the Input module.

Example↗
#import "Gamepad";

while true {
    update_gamepad();
    if !gamepad.connected continue;
    if gamepad.buttons[Gamepad_Code.A] & .START print("jump\n");
    print("stick: %\n", gamepad.left_stick);
}

This one is only checked to compile, not run.

Procedures#

SDL#

init_gamepadsince 0.31.0Source
()

Starts the controller backend. update_gamepad calls it on first use.

()

Reads the controller and refreshes gamepad. Call once per frame. Buttons that changed since the last call carry START or END.

Evdev#

Foreign#

Types#

:: enum

The buttons of a controller. Index Gamepad.buttons with it. A, B, X and Y are the face buttons by their Xbox names; sticks and triggers also appear here as buttons, once they pass their thresholds.

DPAD_UP
DPAD_DOWN
DPAD_LEFT
DPAD_RIGHT
START
BACK
LEFT_SHOULDER
RIGHT_SHOULDER
LEFT_TRIGGER
RIGHT_TRIGGER
FACE_DOWN
FACE_RIGHT
FACE_LEFT
FACE_UP
LEFT_STICK_UP
LEFT_STICK_DOWN
LEFT_STICK_LEFT
LEFT_STICK_RIGHT
RIGHT_STICK_UP
RIGHT_STICK_DOWN
RIGHT_STICK_LEFT
RIGHT_STICK_RIGHT
LEFT_STICK_CLICK
RIGHT_STICK_CLICK
A
B
X
Y
assert(Gamepad_Code.A == .FACE_DOWN);
assert(cast(int) Gamepad_Code.DPAD_UP == 0);
↗
:: struct

The current state of a controller. connected says whether there is one. The sticks run from -1 to 1 on each axis and the triggers from 0 to 1. A stick or trigger past its *_threshold also counts as a button press.

connected: bool
buttons: [] Key_Current_State
left_stick: Math.Vector2
right_stick: Math.Vector2
left_trigger: float
right_trigger: float
generate_events: bool
generate_left_stick_events: bool
vendor_id: u16
product_id: u16
left_stick_threshold: := 0.5
right_stick_threshold: := 0.5
left_trigger_threshold: := 0.3
right_trigger_threshold: := 0.3
pad: Gamepad;
assert(!pad.connected);
assert(pad.left_stick_threshold == 0.5);
↗
libevdevstructforeignSource
:: struct

Regenerate with tools/generators/window_render_evdev.py and the source-built repository formatter.