2 digit response

Hello. I’m trying to create an experiment where participants respond by typing a 1 or 2 digit response, and then press ‘return’ to advance.

I cannot figure out how to create this in the builder. If I select ‘force end of routine’ then I can only respond with 1 keypress. But if I do not, then I cannot advance to the next question.

I have tried to write a stop condition, but I cannot figure out the syntax. For example, I tried $keys[-1]==‘return’, but the error message says that ‘keys’ is not defined.

Any help is appreciated. Thanks.

That is almost valid, but keys is an attribute of the keyboard component rather than being a stand-alone variable, so you need to add its name, like this:

your_keyboard_component_name.keys[-1] == 'return'

However, all that will do is stop the keyboard component from running: it won’t end the trial. So you should instead put that code in a code component rather than in the keyboard component, as that way you can add a line or two. e.g. put something like this in the Each frame tab of a code component so that it runs on every screen refresh:

if your_keyboard_component_name.keys[-1] == 'return':
    continueRoutine = False

You could also concatenate the keypresses into a single string rather than a list, and drop the final 'return', and store that in the data file to make your analysis easier:

if your_keyboard_component_name.keys[-1] == 'return':
    thisExp.addData('response', ''.join(your_keyboard_component_name.keys[0:-1]))
    continueRoutine = False

so ['1', '2', 'return'] would become '12'