Define the start/stop condition of a component is a function of the key press

OS : win 10
PsychoPy version: 2024.2.4 Py 3.8
Standard Standalone Installation? (y/n) :y
**Do you want it to also run online? (y/n)**n
What are you trying to achieve?:
Is there possible to define the start/stop condition of a component is a function of the key press?
Like :
①left/right part disappear when the key ‘right’ or ‘left’ pressed
②Left/right highlight rectangles appear when the key ‘left’ or ‘right’ pressed

What did you try to make it work?:
Builder component:
4 rectangles indicate the card
4 texts contain figures
2 highlight rectangles highlight the key_press choice

Link to the most relevant existing thread you have found:




should i add what condition to make this function happen?
thanks

Hi @Tse_Cherry,

Is it that you’d like the experiment to follow this flow:

  1. All 4 rectangles are presented
  2. The participant presses left/right
  3. Their choice is shown (by either the left or the right rectangles remaining on the screen) for a given duration
  4. The routine ends once that duration is reached

Thanks,

Kim

Hi Kim,
Yes :laughing: I want to achieve this rountine by adding the function of start/stop condition of key press. Here comes the question.

Here is a simple demo.

Hide Side code | try it

Hide visual components based on a key press and then end the routine after a delay.

I have the following code component.

Begin Routine

hideLeft = False
hideRight = False
endTrial = 9999 # Long default routine duration

Each Frame

if t > endTrial:
    continueRoutine = False
elif key_resp.keys and endTrial == 9999:
    if 'left' in key_resp.keys:
        hideRight = True
    elif 'right' in key_resp.keys:
        hideLeft = True
    endTrial = t + 1 # One second delay

For the instruction text I put an end condition of hideLeft or hideRight.