CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pysdl2

Pure Python wrapper around SDL2 libraries for cross-platform multimedia development

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

joystick-input.mddocs/

Joystick and Game Controller Input

Complete joystick and game controller support with both raw joystick access and standardized game controller mapping. PySDL2 provides comprehensive input handling for gaming devices through SDL2's robust input subsystem.

Capabilities

Joystick Management

Core joystick functions for device enumeration and management.

def SDL_NumJoysticks() -> int:
    """
    Get the number of joysticks attached to the system.
    
    Returns:
    Number of joysticks available
    """

def SDL_JoystickNameForIndex(device_index: int) -> bytes:
    """Get the implementation-dependent name of a joystick."""

def SDL_JoystickOpen(device_index: int) -> SDL_Joystick:
    """
    Open a joystick for use.
    
    Parameters:
    - device_index: index of the joystick to open
    
    Returns:
    SDL_Joystick object or None on failure
    """

def SDL_JoystickClose(joystick: SDL_Joystick) -> None:
    """Close a joystick previously opened with SDL_JoystickOpen."""

def SDL_JoystickGetDeviceGUID(device_index: int) -> SDL_JoystickGUID:
    """Get the implementation-dependent GUID for the joystick at a given device index."""

def SDL_JoystickInstanceID(joystick: SDL_Joystick) -> int:
    """Get the instance ID of an opened joystick."""

Joystick Input Reading

Functions for reading joystick axes, buttons, and hat switches.

def SDL_JoystickNumAxes(joystick: SDL_Joystick) -> int:
    """Get the number of general axis controls on a joystick."""

def SDL_JoystickNumButtons(joystick: SDL_Joystick) -> int:
    """Get the number of buttons on a joystick."""

def SDL_JoystickNumHats(joystick: SDL_Joystick) -> int:
    """Get the number of POV hats on a joystick."""

def SDL_JoystickGetAxis(joystick: SDL_Joystick, axis: int) -> int:
    """
    Get the current state of an axis on a joystick.
    
    Parameters:
    - joystick: joystick to query
    - axis: axis index (0-based)
    
    Returns:
    Axis value ranging from -32768 to 32767
    """

def SDL_JoystickGetButton(joystick: SDL_Joystick, button: int) -> int:
    """
    Get the current state of a button on a joystick.
    
    Parameters:
    - joystick: joystick to query  
    - button: button index (0-based)
    
    Returns:
    1 if button is pressed, 0 if not pressed
    """

def SDL_JoystickGetHat(joystick: SDL_Joystick, hat: int) -> int:
    """Get the current state of a POV hat on a joystick."""

Game Controller Management

High-level game controller API with standardized button and axis mapping.

def SDL_IsGameController(joystick_index: int) -> bool:
    """Check if the given joystick is supported by the game controller interface."""

def SDL_GameControllerOpen(joystick_index: int) -> SDL_GameController:
    """
    Open a game controller for use.
    
    Parameters:
    - joystick_index: index of the device to open
    
    Returns:
    SDL_GameController object or None on failure
    """

def SDL_GameControllerClose(gamecontroller: SDL_GameController) -> None:
    """Close a game controller previously opened with SDL_GameControllerOpen."""

def SDL_GameControllerName(gamecontroller: SDL_GameController) -> bytes:
    """Get the implementation-dependent name for an opened game controller."""

def SDL_GameControllerGetJoystick(gamecontroller: SDL_GameController) -> SDL_Joystick:
    """Get the underlying joystick object used by a game controller."""

Game Controller Input Reading

Standardized input reading for game controllers with consistent button and axis naming.

def SDL_GameControllerGetAxis(gamecontroller: SDL_GameController, axis: int) -> int:
    """
    Get the current state of an axis on a game controller.
    
    Parameters:
    - gamecontroller: controller to query
    - axis: axis constant (SDL_CONTROLLER_AXIS_*)
    
    Returns:
    Axis value ranging from -32768 to 32767
    """

def SDL_GameControllerGetButton(gamecontroller: SDL_GameController, button: int) -> int:
    """
    Get the current state of a button on a game controller.
    
    Parameters:
    - gamecontroller: controller to query
    - button: button constant (SDL_CONTROLLER_BUTTON_*)
    
    Returns:
    1 if button is pressed, 0 if not pressed
    """

Constants and Enums

# Joystick hat positions
SDL_HAT_CENTERED = 0x00
SDL_HAT_UP = 0x01
SDL_HAT_RIGHT = 0x02
SDL_HAT_DOWN = 0x04
SDL_HAT_LEFT = 0x08
SDL_HAT_RIGHTUP = 0x03
SDL_HAT_RIGHTDOWN = 0x06
SDL_HAT_LEFTUP = 0x09
SDL_HAT_LEFTDOWN = 0x0c

# Game controller axes
SDL_CONTROLLER_AXIS_LEFTX = 0
SDL_CONTROLLER_AXIS_LEFTY = 1
SDL_CONTROLLER_AXIS_RIGHTX = 2
SDL_CONTROLLER_AXIS_RIGHTY = 3
SDL_CONTROLLER_AXIS_TRIGGERLEFT = 4
SDL_CONTROLLER_AXIS_TRIGGERRIGHT = 5

# Game controller buttons  
SDL_CONTROLLER_BUTTON_A = 0
SDL_CONTROLLER_BUTTON_B = 1
SDL_CONTROLLER_BUTTON_X = 2
SDL_CONTROLLER_BUTTON_Y = 3
SDL_CONTROLLER_BUTTON_BACK = 4
SDL_CONTROLLER_BUTTON_GUIDE = 5
SDL_CONTROLLER_BUTTON_START = 6
SDL_CONTROLLER_BUTTON_LEFTSTICK = 7
SDL_CONTROLLER_BUTTON_RIGHTSTICK = 8
SDL_CONTROLLER_BUTTON_LEFTSHOULDER = 9
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER = 10
SDL_CONTROLLER_BUTTON_DPAD_UP = 11
SDL_CONTROLLER_BUTTON_DPAD_DOWN = 12
SDL_CONTROLLER_BUTTON_DPAD_LEFT = 13
SDL_CONTROLLER_BUTTON_DPAD_RIGHT = 14

Usage Examples

Basic Joystick Usage

import sdl2

# Initialize SDL
sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)

# Check for joysticks
num_joysticks = sdl2.SDL_NumJoysticks()
if num_joysticks > 0:
    # Open first joystick
    joystick = sdl2.SDL_JoystickOpen(0)
    
    # Get joystick info
    name = sdl2.SDL_JoystickName(joystick)
    num_axes = sdl2.SDL_JoystickNumAxes(joystick)
    num_buttons = sdl2.SDL_JoystickNumButtons(joystick)
    
    print(f"Opened joystick: {name}")
    print(f"Axes: {num_axes}, Buttons: {num_buttons}")
    
    # Read input in game loop
    while True:
        # Update joystick state
        sdl2.SDL_JoystickUpdate()
        
        # Read first axis and button
        axis_value = sdl2.SDL_JoystickGetAxis(joystick, 0)
        button_pressed = sdl2.SDL_JoystickGetButton(joystick, 0)
        
        # Close joystick when done
        sdl2.SDL_JoystickClose(joystick)

Game Controller Usage

import sdl2

# Initialize SDL with game controller support
sdl2.SDL_Init(sdl2.SDL_INIT_GAMECONTROLLER)

# Check for game controllers
for i in range(sdl2.SDL_NumJoysticks()):
    if sdl2.SDL_IsGameController(i):
        # Open game controller
        controller = sdl2.SDL_GameControllerOpen(i)
        
        # Read standardized input
        left_stick_x = sdl2.SDL_GameControllerGetAxis(
            controller, sdl2.SDL_CONTROLLER_AXIS_LEFTX
        )
        a_button = sdl2.SDL_GameControllerGetButton(
            controller, sdl2.SDL_CONTROLLER_BUTTON_A
        )
        
        # Close controller when done
        sdl2.SDL_GameControllerClose(controller)
        break

Types

class SDL_Joystick:
    """Opaque joystick structure for raw joystick access."""

class SDL_GameController:
    """Opaque game controller structure for standardized controller input."""

class SDL_JoystickGUID:
    """Structure representing a joystick GUID."""
    data: bytes

Install with Tessl CLI

npx tessl i tessl/pypi-pysdl2

docs

audio.md

events-input.md

file-io.md

fonts-text.md

graphics-rendering.md

image-processing.md

index.md

joystick-input.md

sprites-animation.md

system-utils.md

timer.md

window-display.md

tile.json