The Second Routine detects a key press from the First Routine

Hello,
I created a simplistic version of the issue I’m having. The layout looks like this:
image


Project download:
skip.psyexp (17.9 KB)


The participant receives instructions on the first Routine (r1). He presses the ‘space’ key to continue to the following Routine, r2. The routine skip is done with the keyboard component by forcing the end of the Routine with the allowed space key:

The participants must answer a question in the second Routine (r2). On the second routine, the following Routine is triggered if he presses ‘p’ or ‘q’ (r3). If he presses the ‘space’ key, the current Routine (r2) is restarted and shown again. The trials loop can be repeated 999 times, which means the participant can replay the Routine as many times as he wants.


This is the code component I am using to detect the current key that was pressed:

keys = event.getKeys()

if len(keys)>0:
    for thisKey in keys:
        current_key = ord(thisKey[0])
        
        # if 'q' or 'p' is pressed
        if current_key == 112 or current_key == 113:
           trials.finished = True
           continueRoutine = False   

        # if 'space' is pressed
        elif current_key == 115:
            trials.finished = False
            continueRoutine = False

I am using the ord() function because the default keyboard might not always be in English.


The issue I am having:
After the participant presses the ‘space’ key on the first Routine (‘r1’), the space key is triggered again at the start of the second Routine (‘r2’). It means that the second Routine starts on the second iteration instead of the first one.

In the example I provide, you can see that trials.thisN starts on 1 instead of 0.

How do I avoid it? I’m not sure what causes it and how to prevent this kind of behavior.

Thanks,
Chen

I removed the space key from the code:

keys = event.getKeys()

if len(keys)>0:
    for thisKey in keys:
        current_key = ord(thisKey[0])
        
        # if 'q' or 'p' is pressed
        if current_key == 112 or current_key == 113:
           trials.finished = True
           continueRoutine = False   

And added a keyboard component that detects the space key to end the routine.
Works perfectly.

1 Like