psychopy.org | Reference | Downloads | Github

Ending loop after number of keystrokes independent of stimulus presentation

Hi all,

I am currently programming an experiment in which participants have to synchronize their finger tapping with a visual metronome (a pulsating dot in the middle of the screen).

The presentation of the dot with a fixed frequency works fine.
Now I would like to let this stimulus appear and disappear until the participant has pressed a key 10 times (and thus have tried to synchronize their keystrokes with the pulsating dot 10 times).

I made a routine and set a loop around it with a high number of nReps (999), a number of repetitions which would certainly not be reached.
I included the code below to end the loop prematurely if the required number of keystrokes is reached.

Below you can find the code I tried, the flow, and key properties.

The dot is now showed only once. So, the program is waiting for a keystroke, but if I press the allowed key nothing happens (because the force end of routine option is not ticked). If I tick this box, the dot appears only if the key is pressed, which is exactly what I do not want, because the stimulus presentation and keystrokes should be independent of each other.
If I include only the code without the key component the stimulus is displayed for the amount of nReps which means the code I tried does not suffice to terminate the loop prematurely.

What I want to achieve is to keep track of the number of keystrokes and log the timing of each of them, independent of the stimulus presentation, and end the loop as soon as the required number of keystrokes is reached.

Every offered insight is much appreciated.

Best,
Benjamin

Begin Experiment

# define theseKeys
theseKeys = event.getKeys(keyList=['k'])

Each Frame

# end loop after certain amount of keystrokes
if len(theseKeys) == 10:
    trials.finished = True
    continueRoutine = False

keyProperties

This is because your code will never evaluate to “True”:

  • event.getKeys() is an instantaneous check. If you run it at the start of the experiment, it checks just once, at that point in time, which is pointless as the task hasn’t even commenced at that stage. i.e. “start experiment” code is code you use to set things up for the rest of the experiment: the subject hasn’t seen anything at that stage.
# end loop after certain amount of keystrokes 
if len(theseKeys) == 10:
  • The above code will never evaluate to True, as you only checked for the key presses once. But even then, event.getKeys() only tells you what has been pushed since it was last called (it flushes the buffer of keypresses so you don’t keep detecting the same key). So it doesn’t keep count of keypresses for you: you’ll need to do that yourself.

  • in your “each frame” code, you refer to theseKeys. Either that will be the unchanging value you set at the beginning of the experiment, or perhaps it is a variable Builder creates from your keyboard component?I don’t know, but anyway, either do your checks in code, or use the Builder keyboard competent, but don’t let them fight with each other.

Hi Michael,

I managed to end the loop prematurely with only a code component. Combining my own code together with a keyboard component resulted in a conflict between both.

Thank you for your help.
I have another question regarding the logging of reaction times for longer than 1 routine but I will open another thread for that.

Best regards,
Benjamin

1 Like