Keyboard issue related to terminating a routine

I have a keyboard issue related to terminating a routine:

if catch_key.keys == 'x':
    fbstate = 1
    continueRoutine = False
else:
    fbstate = 0

‘fbstate’ is a variable I assign to nReps in a following routine. So, I want to start a routine when subjects press ‘x’. However, if subjects don’t press ‘x’, I want the routine to continue as normal and skip the following routine to which I assign ‘fbstate’. This is in the ‘Each Frame’ tab.

Any help would be greatly appreciated.

Since catch_key is probably storing all keys then it will be a list, not a string. Try

if 'x' in catch_key.keys:
    fbstate = 1
    continueRoutine = False
else:
    fbstate = 0

Hmm, I was pretty sure this was going to work, however, I got the following error:

TypeError: Cannot use ‘in’ operator to search for ‘x’ in undefined

Sorry – try this. The issue is that .keys is undefined until the first key press

if catch_key.keys:
     if 'x' in catch_key.keys:
         fbstate = 1
         continueRoutine = False
     else:
         fbstate = 0

Now it runs fine and terminates the current routine correctly, but it actually skips the following routine, as if ‘fbstate’ is always assigned a value of 0.

Please could you show your flow and the loop?

If you just want to show/hide a single routine, the best approach in the latest version is Routine Settings / Flow / skip if instead of a loop.

Unfortunately, I’m using 2020.1.3 by necessity.

Here are my keyboard settings.

image

Here’s my loop.

I’m also attempting to “animate” a stimulus by changing it at a certain point after its presentation begins:

if Catch_trials_p == '1':
    if GlobalClock.getTime() >= (dur/2):
      GabNum = Math.floor(Math.random() * 6 + 1);
      gaborPath = "resources/Gabor_adaptor_" + GabNum + ".jpg";

‘Catch_trials_p’ and ‘dur’ are columns in a condition file. Don’t know if that would interfere somehow (probably not).

If your keyboard component is only set to monitor for x then you could simplify the code by having that keyboard component force the end of the routine.

In End Routine put

fbstate = 0
if catch_key.keys:
     fbstate = 1

Please could you show the flow? I want to check that your fbcond loop doesn’t start until after the routine with the catch_key keyboard component. What is that routine called?

image

‘fbcond’ is the loop containing ‘fbstate’. Your End routine suggestion did not solve it, unfortunately (I ticked the force end of routine option).

Is catch_key in K_p_Garbor_adaptor ?

Put print('fbstate',fbstate) in End Routine just to check the value is correct

Also, I could put a print statement in Begin Routine in a code component in fb to check whether the issue is with the visibility of the routine rather than the loop.

This is the solution, provided that ‘event.getKeys()’ is specified in the ‘Begin routine’ tab. Thank you!