Joystick

From XPUB & Lens-Based wiki

A simple pygame project to view joystick / gamepad data... see pygame docs

import sys, pygame
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count() > 0:
    print "creating joystick object"
    stick = pygame.joystick.Joystick(0)
#    print stick
#    print dir(stick)
    stick.init()

size = width, height = 320, 240
black = 0, 0, 0
screen = pygame.display.set_mode(size)
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
            running = False
        print event

    screen.fill(black)
    pygame.display.flip()

produces output like:

<Event(9-JoyHatMotion {'joy': 0, 'hat': 0, 'value': (1, 0)})>
<Event(11-JoyButtonUp {'joy': 0, 'button': 0})>
<Event(9-JoyHatMotion {'joy': 0, 'hat': 0, 'value': (0, 0)})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.0, 'axis': 0})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.06183050019837031, 'axis': 0})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.27832880642109442, 'axis': 1})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 4})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 5})>