End the routine when certain criterion is met

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): win10
PsychoPy version (e.g. 1.84.x): 2020.1.3 builder
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:

Psychopy builder offers the option to end the routine when a key is pressed. Can I add another criterion to it? Say when the space key is pressed, AND there is something stored in a variable (FillerTaskQ), then the routine can be ended. I would like to make sure these questions aren’t skipped.

Thanks in advance for any help!

Hi There,

Without a bit more context, I am not quite sure what the code component in the screenshot here refers to. But, I can say that you would want to ensure your keyboard response had ‘Force End Routine’ deselected (so that the trial doesn’t end immediately upon a keypress). Then you would want a component something like

if key_resp.keys and myVar.property:
    continueRoutine = False

Where ‘key_resp’ is the name of your keyboard component, myVar is the name of the vairable and ‘property’ is the attribute you are trying to access in that variable (whatever you need that is stored).

Hope this helps,
Becca

Hi Becca,

Thanks so much for your help! In my case the code is
if QSpace.keys and len(textFillQ) != 0:
continueRoutine = False
so I can make sure there are something stored in var “textFillQ” before it can proceed.
(QSpace is the keyboard component allows “space” keys and textFillQ is the variable that collects response allows “a” or “b” keys)

Now if “space” is pressed first, the experiment won’t proceed. But as soon as “a” or “b” is pressed, the routine immediately stops without another “space” press. Is their a way to revise the code so that I will need to press the “space” again after answering “a” or “b”? Thank you!

Hi There,

OK so it looks like you have a single keyboard component listening for 3 keys (‘space’, ‘a’ and ‘b’). In that case you want to check that the last key pressed was space to do that you can get the last key press using something like:

if QSpace.keys:
    if QSpace.keys[-1]=='space' and len(textFillQ)>0:

Hope this helps! :slight_smile:

Becca

PS. edited because we first need to check a key has been pressed (i.e. QSpace.keys) and THEN we check that the last key pressed was the space key (i.e. (QSpace.keys[-1]==‘space’)

Actually the keyboard components are different for “space” and “a”, “b”. QSpace is only for “space” and the custom code I attached previously in the thread has the “kbFiller1” to collect “a” “b” response.
I don’t know why it looks like my psychopy mixes up different keyboard component. Previously when I try keyboardcomponent.stop(), psychopy stops collecting input from all my other keyboard components. Do you have any idea why it happened?

But I did revise my code so it is working now. I deleted QSpace and let kbFiller1 take over all the work.

keysQ = kbFiller1.getKeys(['a','b','backspace','space','return'], waitRelease = False)

for thisKey in keysQ: 
    if len(textFillQ) == 0: 
        if thisKey in ['a','b']:
            textFillQ += thisKey.name
    elif len(textFillQ) > 0:
        if thisKey == 'backspace':
            textFillQ = textFillQ[:-1]
        elif thisKey == "space" or thisKey == "return":
            continueRoutine = False

textFeedbackQ.setText(textFillQ)

Thanks again for your patient help! It is really helpful to a complete novice like me :slight_smile:

Brad