17 Gamepads
This section covers gamepad (game controller) support with standardized button and axis names. For lower-level joystick access, see Joysticks.
17.1 Detection
procedure
(has-gamepad?) → boolean?
procedure
(for ([id (get-gamepads)]) (printf "Gamepad: ~a~n" (get-gamepad-name-for-id id)))
procedure
procedure
(is-gamepad? instance-id) → boolean?
instance-id : exact-nonnegative-integer?
17.2 Opening and Closing
procedure
(open-gamepad instance-id) → gamepad?
instance-id : exact-nonnegative-integer?
The gamepad is registered with the current custodian for automatic cleanup.
(define ids (get-gamepads)) (when (pair? ids) (define gp (open-gamepad (car ids))) (printf "Opened: ~a~n" (gamepad-name gp)))
procedure
(gamepad-connected? gp) → boolean?
gp : gamepad?
procedure
(gamepad-destroy! gp) → void?
gp : gamepad?
Note: Gamepads are automatically closed when their custodian shuts down.
17.3 Gamepad Information
procedure
(gamepad-name gp) → (or/c string? #f)
gp : gamepad?
procedure
(gamepad-path gp) → (or/c string? #f)
gp : gamepad?
procedure
gp : gamepad?
procedure
(gamepad-type gp) → symbol?
gp : gamepad?
Values: 'unknown, 'standard, 'xbox360, 'xboxone, 'ps3, 'ps4, 'ps5, 'switch-pro, 'switch-joycon-left, 'switch-joycon-right, 'switch-joycon-pair.
procedure
(gamepad-real-type gp) → symbol?
gp : gamepad?
procedure
gp : gamepad?
procedure
gp : gamepad?
procedure
(gamepad-serial gp) → (or/c string? #f)
gp : gamepad?
17.3.1 Info by ID (before opening)
procedure
(get-gamepad-name-for-id instance-id) → (or/c string? #f)
instance-id : exact-nonnegative-integer?
procedure
(get-gamepad-type-for-id instance-id) → symbol?
instance-id : exact-nonnegative-integer?
17.4 Buttons
procedure
(gamepad-button gp button) → boolean?
gp : gamepad? button : (or/c symbol? exact-nonnegative-integer?)
'south (A on Xbox, Cross on PlayStation)
'east (B on Xbox, Circle on PlayStation)
'west (X on Xbox, Square on PlayStation)
'north (Y on Xbox, Triangle on PlayStation)
'back, 'start, 'guide
'left-stick, 'right-stick (L3/R3)
'left-shoulder, 'right-shoulder (LB/RB or L1/R1)
'dpad-up, 'dpad-down, 'dpad-left, 'dpad-right
Xbox: 'a, 'b, 'x, 'y
PlayStation: 'cross, 'circle, 'square, 'triangle
Shoulder: 'lb, 'rb, 'l1, 'r1
(when (gamepad-button gp 'south) (player-jump!))
procedure
(gamepad-has-button? gp button) → boolean?
gp : gamepad? button : (or/c symbol? exact-nonnegative-integer?)
17.5 Axes
procedure
(gamepad-axis gp axis) → exact-integer?
gp : gamepad? axis : (or/c symbol? exact-nonnegative-integer?)
For sticks: -32768 to 32767 (centered at 0). For triggers: 0 to 32767.
'left-x, 'left-y —
Left stick 'right-x, 'right-y —
Right stick 'left-trigger, 'right-trigger —
Triggers (LT/RT or L2/R2)
(define lx (gamepad-axis gp 'left-x)) (define ly (gamepad-axis gp 'left-y)) (move-player! (/ lx 32768.0) (/ ly 32768.0))
procedure
(gamepad-has-axis? gp axis) → boolean?
gp : gamepad? axis : (or/c symbol? exact-nonnegative-integer?)
17.6 Button Labels
For displaying button prompts that match the controller type.
procedure
(gamepad-button-label gp button) → symbol?
gp : gamepad? button : (or/c symbol? exact-nonnegative-integer?)
Returns symbols like 'a, 'b, 'x, 'y for Xbox, or 'cross, 'circle, 'square, 'triangle for PlayStation.
procedure
(gamepad-button-label-for-type type button) → symbol?
type : symbol? button : (or/c symbol? exact-nonnegative-integer?)
17.7 Player Index
procedure
gp : gamepad?
procedure
(set-gamepad-player-index! gp index) → void?
gp : gamepad? index : exact-integer?
17.8 Rumble
procedure
(gamepad-rumble! gp low high [duration-ms]) → boolean?
gp : gamepad? low : exact-nonnegative-integer? high : exact-nonnegative-integer? duration-ms : exact-nonnegative-integer? = 0
low and high are motor intensities (0-65535). duration-ms is the duration in milliseconds (0 = infinite).
;; Strong rumble for 200ms (gamepad-rumble! gp 65535 65535 200)
procedure
(gamepad-rumble-triggers! gp left right [ duration-ms]) → boolean? gp : gamepad? left : exact-nonnegative-integer? right : exact-nonnegative-integer? duration-ms : exact-nonnegative-integer? = 0
17.9 LED
17.10 Power
procedure
(gamepad-power-info gp) →
symbol? exact-integer? gp : gamepad?
Power states: 'unknown, 'on-battery, 'no-battery, 'charging, 'charged, 'error.
Percentage is 0-100, or -1 if unknown.
procedure
(gamepad-connection-state gp) → symbol?
gp : gamepad?
Values: 'unknown, 'wired, 'wireless, 'invalid.
17.11 Type Conversions
procedure
sym : symbol?
procedure
(button->symbol btn) → symbol?
btn : exact-nonnegative-integer?
procedure
sym : symbol?
procedure
(axis->symbol axis) → symbol?
axis : exact-nonnegative-integer?
procedure
(gamepad-type->symbol type) → symbol?
type : exact-nonnegative-integer?