Pygame: Analog trigger initial value is not the neutral trigger position

Viewed 279

I'm developing an application that uses pygame to read analog trigger values from an XBox 360-Style gamepad. This application relies on the triggers being initialized to the neutral position, or else it results in undefined behaviour.

The problem: analog triggers are interpreted as axes in pygame and those get initialized to 0 until they receive the first user input. But the neutral position (triggers are not pressed) of the triggers is mapped to -1.0.

So, until I somehow press both triggers, the signal I receive is “half-pressed” for both triggers, although I'm not pressing anything.

Is there a way to specify the initial state of the Joystick object? I already tried to define a custom JOYAXISMOTION event which didn't help.

Minimal code example:

import pygame
import time

pygame.joystick.init()

if pygame.joystick.get_count() <= 0:
    print("No joystick or gamepad found")
    exit()

device = pygame.joystick.Joystick(0)
device.init()

print(device.get_numaxes())

while True:
    pygame.event.get()

    # here, it prints 0.0, 0.0 when not touching the triggers, 
    # until I press them
    print("Left trigger: {}\t Right trigger: {}".format(device.get_axis(2),  # left trigger
                                                        device.get_axis(5)))  # right trigger

    time.sleep(0.1)
0 Answers
Related