Issue with Key Reset Across Routines in PsychoPy Experiment

Hello everyone,

I’m working on an experiment in PsychoPy where the task is to avoid repeating the previous key press. If the participant repeats the key, the response is marked as incorrect, but it’s considered correct if the key is different from the previous one. Here’s the code I used for the first routine:

Begin Experiment

oldKey = ''  
score = 0    

Each frame

if key_resp_3.keys:
    currentKey = key_resp_3.keys[-1] 

    # Compare with the previous key
    if currentKey != oldKey:
        feedback_text.text = "Correcto"  # Positive feedback
        oldKey = currentKey  # Update the previous key
    else:
        feedback_text.text = "Incorrecto"  # Negative feedback

End Routine

score = 0
if oldKey != key_resp_3.keys[-1]:
    score = 1

thisExp.addData('Score', score)  # Save the score in the data

I replicated this code across the other routines, but in each new routine, the first key press (which should technically be correct regardless of the key) is marked as incorrect if it matches the last key pressed in the previous routine.

I’ve tried resetting the previous key at the beginning of each routine using the following code:

Begin Routine

key_resp_4.clearEvents(eventType='keyboard')
oldKey = '' 

but it’s not working as expected, it starts to mark every key as correct. Does anyone know how I can fix this? Thanks!

How does the routine end?

In the first frame after a key is pressed currentKey != oldKey and you should get correct feedback. However, on the following frame currectKey now equals oldKey and the feedback will switch. This will still be true at End Routine (score = 0)

What I do is check if len(key_resp_3.keys) > nKeys and if so I know a new key has been pressed. However, I’m not sure how this would work with your End Routine code which implies that you only have a single key press per routine.

1 Like

Thanks for your help! I’m a bit confused about what you mean by “only have a single key press per routine.”

In my setup, each routine consists of a loop with 36 attempts, and the participant can press a key multiple times throughout that loop. After each key press, they receive feedback before moving on to the next attempt.

Does this affect how I should be evaluating the key presses?

Thanks!

Can they press the key more than once for each attempt? If so, what would that mean to you?

No, they only can press it once per attempt

In that case do you need the Each Frame code? Is feedback_text in the same routine or a feedback routine?

The feedback is in a Feedback routine, and I have the code in ‘Each Frame’ because otherwise, the feedback doesn’t appear after the key press. With each attempt, they see an image, and after the key press, the feedback follows

The code component should be at the top of the routine. If the code component is in the feedback routine it should be on the Begin Routine tab. Alternatively, it could be in the End Routine tab of the trial routine.

1 Like

Hello. I’ve been trying to get it to work. My code component is in the trial routine, but when I move the code from Each Frame to End Routine, any key starts to be marked as correct, regardless of whether it is repeated.
Thanks for your help