Fail to terminate a loop through code widget

Hi all! I’m trying to terminate the loop in my experiment depending on which key the user presses. Ideally, the loop will terminate if the user presses key ‘j’. I put my code snippet in the first routine in the loop. Here it is:

if key_resp_3 == ‘j’:
trials.finished = True

However, this code snippet (see screenshot) doesn’t seem to terminate the loop or do anything to it. I tried adding it in all sections (eg. before routine; each frame; end routine, etc) but nothing changed. There is no error message. Any suggestions on what I should do? Thanks in advance!


if key_resp_3.keys == 'j':
     trials.finished = True

or better still

if 'j' in key_resp_3.keys: # to allow for the keys being a list
     trials.finished = True

Hello! Thanks for getting back to me! I tried your solution and it works but I now realized that I want to skip the rest of the routines in a loop after a user presses ‘j’ and goes on to the next iteration; however, trials.finished terminates the entire loop. I tried using

continueRoutine = False but it didn’t work. Any suggestions on what I should do? Also here is a screenshot of my experiment:

To be clear.

If the participant presses j in the trial routine, you would like generateHL and feedback to be skipped but you don’t want the trials loop to exit?

In that case you should put:

if 'j' in key_resp_3.keys: # to allow for the keys being a list
     continueRoutine = False

in Begin Routine of generateHL and feedback.

Thank you so much for your help! It worked :smiley: