Reading joystick values with Python

Markus Blechschmidt picture Markus Blechschmidt · Oct 6, 2013 · Viewed 18k times · Source

I want to read the values of an Logitech Logitech Extreme 3D Pro with a Raspberry Pi. I'm using the pygame library

The script:

import pygame
import sys
import time

pygame.joystick.init()

print pygame.joystick.get_count()

_joystick = pygame.joystick.Joystick(0)
_joystick.init()
print _joystick.get_init()
print _joystick.get_id()
print _joystick.get_name()
print _joystick.get_numaxes()
print _joystick.get_numballs()
print _joystick.get_numbuttons()
print _joystick.get_numhats()
print _joystick.get_axis(0)

The ouput:

1
1
0
Logitech Logitech Extreme 3D Pro
4
0
12
SDL_JoystickNumHats value:1:
1
SDL_JoystickGetAxis value:0:
0.0

There are 4 axes and I turned all of them.

I can't find the problem. I already tried using other axes.

Thanks for help.

Answer

user4092994 picture user4092994 · Sep 30, 2014

I ran into the same problem. You have to write pygame.event.get() in order to read information from the joystick. Otherwise it never updates.