| Reference | Downloads | Github

Defining a key that ends routine in JS

URL of experiment:

Description of the problem: Hi, can anyone help me to convert a Python code to JS please?
In my experiment I want to end a routine (not the entire loop!) if the participants press ‘left’ or ‘right’, but to continue the routine if they press ‘space’.
In the builder, I used this Python code:

from psychopy import event
keys = event.getKeys()

if keys:
    if 'space' in keys: # skip routine
        continueRoutine = True #do not end routine.
    elif 'right' in keys or 'left' in keys:
        continueRoutine = False #end routine. 

I tried to convert this code to JS, but it still doesn’t work :frowning_face:
That is, in the moment, it doesn’t matter if they press ‘left’, ‘right’, or ‘space’, it continues the routine.

When you say you tried to convert the code to JS, what exactly do you mean? Did you use the Auto->JS system in the most recent versions of PsychoPy? What does the JavaScript code look like? (Remember to surround code blocks with ` when posting them on the forum so they format correctly.)

I managed to find the right code in JS:
if (theseKeys.length > 0) { // at least one key was pressed key_respTrial.keys = [].concat(key_respTrial.keys, theseKeys[0].name).filter((i) => i !== undefined); // storing all keys key_respTrial.rt = [].concat(key_respTrial.rt, theseKeys[0].rt).filter((i) => i !== undefined); if (theseKeys.length > 0 && theseKeys[0].name === 'space') { textShortLong.setText(LongShort); StudyTrialComponents.push(textShortLong); textShortLong.setAutoDraw(true); // a response ends the routine continueRoutine = true; } else { continueRoutine = false;

But it stores only the first key pressed. That is, if the participant pressed ‘space’ and then ‘right’, it saves only the ‘space’ key (and its reaction time).
Does anyone know what I should write to enable it to save two keys and two reaction times??? (i.e., space + rt , right + rt)
Thanks in advance!

[I uploaded the experiment again so its under the name: ]

In builder, is the keyboard component set to store all keys or first key?

Also, the experiment is not public so I can’t view it.

It is set to store all keys.

I’ll try to explain a bit further. In my experiment, the participants see pictures on the screen and are required to press ‘right’ or ‘left’ in response to. Additionally, they can ask for a text which appears next to the picture by pressing ‘space’ (see the code I posted before)… but they still have to press ‘right’ or ‘left’ to continue the routine.
In the builder I used two blocks since I couldn’t get the Python code to allow only the ‘space’ key to make the text appear and the ‘right’ and ‘left’ keys to continue to the next picture. But JS does it in one block. So now, instead of having two blocks, I have only one block (with the options of pressing ‘right’, ‘left’ or ‘space’).
So in the builder I had a key and rt stored for each block individually. But now, I am lacking a code that makes sure that if ‘space’ is pressed, then the second response is also being saved (key + rt). And that the second response can only be ‘right’ or ‘left’.

Thanks for the help!