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

**URL:** <https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912>\
**Category:** Builder\
**Created:** [July 25, 2024, 6:02am UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912 "2024-07-25T06:02:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Nicholson](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Alex\_Nicholson](https://discourse.psychopy.org/u/Alex_Nicholson)\
**Post date:** [July 25, 2024, 6:02am UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912/1 "2024-07-25T06:02:46Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/3/833d10fd9548d1b2e841ab7db771e8441891c63c.png)

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.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 25, 2024, 6:42am UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912/2 "2024-07-25T06:42:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alex\_Nicholson](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Alex\_Nicholson](https://discourse.psychopy.org/u/Alex_Nicholson)\
**Post date:** [July 25, 2024, 7:01am UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912/3 "2024-07-25T07:01:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 25, 2024, 8:39am UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912/4 "2024-07-25T08:39:08Z")

</div>

Let’s call your key release component key\_held

End Routine

```auto
if not key_held.keys:
     inner_loop.finished = True

```

---

<div class="post-metadata">

**Author:** ![Alex\_Nicholson](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Alex\_Nicholson](https://discourse.psychopy.org/u/Alex_Nicholson)\
**Post date:** [July 25, 2024, 9:11am UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912/5 "2024-07-25T09:11:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alex\_Nicholson](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Alex\_Nicholson](https://discourse.psychopy.org/u/Alex_Nicholson)\
**Post date:** [July 25, 2024, 9:57pm UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912/6 "2024-07-25T21:57:36Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 29, 2024, 2:26pm UTC](https://discourse.psychopy.org/t/how-to-detect-whether-key-still-held-down-from-previous-routine/40912/7 "2024-07-29T14:26:13Z")

</div>

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.
