How to detect whether key still held down (from previous routine)

Win11
PsychoPy version 2024.1.5
Standard Standalone

Hi there,

I want my loop to restart if the key ‘5’ is not held down for the duration. If the key is held down at the end of the loop I want the loop to finish and go on to the next routine.

In my loop (inner_loop) I have two routines: keys_initiation and fixation_cross_3.

In the keys_initiation routine I have a keyboard component, that forces the end of the routine when the number 5 is pressed.

In the fixation_cross _3 routine I have a fixation cross with a duration of 2 seconds. I want the participant to be required to hold down the number 5 for the duration of this routine - and if they release before the end they should be taken back to the start of keys_initiation.

I have tried the following code component in the fixation_cross_3 routine but I think it is flawed as it seems to clear the keyboard press from the previous routine (so i t immediately thinks that no key is being pressed):

Begin routine:

key_held_down = False

Each frame:

if key_resp.keys == 5:
key_held_down = True
else:
key_held_down = False

if not key_held_down:
inner_loop.finished = False
continueRoutine = False

End routine:

if key_held_down:
inner_loop.finished = True

Any help much appreciated! Thanks.

This is more code than you need.

In the second routine add a keyboard component that monitors for the 5 key being released. Then finished the loop if a response is detected.

Thanks for the speedy response.

My concern with that approach is that I need a way to take the participant back to the keys_initiation routine if they release early during fixation_cross_3. But equally I need a way to move on to stimulus_onset if they hold the 5 down for the duration of the fixation_cross_3 routine. I thought the way to do that was with the inner_loop function and code that says to break out of the routine if …

Perhaps I just need to do it as per your suggestion (add the keyboard component into fixation_cross_3 and add some code at the end of the routine that says that if the routine has lasted for 2 seconds then inner_loop finished = True?

Let’s call your key release component key_held

End Routine

if not key_held.keys:
     inner_loop.finished = True

Oh my gosh, that has worked! So beautifully simple. Thank you!

Hi just to follow this up, could I please get your advice on how I can do the equivalent with a mouse response? The flanker experiment I am building has two different conditions - using keyboard responses, and also touch screen responses (using mouse component).

I have set up the mouse condition with circles on the screen - the participant will be required to touch the circle at the bottom of the screen and keep their finger holding down on that circle to initiate the trial.

So similar to above, I thought I would be able to use the mouse component to end the routine if it was “released”, but the mouse component does not have a “release” action like the component does, to force end of routine. Is there any chance you could suggest an easy way to acheive this? Thanks!

You could have a loop with lots of reps which is escaped (using loopname.finished == True) if the mouse response is not made within the time limit – you could have a time limit on the routine of 0.3 or 0.5 seconds.