Mouse movement restriction: move only in the (0, y) axis

Hello,

I’m a beginner for coding in PsychoPy.

In my experiment, I would like that the participants can only move the mouse vertically from the center of the screen (0, 0). In other words, I would like that participants can only move on the (0, y) axis.

I tried the code below, but the movement of the mouse movement is no longer fluid, but jerky.

Could you help me ?

Best,
KB

mouse.setVisible(True)
mouse.setPos((0, 0))

x, y = mouse.getPos()
if x > 0: # Si la souris est au-dessus de la balle
mouse.setPos((0, y))
elif x < 0:
mouse.setPos((0, y)) # Retourner la souris au centre de la balle

Please could you confirm which tabs these are in?

Personally I would probably hide the real mouse pointer and move an image of a mouse pointer along the desired vertical line: position (0,mouse.getPos()[1])

For a better exposition of my issue, you could see the full code below

from psychopy import visual, event, core, monitors
import random
from numpy import linspace

win = visual.Window(size=(800, 600), color=‘black’, units=‘pix’)

ball = visual.Circle(win, radius=56.69, pos=(0, 0), fillColor=‘gray’, lineColor=None)
opening = visual.Circle(win, radius=30, pos=(0, 175), fillColor=‘white’, lineColor=None)
chemin_image = “cross.jpg”
cross = visual.ImageStim(win, image=chemin_image, pos=(0,-175))
chemin_image = “fixationcross.png”
false_circle = visual.Circle(win, radius=10, pos=(0, 0), fillColor=‘blue’, lineColor=None)
fixation_cross = visual.ImageStim(win, image=chemin_image, pos=(0,0))
fixation_ball = visual.Circle(win, radius=8, pos=(0, 0), fillColor=‘gray’, lineColor=None)

opening_sizes = linspace(5.67, 113.4, num=20)
random.shuffle(opening_sizes)

for block in range(5): # 5 blocs
for opening_size in opening_sizes:
fixation_cross.draw()
fixation_ball.draw()
win.flip()
mouse = event.Mouse()
response_clock = core.Clock()
response_clock.reset()
click_detected = False
while not click_detected and response_clock.getTime() < 10:
if mouse.getPressed()[0]:
if fixation_ball.contains(mouse):
click_detected = True

    opening.setRadius(opening_size) 
    ball.draw()
    opening.draw()
    cross.draw()
    win.flip()
    
    participant_response = None
    mouse = event.Mouse()
    mouse.setVisible(True)  
    mouse.setPos((0, 0))    
    response_clock = core.Clock()
    response_clock.reset()
    click_detected = False
    while not click_detected and response_clock.getTime() < 10:
        mouse_pos = mouse.getPos()
        if mouse.getPressed()[0]:
            if opening.contains(mouse):
                participant_response = 'opening'
                click_detected = True
            elif cross.contains(mouse):
                participant_response = 'cross'
                click_detected = True
                
        # Restreindre le mouvement vertical
        x, y = mouse.getPos() 
        if x > 0:  
            mouse.setPos((0, y))  
        elif x < 0:  
            mouse.setPos((0, y))  
        
    if participant_response is None:
            participant_response = 'NR'

    print(f"Block: {block + 1}, Opening Size: {opening_size}, Response: {participant_response}")

win.close()

Try replacing this with

# Restreindre le mouvement vertical
        x, y = mouse.getPos() 
        mouse.setPos((0, y)) 

Thanks for you help and you suggestion. Unfortunately if I replace with this, my mouse is frozen. It does not move.

I’m trying to share a video of the issue with my code :

You’ve given me an idea. Try

# Restreindre le mouvement vertical
        x, y = mouse.getPos() 
        if x > 0.05:  
            mouse.setPos((0.05, y))  
        elif x < 0.05:  
            mouse.setPos((-0.05,y))

I think your line needs some width

Thanks, that’s a great idea. It doesn’t solve the problem. Nevertheless, I have exaggerated the width to try to clarify the nature of the problem. It seems that the movement is not fluid because the mouse does not return to the y position when the participant deviates from x, but returns to a lower y position. See the video below for an example.

x, y = mouse.getPos()
if x > 10:
mouse.setPos((0, y))
elif x < -10:
mouse.setPos((0, y))