psychopy.org | Reference | Downloads | Github

Response to mouse button up (mouse release) instead of mouse button down


#1

Hi all,

I am using Psychopy 3 on Windows 10 and I am trying to create a rectangle that - when clicked on - allows users to continue to the next screen. I managed to make this work on a left button press (see example below, where the screen just closes after a button press). However, when creating multiple screens with identical ‘continue buttons’, I discovered that the programme skips through all screens if you hold the mouse button for too long.

The ideal solution would be to only close/flip the current screen after a ‘mouse button up’ press (comparable functionalities exist in Neurobs Presentation). I saw that the CustomMouse has this option, but unfortunately this functionality is too limited for the rest of my script. Does anybody know another workaround…?

Note: a simple wait() statement could do part of the job of course, but I am looking for a more ‘neat’ option.

#import modules
from psychopy.visual import Window, Rect
from psychopy.event import  Mouse

#initialise screen and mouse
display = (1366,768) 
win = Window(size=display, color='white', fullscr=False, units = 'pix')
mouse = Mouse(visible=True)

#create box object
box = Rect(win,width=420,height=50, lineWidth = 3,lineColor='black', pos=(0,0), fillColor = (0.9,0.7,0.7))

#show box object until mouse click
while True:
    box.draw()
    if mouse.isPressedIn(box, buttons=[0]):  # left button press
        win.close()
    win.flip()

#2

At the start of the next routine set a variable mouse_has_been_up = False

On your check of the mouse, set that variable to True if the button is not pressed. Meanwhile, if the button is pressed, only take action if that variable is True.

ie this ensures that responses to a mouse press only occur if there has been an intervening mouse release before it.


#3

Thanks for your reply!

For those interested, this is how I translated the solution into code:

#import modules
from psychopy.visual import Window, Rect
from psychopy.event import  Mouse

#initialise screen and mouse
display = (1366,768)
win = Window(size=display, color='white', fullscr=False, units = 'pix')
mouse = Mouse(visible=True)

#create box object
box = Rect(win,width=420,height=50, lineWidth = 3,lineColor='black', pos=(0,0), fillColor = (0.9,0.7,0.7))

#show box object until mouse click
mouse_has_been_pressed = False
while True:
    box.draw()

    if mouse.isPressedIn(box, buttons=[0] ):  # left button press
        mouse_has_been_pressed = True

    if not mouse.isPressedIn(box, buttons=[0] ) and mouse_has_been_pressed == True:
        win.close()
        mouse_has_been_pressed = False

    win.flip()