In my experiment, I have 10 trials, and at each trial, the participant has to press a key ("space") asap after seeing a change of colour in the animation. As feedback that confirms the key pressing, I'd like to move to the next trial (move to the next loop iteration) after the key was pressed. I tried to implement the idea in my code with break
and continue
, but it doesn't work:
for i in range(nTrials):
# Start with fixation cross
fixation.draw()
win.flip()
core.wait(2)
# Play the video for 200 frames
for Nframes in range(200):
optic_flow_movie.draw()
fixation.draw()
win.flip()
# Get Participants responses
keys = psychopy.event.getKeys(keyList=["space"],timeStamped=clock)
if (keys[0][0] == 'space') is True:
break
else:
continue
# Take only the timestamps from the variable key and store it in the variable RTs
RTs = [sublist[1:2] for sublist in keys] # This stores only the timestamps
RTs = [item for sublist in RTs for item in sublist] # This converts from list of lists to a flat list
Many thanks for your help !
It's not entirely clear what the structure of your trial is, but if you want to monitor responses during the animation, then the call to event.getKeys()
needs to be embedded within the drawing loop (i.e. within for Nframes in range(200):
). That way, you are checking for a keypress on every screen refresh, so the animation can be halted in realtime.
At the moment, the code shows the entire animation and only then checks the keyboard (but just once per trial). Regardless of what happens there, the next trial will begin, as that is the last code in the main trial loop (i.e. for i in range(nTrials):
.
Lastly, the event
module is deprecated for keyboard responses. You should really shift to the newer Keyboard
class for much better performance:
https://www.psychopy.org/api/hardware/keyboard.html
https://discourse.psychopy.org/t/3-ways-to-get-keyboard-input-which-is-best/11184/3