Largely based on materials from PsychoPy Course by Jonas Lindeløv, licensed under GPL v.2
psychopy.event
module is the original PsychoPy way. Cons: runs in the same process, so might be blocking execution etcpsychopy.iohub
runs in a separate process# PsychoPy event way
from psychopy import visual
from psychopy import event
win = visual.Window()
stim = visual.TextStim(win, text="Press a bloody button")
# Keep main process busy
flip_times = []
event.clearEvents()
for frame in range(60):
stim.draw()
flip_times += [win.flip()]
# Get responses and their timing
responses = event.getKeys(timeStamped=True)
if responses:
print responses[0][1] - flip_times[0] # RT
else:
print "no responses"
win.close()
Excercise
response
# PsychoPy iohub way
# Unfortunately there is an "issue" with using iohub under IPython notebooks:
# https://github.com/psychopy/psychopy/issues/907
# which overrides sys.stdout so lets workaround
import sys
stdout_orig = sys.stdout
from psychopy import iohub
sys.stdout = stdout_orig # workaround "patch"
io = iohub.launchHubServer() # Start the server listening to events
keyboard = io.devices.keyboard
win = visual.Window()
stim = visual.TextStim(win, text="Press a bloody button")
# Keep main process busy
flip_times = []
io.clearEvents('all')
for frame in range(60):
stim.draw()
flip_times += [win.flip()]
# Get response and RT
responses = keyboard.getPresses()
# Or use one of these:
releases = keyboard.getReleases()
keys = keyboard.getKeys()
io.quit() # or .shutdown() - Stops and disconnect from iohub server
win.close()
if responses:
print responses[0].time - flip_times[0]
else:
print "no responses"
Excercise
responses
, releases
, keys
keys
is ... Make it non-...Once again -- iohub runs in the background and collects events until it is shutdown
from psychopy.iohub import launchHubServer
# Start the ioHub process. The return variable is what is used
# during the experiment to control the iohub process itself,
# as well as any running iohub devices.
io = launchHubServer()
# By default, ioHub will create Keyboard and Mouse devices and
# start monitoring for any events from these devices only.
keyboard = io.devices.keyboard
mouse = io.devices.mouse
# As a simple example, use the keyboard to have the experiment
# wait until a key is pressed.
print "Press any Key to Exit Example.... ."
keys = keyboard.waitForKeys()
print "Key press detected, exiting experiment."
# See what keys were pressed
# uncomment, gather and retrospect all the events which iohub collected by NOW
# all_events = io.getEvents()
# Shutdown the iohub
io.quit()
Questions*
from psychopy import iohub, visual, event
win = visual.Window()
textStim = visual.TextStim(win, text='press now', height=0.1, wrapWidth=100)
io = iohub.launchHubServer()
keyboard = io.devices.keyboard
while True:
# Animate and show results
textStim.ori += 0.1
textStim.draw()
win.flip()
# Get responses
event_getkeys = event.getKeys(timeStamped=True)
io_getpresses = keyboard.getPresses()
# Update text if a matching response was found
if len(event_getkeys) and len(io_getpresses):
textStim.text = """
event.getKeys time: %.3f s
iohub.getPresses time: %.3f s
io time is %i ms before event
""" %(event_getkeys[0][1], io_getpresses[0].time,
1000*(event_getkeys[0][1] - io_getpresses[0].time))
if io_getpresses[0].char == 'q':
break
win.close()
io.quit()
Excercise
from psychopy import iohub
sys.stdout = stdout_orig # workaround "patch"
io = iohub.launchHubServer()
keyboard = io.devices.keyboard
io.wait(1)
io.clearEvents('all')
print 'listening now!'
sys.stdout.flush()
# iohub wait* methods above are identical to doing this, although they do
# it in a different process than the main python session:
while True:
events = keyboard.getEvents(iohub.EventConstants.KEYBOARD_RELEASE) # listening for releases
if events: # if non-empty
break
print 'finish'
io.quit()
Excercise
Let's now wait for specific keypresses (could well be the trigger from Lumina emulated as a keyboard)
# Initiate keyboard
from psychopy import iohub
sys.stdout = stdout_orig # workaround "patch"
io = iohub.launchHubServer()
keyboard = io.devices.keyboard
# WAITING FOR KEYBOARD
print 'Listening now...'
keyboard.waitForKeys(chars=['@', '!'])
print 'key event!'
print keyboard.state # print keyboard state
sys.stdout.flush() # so we could see right away
keyboard.waitForReleases(keys=['f'], mods=['lalt', 'ralt'])
print 'key release!'
print keyboard.state
sys.stdout.flush() # so we could see right away
keyboard.waitForPresses(chars=['F', '@', '!'])
print 'key press!'
print keyboard.state
sys.stdout.flush() # so we could see right away
io.quit()
Excercise
Write code that waits until the subject has written 'Dawg!'
Hint: (D, a, w, g, !).
Start a psychopy Window with a TextStim that updates to show this in text, as if one was typing in a text field. (Extra: Maybe the text should turn green upon success?)
Extra: use keyboard.state to determine if the subject is currently holding down a, s, d, f, and g and exit the loop if then
Hint: dict.keys() and set(lista) == set(listb)