I hope someone can help us. I’ve written some PsychoPy code for a graduate psychology student who needs the code for her dissertation research. We chose to get everything working with simple keystrokes before adding joysticks in the final steps. The PsychoPy documentation describes how to use the joysticks, but as far as I can tell, PsychoPy code does not respond to events. It recognizes the joystick brand, number of buttons, degrees of freedom, but doesn’t respond to events.
Here’s our test code, running with Python 2.7.11, Mac OS X 10.11.1. If anyone can give us a simple solution to make the code respond to joystick events, we will be extremely grateful.
```
from psychopy import visual, core # import some libraries from PsychoPy
from psychopy import event
from psychopy import data
from psychopy.tools.filetools import fromFile
import random
import glob, sys, os
from PIL import Image
from datetime import timedelta
from psychopy.hardware import joystick
import pyglet
#===========================================================================
# Test Joystick
#===========================================================================
joystick.backend='pyglet' # must match the Window
joysticks = pyglet.input.get_joysticks()
mywin = visual.Window([800,600], monitor="testMonitor", units="deg", winType='pyglet') #create a window
print "devices: ", pyglet.input.get_devices()
print "len of pyglet joysticks = ", len(joysticks)
if joysticks:
joy = joysticks[0]
joy.open(mywin)
joy.backend = 'pyglet' #must match the Window
print "name: ", joy.device
print
text = " TESTING JOYSTICKS \n"
message = visual.TextStim(mywin, alignHoriz='center', alignVert='center', height=0.5, text=text)
message.setAutoDraw(True) # automatically draw every frame
event.clearEvents()
mywin.flip()
#
# To use a joystick, first call `open`, then in your game loop examine
# the values of `x`, `y`, and so on. These values are normalized to the
# range [-1.0, 1.0].
#
# To receive events when the value of an axis changes, attach an
# on_joyaxis_motion event handler to the joystick. The `Joystick` instance,
# axis name, and current value are passed as parameters to this event.
#
# To handle button events, you should attach on_joybutton_press and
# on_joy_button_release event handlers to the joystick. Both the `Joystick`
# instance and the index of the changed button are passed as parameters to
# these events.
while True:
core.wait(2.0)
buttons = joy.buttons
mywin.flip()
if joy.on_joybutton_press(joy,0):
print "button pressed!!"
exit()
print "button values: ", buttons
print "controller 1: x, y axes: ", joy.x, joy.y
print "controller 2: x, y axes: ", joy.z, joy.rz
print "-----"
if (joy.buttons[0]==True or joy.buttons[1]==True or event.getKeys('q')):
break
#mywin.flip()
```
The loop runs forever without any of the values changing.
Thanks.