littlezx
littlezx

Reputation: 11

PyGame code doesn't execute properly when event is executed

I am trying to make the most simple pythong code that will respond when a button is pressed on a joystick. I used code from several different examples and I still cannot get it to work. The following code will not dispatch the event when I press the trigger (or any button for that matter)

import pygame

joy = []

def handleJoyEvent(e):
     if e.type == pygame.JOYBUTTONDOWN:
        str = "Button: %d" % (e.dict['button'])
        if (e.dict['button'] == 0):
            print ("Pressed!\n")
    else:
        pass

def joystickControl():
    while True:
        e = pygame.event.wait()
        if (e.type == pygame.JOYBUTTONDOWN):
            handleJoyEvent(e)

# main method
def main():
    pygame.joystick.init()
    pygame.display.init()
    for i in range(pygame.joystick.get_count()):
        myjoy = pygame.joystick.Joystick(i)
        myjoy.init()
        joy.append(myjoy)

    # run joystick listener loop
    joystickControl()

# allow use as a module or standalone script
if __name__ == "__main__":
    main()

Upvotes: 1

Views: 416

Answers (1)

Krendil
Krendil

Reputation: 113

I assume you've tried leaving off the if and just printing str? Your joystick might also not be working properly. Does it work in other programs?

If you are using linux you might need to install a joystick driver. For Windows, check the device manager.

Upvotes: 1

Related Questions