Gamepad#
- class genesis_forge.gamepads.Gamepad(debug: bool = False)[source]#
Bases:
objectWrapper around SDL2 ControllerEventLoop that provides a polling-based interface.
This maintains axis and button state that can be queried at any time, similar to the old HID-based Gamepad API.
Example:
>>> gamepad = Gamepad() >>> gamepad.axis(0) # Get left stick X axis 0.0 >>> gamepad.buttons() # Get list of pressed buttons ['a', 'b']
Initialize the SDL2 gamepad wrapper.
- Parameters:
debug – If True, print debug information
- axis(index: int) float[source]#
Get the value of an axis.
- Parameters:
index – The axis index (0-5)
- Returns:
The axis value in range [-1.0, 1.0] for sticks, [0.0, 1.0] for triggers
Low-level SDL2 API#
For advanced users who want direct access to the SDL2 event loop:
- class genesis_forge.gamepads.ControllerEventLoop(handle_key: Callable[[Key], None], alive: threading.Event | None = None, timeout: int = 2000)[source]#
Bases:
objectMinimal SDL2 controller event loop for genesis-forge.
This class runs in a background thread and processes SDL2 controller events, converting them to Key objects and passing them to a callback function.
- Parameters:
handle_key – Callback function that receives Key objects for each controller event
alive – Optional threading.Event to control the event loop. When cleared, the loop exits.
timeout – Milliseconds to wait for events before checking the alive flag (default: 2000ms)
Example:
def on_key(key: Key): print(f"Key event: {key}") loop = ControllerEventLoop(handle_key=on_key) thread = threading.Thread(target=loop.run, daemon=True) thread.start() # Later, to stop: loop.stop()