End Routine after two key presses and store all key presses

OS (e.g. Win10): Mac OS v12.5.1
PsychoPy version : v2022.2.4
Standard Standalone? (y/n) Y
What are you trying to achieve?:
I am coding for an experiment where I am hoping to end routine after two key presses and to store the key presses as well as the total time spend for the two key presses.

I have tried the following code by @Oli

#beginRoutine Tab
thisResp = [] #make an empty list

#every Frame
keys = event.getKeys()

if keys: # if at least one key exists in the list,
    if 'escape' in keys: # now need to check for the quit key ourselves

     else: #add this frame keys to response list
        if len(thisResp) == 2:
          contineRoutine = False #end routine.

The code does allow me to end the routine at 2 key presses but it doesn’t store any of the data.

The data file looks like above.
So, I am wondering how I should code to store the key presses and the reaction time.

It looks like the indentation has gone funny when you copied the text to discourse - how the last 4 lines are indented is important for how they execute. Could you share a screenshot of it in the code component?

1 Like

Thank you so much for your help. Please see below for the screenshot of the code component. I would appreciate if you happened to have any insight. Thank you so much.

Very strange that it allows you to end the routine at 2 presses if the data isn’t written - that looks like they should either all happen or none should. What happens if you add this line outside of any if statement in the Each Frame tab?

thisExp.addData("testColumn", "VALUE")

does the column testColumn appear in your data?

1 Like

Thank you so much for your response. Hope you have a good holiday. I apologize for missing your message.
Please see below for the output when added that line. It appears data can be added, but I am not sure why it is not working in the if statement and if I am referring to certain elements incorrectly.