Turn text component on and off in single routine

Using version (2023.2.3).

In my experiment, after watching a video stimulus participants are asked to replicate the position of a number of objects on a blank canvas. Initially, I used a spacebar keyboard response to end routine and move onto the next. However, I would like to add in extra step asking if the participant is finished with their choices before moving on (in case they pressed spacebar by accident, for example).

The plan is to press space bar > display a message like ‘Are you finished, left arrow for no, right arrow for yes’ > if left, remove text component and remain on routine, if right, end routine and go to next routine.

To achieve this, so far, I have set the text component to start on a condition (spaceisPressed) and stop on a condition (leftisPressed), and the following lines of code.

In Begin Routine

spaceisPressed = False
leftisPressed = False

event.clearEvents() #as spacebar is pressed in the previous routine and I couldn't press it again. 

In Each Frame

keys = event.getKeys()
if 'space' in keys:  
    spaceisPressed = True
 
if 'left' in keys:    
    leftisPressed = True

So far this achieved what I want (text appears after ‘space’ and disappears after ‘left’) but I can’t complete it more than once, i.e., the participant can’t press space again to display the text.

I have tried adding ’ event.clearEvents() ’ at the end of the Each Frame code, but this didn’t help.

I have tried adding ‘leftisPressed = False ’ after ‘if ‘space’ in keys:’ and ’ spaceisPressed = False ’ after if ‘left’ in keys :’, but these didn’t make a difference.

I have tried setting the data tab of the keyboard component to last key, and all keys (instead of first key), this still doesn’t help. I also changed the keyboard component to set every repeat, but I didn’t think this would work.

I’m not very experienced in coding, so not sure if I’m missing something fairly obvious. Any help appreciated :slight_smile:

Thanks,
Aimee

Dear Aimee,

Without seeing your experiment in more detail, I think the problem is that at the beginning of the routine it sets spaceispressed = False, and depending on the initialization of that routine, might not be set correctly on repeat.
One way to set this up, without a lot of coding knowledge, is to separate the steps in this routine in separate steps. It would like something like this:


Where the video is played.
Then the position replication occurs, and that routine ends with a spacebar.
Then a screen of “Are you sure? Yes No” is shown. If yes, you end the trial (end_of_trial for visual purposes), and if “No” you repeat the loop from the beginning of participant input.
Another tip to make sure erroneous participant inputs are “protected against” is to set the spacebar input to start 2-3 seconds into the routine, or some amount of time that is unrealistic to complete the task. This way if they accidentally press it, it isn’t registered, and for lazy participants, it forces them to at least try before skipping through the experiment.
Issac

1 Like

Hello

In this little toy experiment, when I press space, the text “press space” disappears and the instruction to continue or end the experiment appears. If I press left, the text “press space” replaces the instruction. The experiment sets the opacity of the text stimuli depending on the key pressed. The code is as follows

Begin experiment

opaqueCont = 0
opaqueSpace = 1

Each frame

keys = event.getKeys()

if 'space' in keys:
    opaqueSpace = 0
    opaqueCont = 1
elif 'left' in keys:
    opaqueSpace = 1
    opaqueCont = 0
elif 'right' in keys:
    continueRoutine = False

and the same setup for the “space-text”


Flow of the toy experiment

Does this fit in with what you want to achieve?

Best wishes Jens

1 Like

Hi Issac & Jens,

Thank you both so much for this advice! I did manage to work something out on my own before getting your responses.

Issac, I had considered the idea of splitting it into separate routines, but I was worried that the response (position data of the objects) wouldn’t ‘save’. I.e., if participants decided that they weren’t finished the trial, and went back to the previous routine, they would have to input the response all over again. I thought there might be away prevent that via code, but I wasn’t sure how easy that would have been.

Jens, thanks for sharing this and it’s very similar to what I ended up with by changing the opacity of the text. Instead of an elseif statement, I used separate if statements, but perhaps it would have made more sense using elseif statements like you.

In case anyone is interested, here is the code I ended up with.

Begin routine

opacity = 0
spacepress = False

event.clearEvents()

Each frame

keys = event.getKeys() 
if 'space' in keys: 
    opacity = 1.0
    spacepress = True
 
if 'left' in keys:    
    opacity = 0.0
    spacepress = False
    
if 'right' in keys and spacepress:
     continueRoutine = False
    
event.clearEvents()

I had to make the variable $spacepress as the start condition for the right keyboard press, in order to prevent participants from pressing right before they pressed spacebar.

It might not have been the best way to get around it but it does what I want for now! If anyone spots anything that might cause issues, please feel free to let me know as I would welcome any advice to improve my coding skills and use of psychopy :slight_smile:

Thanks,
Aimee

1 Like