Locking or restraining mouse position

If this template helps then use it. If not then just delete and start from scratch.

OS MacOS Monterey v. 12.4
PsychoPy version 2022.2
**Standard Standalone? Y

First, I appreciate any and all help. I’m fairly new to PsychoPy, and the eventual goal here is to get a mousetracking experiment going. What I refer to here is just a very simple example I’ve been playing with to explore some components I hadn’t used before (mouse events, button response).

What are you trying to achieve?:

I attach a minimal example below, but I am hoping for the following:

In the routine “begin”:
· Click center dot to proceed to “trial”.

In the routine “trial”:
· Set mouse position to (0, 0) at start of routine.
· Lock that position for duration of stimuli presentation (set here as 3s).
· Present stimuli (text and center dot) for 3s.
· After stimuli presentation (3s), present two buttons as valid clicks to end routine.
· End routine on valid click (either of the buttons).

What did you try to make it work?:
In the following code chunk under “Begin Routine” for trial, using core.Clock() allows the routine to run, but only resets initially to 0, 0. My hope was that the while loop would restrain it for the 3s duration. Using t (as commented out) doesn’t allow the routine to initiate; it just freezes at the end of prior routine.

This was a first attempt. I’m sure there are other better ways to achieve this end, but I’m not at a place yet where I’m seeing what those are.

mouseTimer = core.Clock()

while mouseTimer.getTime() < 3:
    mouse.setPos(newPos=(0,0))
    
#while t < 3:
 #   mouse.setPos(newPos=(0,0))

What specifically went wrong when you tried that?:

In addition to what I state above (only resetting but not “locking” mouse position), it presents the stimuli and the buttons all at one time (apparently 0s) then offsets the center dot and stimuli at 3s.

EDIT to add… My interest in maintaining the visibility of the mouse cursor (as I know its visibility can be turned off or it could be instantiated later in the routine) is guided by the fact that for an eventual mousetracking experiment I’d like to maintain its presence in the participant’s mind as the response tool.

lock_mouse.psyexp (22.5 KB)

Hi @dwbatten,

I’ve just updated your code very slightly to fix this. I’ve essentially just moved the code you had to the Each Frame tab rather than Begin Routine. This is because your timer needs to be checked on every frame to see if 3s has passed.

Please give the attached a go and let me know if it does what you’re after.
lock_mouse.psyexp (22.3 KB)

Thanks so much,

Kim

1 Like

Thank you! That does work. I wasn’t sure if the “while” loop would keep querying that constantly, but I see how setting it to refresh the call on frames works.

1 Like