Gamepad¶
Wrapper 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:
| Name | Type | Description | Default |
|---|---|---|---|
debug
|
bool
|
If True, print debug information |
False
|
Source code in genesis_forge/gamepads/gamepad.py
dpad
property
¶
Get the current D-pad direction (e.g., 'up', 'down', 'left', 'right').
is_running = True
instance-attribute
¶
axis(index)
¶
Get the value of an axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The axis index (0-5) |
required |
Returns:
| Type | Description |
|---|---|
float
|
The axis value in range [-1.0, 1.0] for sticks, [0.0, 1.0] for triggers |
Source code in genesis_forge/gamepads/gamepad.py
buttons()
¶
Get the list of currently pressed buttons.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of button names (lowercase, e.g., 'a', 'b', 'x', 'y') |
Low-level SDL2 API¶
For advanced users who want direct access to the SDL2 event loop:
Minimal 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:
| Name | Type | Description | Default |
|---|---|---|---|
handle_key
|
Callable[[Key], None]
|
Callback function that receives Key objects for each controller event |
required |
alive
|
Event | None
|
Optional threading.Event to control the event loop. When cleared, the loop exits. |
None
|
timeout
|
int
|
Milliseconds to wait for events before checking the alive flag (default: 2000ms) |
2000
|
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()
Source code in genesis_forge/gamepads/sdl2.py
alive = alive or threading.Event()
instance-attribute
¶
handle_key = handle_key
instance-attribute
¶
timeout = timeout
instance-attribute
¶
run()
¶
Source code in genesis_forge/gamepads/sdl2.py
Gamepad input abstraction shared across backends.