# joystick (pyglet and pygame)¶

AT THE MOMENT JOYSTICK DOES NOT APPEAR TO WORK UNDER PYGLET. We need someone motivated and capable to go and get this right (problem is with event polling under pyglet)

Control joysticks and gamepads from within PsychoPy.

You do need a window (and you need to be flipping it) for the joystick to be updated.

Known issues:
• currently under pyglet the joystick axes initialise to a value of zero and stay like this until the first time that axis moves

• currently pygame (1.9.1) spits out lots of debug messages about the joystick and these can’t be turned off :-/

Typical usage:

from psychopy.hardware import joystick
from psychopy import visual

joystick.backend='pyglet'  # must match the Window
win = visual.Window([400,400], winType='pyglet')

nJoys = joystick.getNumJoysticks()  # to check if we have any
id = 0
joy = joystick.Joystick(id)  # id must be <= nJoys - 1

nAxes = joy.getNumAxes()  # for interest
while True:  # while presenting stimuli
joy.getX()
# ...
win.flip()  # flipping implicitly updates the joystick info

class psychopy.hardware.joystick.XboxController(id, *args, **kwargs)[source]

Joystick template class for the XBox 360 controller.

Usage:

xbctrl = XboxController(0) # joystick ID y_btn_state = xbctrl.y # get the state of the ‘Y’ button

An object to control a multi-axis joystick or gamepad.

Known issues

Currently under pyglet backends the axis values initialise to zero rather than reading the current true value. This gets fixed on the first change to each axis.

_clip_range(val)[source]

Clip the range of a value between -1.0 and +1.0. Needed for joystick axes.

Parameters

val

Returns

get_a()[source]

Get the ‘A’ button state.

Returns

bool, True if pressed down

get_b()[source]

Get the ‘B’ button state.

Returns

bool, True if pressed down

get_back()[source]

Get ‘back’ button state (button to the right of the left joystick).

Returns

bool, True if pressed down

get_hat_axis()[source]

Get the states of the hat (sometimes called the ‘directional pad’). The hat can only indicate direction but not displacement.

This function reports hat values in the same way as a joystick so it may be used interchangeably with existing analog joystick code.

Returns a tuple (X,Y) indicating which direction the hat is pressed between -1.0 and +1.0. Positive values indicate presses in the right or up direction.

Returns

tuple, zero centered X, Y values.

get_left_shoulder()[source]

Get left ‘shoulder’ trigger state.

Returns

bool, True if pressed down

get_left_thumbstick()[source]

Get the state of the left joystick button; activated by pressing down on the stick.

Returns

bool, True if pressed down

get_left_thumbstick_axis()[source]

Get the axis displacement values of the left thumbstick.

Returns a tuple (X,Y) indicating thumbstick displacement between -1.0 and +1.0. Positive values indicate the stick is displaced right or up.

Returns

tuple, zero centered X, Y values.

get_named_buttons(button_names)[source]

Get the states of multiple buttons using names. A list of button states is returned for each string in list ‘names’.

Parameters

button_names – tuple or list of button names

Returns

get_right_shoulder()[source]

Get right ‘shoulder’ trigger state.

Returns

bool, True if pressed down

get_right_thumbstick()[source]

Get the state of the right joystick button; activated by pressing down on the stick.

Returns

bool, True if pressed down

get_right_thumbstick_axis()[source]

Get the axis displacement values of the right thumbstick.

Returns a tuple (X,Y) indicating thumbstick displacement between -1.0 and +1.0. Positive values indicate the stick is displaced right or up.

Returns

tuple, zero centered X, Y values.

get_start()[source]

Get ‘start’ button state (button to the left of the ‘X’ button).

Returns

bool, True if pressed down

get_trigger_axis()[source]

Get the axis displacement values of both index triggers.

Returns a tuple (L,R) indicating index trigger displacement between -1.0 and +1.0. Values increase from -1.0 to 1.0 the further a trigger is pushed.

Returns

tuple, zero centered L, R values.

get_x()[source]

Get the ‘X’ button state.

Returns

bool, True if pressed down

get_y()[source]

Get the ‘Y’ button state.

Returns

bool, True if pressed down

psychopy.hardware.joystick.getNumJoysticks()[source]

Return a count of the number of joysticks available.

class psychopy.hardware.joystick.Joystick(id)[source]

An object to control a multi-axis joystick or gamepad.

Known issues

Currently under pyglet backends the axis values initialise to zero rather than reading the current true value. This gets fixed on the first change to each axis.

getAllAxes()[source]

Get a list of all current axis values.

getAllButtons()[source]

Get the state of all buttons as a list.

getAllHats()[source]

Get the current values of all available hats as a list of tuples.

Each value is a tuple (x, y) where x and y can be -1, 0, +1

getAxis(axisId)[source]

Get the value of an axis by an integer id.

(from 0 to number of axes - 1)

getButton(buttonId)[source]

Get the state of a given button.

buttonId should be a value from 0 to the number of buttons-1

getHat(hatId=0)[source]

Get the position of a particular hat.

The position returned is an (x, y) tuple where x and y can be -1, 0 or +1

getName()[source]

Return the manufacturer-defined name describing the device.

getNumAxes()[source]

Return the number of joystick axes found.

getNumButtons()[source]

Return the number of digital buttons on the device.

getNumHats()[source]

Get the number of hats on this joystick.

The GLFW backend makes no distinction between hats and buttons. Calling ‘getNumHats()’ will return 0.

getX()[source]

Return the X axis value (equivalent to joystick.getAxis(0)).

getY()[source]

Return the Y axis value (equivalent to joystick.getAxis(1)).

getZ()[source]

Return the Z axis value (equivalent to joystick.getAxis(2)).