psychopy.org | Reference | Downloads | Github

Record duration mouse press

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

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

1 Like

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

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.

Yeah thanks, I’ll try this!

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

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!