| Reference | Downloads | Github

Track mouse movement and time

Hi all,

I’m trying to program an experiment where people need to move the mouse from one side of the screen to another. I’m stuck with a very simple problem. I need participants to do quick mouse movements from the left side of the screen (-.7,0) to the center of the screen (0,0) in less than 3 seconds. If they take more than 3 seconds, they have to repeat the trial. I’ve managed to get everything to work, except that I would like the 3 seconds to start counting only once participants start moving the mouse. With my code now, the 3 seconds start even if participants haven’t moved the mouse (so if I wait for 3 secs, and then start moving the mouse, the trial is repeated right away)


            if checkstart == True:
                if -.69 <= x <= 0 and t > 3: #if mouse starts moving on x axis
                        t > 3: #if time is more than 3 secs
                    #buzzer plays
                             key_resp.corr = 3 #trial is repeated

Any help would be highly appreciated!


Hi @Mardock, you can take advantage of some of the mouse methods for this e.g., “mouseMoved” - see docs.

With this solution, you set your mouse position at the beginning of each trial, set a mouseTimer and set a flag to show whether the mouse has moved since the beginning of the trial:

# Begin Routine
mouse.setPos((-.7, 0))     # Set mouse to left position - or use variable from your conditions file
mouse.getPos()             # Necessary to reset position for mouseMoved mouse method
mouseTimer = core.Clock()  # New mouse clock
mouseMoved = False         # move flag

# Each Frame
if mouse.mouseMoved() and not mouseMoved:
    mouseTimer.reset()     # Set clock to zero
    mouseMoved = True

if mouseMoved:
    if mouseTimer.getTime() > 3:      # ...etc
1 Like

Hi @dvbridges. Thanks! I tried your solution, but for some reason it’s not working. I don’t get any errors, but even when I take more than 3 secs, nothing happens…
There’s something I forgot to mention about my problem: what I want is for the clock to start counting the moment people start moving, but stop counting once people go beyond the center of the screen (0.0). The important thing here is that the clock should only count when they move from (-.7,0) to (0,0).

Ok, what else should happen when they reach the middle of the screen?

They have to press a button. The task is a bit more complicated than what I described, but the overall idea is that the mouse needs to be moved quickly (in less than 3 secs) from (-.7,0) to (0,0). They can however continue moving the mouse beyond (0,0), but without the time constraint. The 3 seconds constraint only applies when they move from (-.7,0) to (0,0), and not beyond that.

Ok, heres another example. If you do not get to the center, you get negative feedback after 3 seconds. If you make it past the center, the trial continues until you click the mouse (anywhere) and you get positive feedback. If you end the trial using a mouse click before you reach the center, you get negative feedback.

mouseTest.psyexp (13.7 KB)

The behaviour can be adapted for your task, but the core aspects of detecting when the mouse crosses line and providing auditory feedback if user has not crossed the line after 3 seconds are all there.


Thanks @dvbridges. I ran your code, and it does exactly what I’m looking for. However, for some reason when adapting it to my task (I copied the relevant parts at the beginning of the trial and in each frame), this doesn’t seem to do anything in my task, i.e. even when stopping for more than 3 seconds before the middle of the screen the trial is not repeated (nothing actually happens).

I’ll try to see if I manage to find a solution. Thanks!