Prevent multiple mouse clicks when holding it

URL of experiment:

Description of the problem:

Hi, everyone. Does anyone know how to solve the problem of multiple mouse clicking when holding it?
In my experiment, mouse should be clicked multiple times in a routine, so I put the code about mouse clicking in the ‘each frame’ section of the code component.

for clickable in clickables:
    if pracforage.isPressedIn(clickable, buttons = [0]):
        event.clearEvents()
        pracforage.clickReset()
        pracforage.status = PsychoJS.Status.FINISHED
        clickables = [] #to prevent other clickings..
        clicked_all = []
        clicked_all.append(clickable.name) #store every clicks

What I need is to click only one item per each click.
However, when I hold the mouse clicked for several frames, items with the mouse cursor are all clicked, unlike my intention…
I want to receive the first clicking information until the mouse click is released.
I tried various things like removing the mouse click event, clickReset(), changing the mouse status to ‘FINISHED’ (then change it to ‘STARTED’ after things are done), copying ‘clickables’ from a global variable before the mouse click then removing all the items in clickables when something is clicked. The problem is that because this code is run each frame, all the restrictions I made are alive again.
Can I ignore all other events except for the first click until the mouse is released???

Thanks!

Hi @hij113,

I would add some code to the Begin Routine section of your code component to make the variables for storing clicks and mouse state. Then in Each Frame you perform your checks of mouse state and update values as needed.

If the mouse-state is ‘up’, you check for new mouse clicks. If a mouse click happens, then you update your variables and record mouse-state as ‘down’.

If the mouse-state is ‘down’, you check for the mouse button to be released. If that happens, then you reset the mousse-state as ‘up’.

Some sample Python code is below.

In Begin Routine:

mouseup = True # the mouse buttons should be starting in the up position
# initialize stored click variables here, for example:
# clicked_all = []
# number_of_clicks = 0

In Each Frame:

if mouseup : # mouse-state was last recorded as up,
    if 1 in mouse.getPressed(): # mouse has been pressed, update values
        mouseup = False # mouse is now down
        # UPDATE CLICK VARIABLES, for example:
        # number_of_clicks += 1
        
else: # mouse-state is recorded as down
    if not 1 in mouse.getPressed(): # no buttons on the mouse are held down, reset mouse-state to up
        mouseup = True

I use 1 in mouse.getPressed() to check if any button of the component mouse has been pressed anywhere on-screen.

One limitation is that if the mouse is held down when the routine ends, the next routine may record a click.

-shabkr

1 Like

It works perfectly!!!
Thanks a lot!