Teek::SDL2::Gamepad Class
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)
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
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
indexInteger— 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_GameControllerUpdate → SDL_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
valueInteger— raw axis value (-32768..32767)thresholdInteger— dead zone threshold
Returns Integer
Instance Methods ↑
Instance methods (C-defined)
axis(axis)
Current value of an analog axis.
Parameters
axisSymbol— 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
buttonSymbol— 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
rumble(low_freq, high_freq, duration_ms)
Trigger haptic feedback (rumble).
Parameters
low_freqInteger— low-frequency motor intensity (0–65535)high_freqInteger— high-frequency motor intensity (0–65535)duration_msInteger— duration in milliseconds
Returns Boolean — true on success
set_virtual_axis(axis, value)
Set the value of an axis on a virtual gamepad.
Parameters
axisSymbol— axis namevalueInteger— 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
buttonSymbol— button namepressedBoolean— true for pressed, false for released
Returns nil