Teek API Documentation

An SDL2 GameController for polling buttons, analog sticks, and triggers.

Gamepad wraps SDL2’s GameController API, which automatically maps physical controls to an Xbox-style layout. This is higher-level than the raw Joystick API and works with most modern controllers out of the box (Xbox, PlayStation, Switch Pro, etc.).

Inherits: Object

Class Methods

Class methods (C-defined)

all

Open and return all connected gamepads.

Returns Array<Gamepad>

attach_virtual

Create a virtual gamepad device for testing without hardware.

Returns Integer — device index (pass to .open)

@raise RuntimeError if a virtual device is already attached

axes

List of valid axis symbols.

Returns Array<Symbol>

buttons

List of valid button symbols.

Returns Array<Symbol>

count

Returns the number of connected gamepads recognized by SDL2.

Returns Integer

detach_virtual

Remove the virtual gamepad created by .attach_virtual.

Returns nil

first

Open the first available gamepad.

Returns Gamepad, nil — nil if no gamepads are connected

init_subsystem

Initialize the SDL2 gamepad subsystem. Called automatically by other methods, but can be called early for hot-plug detection.

Returns nil

on_added({|device_index| ... })

Register a callback for when a new gamepad is connected.

Returns nil

@yieldparam device_index Integer device index (pass to .open)

on_axis({|instance_id, axis, value| ... })

Register a callback for axis motion events.

Returns nil

@yieldparam instance_id Integer SDL joystick instance ID

@yieldparam axis Symbol axis name (e.g. :left_x, :trigger_left)

@yieldparam value Integer axis value (-32768..32767 for sticks, 0..32767 for triggers)

on_button({|instance_id, button, pressed| ... })

Register a callback for button press/release events.

Returns nil

@yieldparam instance_id Integer SDL joystick instance ID

@yieldparam button Symbol button name (e.g. :a, :dpad_up)

@yieldparam pressed Boolean true if pressed, false if released

on_removed({|instance_id| ... })

Register a callback for when a gamepad is disconnected.

Returns nil

@yieldparam instance_id Integer SDL joystick instance ID

open(index)

Open the gamepad at the given device index.

Parameters
  • index Integer — device index (0-based)

Returns Gamepad

@raise ArgumentError if index is negative

@raise RuntimeError if index is out of range or device cannot be opened

poll_events

Pump SDL events and dispatch gamepad events to registered callbacks. Call this periodically (e.g. every 16–50ms) for responsive input.

Note: on macOS, SDL_PollEvent pumps the Cocoa run loop, which can steal events from other UI toolkits (e.g. Tk). Use .update_state instead when you only need fresh controller state.

Returns Integer — number of events processed

shutdown_subsystem

Shut down the gamepad subsystem. Existing Gamepad objects become invalid.

Returns nil

update_state

Refresh controller state without pumping the platform event loop. Calls SDL_GameControllerUpdateSDL_JoystickUpdate directly, bypassing SDL_PumpEvents and the Cocoa run loop on macOS.

After calling this, #button? and #axis return updated values. Event callbacks (on_button, on_axis, etc.) will NOT fire — use .poll_events when you need callbacks.

Returns nil

virtual_device_index

Device index of the virtual gamepad.

Returns Integer, nil — nil if no virtual device is attached

apply_dead_zone(value, threshold DEAD_ZONE)

Apply a dead zone to a stick axis value. Returns 0 if the value is within the dead zone, otherwise returns the original value.

Parameters
  • value Integer — raw axis value (-32768..32767)
  • threshold Integer — dead zone threshold

Returns Integer

Instance Methods

Instance methods (C-defined)

attached?

Whether the controller is still physically connected.

Returns Boolean

axis(axis)

Current value of an analog axis.

Parameters
  • axis Symbol — one of :left_x, :left_y, :right_x, :right_y, :trigger_left, :trigger_right

Returns Integer — -32768..32767 for sticks, 0..32767 for triggers

button?(button)

Whether the given button is currently pressed.

Parameters
  • button Symbol — one of :a, :b, :x, :y, :back, :guide, :start, :left_stick, :right_stick, :left_shoulder, :right_shoulder, :dpad_up, :dpad_down, :dpad_left, :dpad_right

Returns Boolean

close

Close the controller. Further method calls will raise.

Returns nil

closed?

Whether the controller has been closed.

Returns Boolean

instance_id

SDL joystick instance ID. Useful for matching with event callbacks.

Returns Integer

name

Human-readable name of the controller (e.g. “Xbox One Controller”).

Returns String

rumble(low_freq, high_freq, duration_ms)

Trigger haptic feedback (rumble).

Parameters
  • low_freq Integer — low-frequency motor intensity (0–65535)
  • high_freq Integer — high-frequency motor intensity (0–65535)
  • duration_ms Integer — duration in milliseconds

Returns Boolean — true on success

set_virtual_axis(axis, value)

Set the value of an axis on a virtual gamepad.

Parameters
  • axis Symbol — axis name
  • value Integer — axis value (-32768..32767 for sticks, 0..32767 for triggers)

Returns nil

set_virtual_button(button, pressed)

Set the state of a button on a virtual gamepad.

Parameters
  • button Symbol — button name
  • pressed Boolean — true for pressed, false for released

Returns nil