# Record duration mouse press

**URL:** <https://discourse.psychopy.org/t/record-duration-mouse-press/20327>\
**Category:** Online experiments\
**Tags:** timing, mouse\
**Created:** [February 9, 2021, 2:03pm UTC](https://discourse.psychopy.org/t/record-duration-mouse-press/20327 "2021-02-09T14:03:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sabinosaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sabinosaurus/32/13114_2.png) [@Sabinosaurus](https://discourse.psychopy.org/u/Sabinosaurus)\
**Post date:** [February 9, 2021, 2:03pm UTC](https://discourse.psychopy.org/t/record-duration-mouse-press/20327/1 "2021-02-09T14:03:20Z")

</div>

Hi guys,

I want to record the duration of a mouse press in my experiment. I thought I had found a pretty straightforward solution that worked offline (see code below), but the duration that is recorded for the online version is always somewhere around 0.167 (1 frame I guess?).

clock is defined at the beginning of the exp (in js code component):  
sw = new util.Clock();

Each frame (in py-\> auto code component):

if mouse1.isPressedIn(polygonA):  
sw.reset()  
sound\_15.play()

if sound\_15.status == STARTED and not mouse1.getPressed()[0]:  
sound\_15.stop()  
t1 = sw.getTime()

Any help would be appreciated.

-Sabine

---

<div class="post-metadata">

**Author:** ![Sabinosaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sabinosaurus/32/13114_2.png) [@Sabinosaurus](https://discourse.psychopy.org/u/Sabinosaurus)\
**Post date:** [February 9, 2021, 8:16pm UTC](https://discourse.psychopy.org/t/record-duration-mouse-press/20327/2 "2021-02-09T20:16:24Z")

</div>

Ah, I suppose sw only starts running between the press and release of the click? So that’s one frame.

---

<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:** [February 9, 2021, 8:24pm UTC](https://discourse.psychopy.org/t/record-duration-mouse-press/20327/3 "2021-02-09T20:24:03Z")

</div>

> [@Sabinosaurus](#):
>
> if mouse1.isPressedIn(polygonA):  
> sw.reset()  
> sound\_15.play()

This is true (and therefore the clock reset) on every frame that the mouse is pressed. You need to add a flag.

The following might work

```auto
if sound_15.status == STARTED and mouse1.getPressed()[0]:
     pass
elif sound_15.status == STARTED:
     sound_15.stop()
     t1 = sw.getTime()
elif mouse1.isPressedIn(polygonA):
     sw.reset()
     sound_15.play()

```

You could add continueRoutine=False below setting t1=0. I’m not sure when you wnat to end the routine.

---

<div class="post-metadata">

**Author:** ![Sabinosaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sabinosaurus/32/13114_2.png) [@Sabinosaurus](https://discourse.psychopy.org/u/Sabinosaurus)\
**Post date:** [February 9, 2021, 8:31pm UTC](https://discourse.psychopy.org/t/record-duration-mouse-press/20327/4 "2021-02-09T20:31:44Z")

</div>

Yeah thanks, I’ll try this!

---

<div class="post-metadata">

**Author:** ![Sabinosaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/sabinosaurus/32/13114_2.png) [@Sabinosaurus](https://discourse.psychopy.org/u/Sabinosaurus)\
**Post date:** [February 10, 2021, 10:14pm UTC](https://discourse.psychopy.org/t/record-duration-mouse-press/20327/5 "2021-02-10T22:14:51Z")

</div>

Actually, it did not work properly, as it seems that the timer was never reset. I ended up adding an extra bool:

```auto
if sound_16.status == STARTED and mouse1.isPressedIn(polygon) and stopwatchStart == True:
    pass
elif sound_16.status == STARTED and stopwatchStart == True:
    sound_16.stop()
    t1 = sw.getTime()
elif mouse1.isPressedIn(polygon):
    sw.reset()
    stopwatchStart = True

```

But thanks for the help, it is much appreciated!
