Counting mouse clicks while click is held down

Hey everyone,

I’m attempting to code an experiment that asks participants to click target stimuli intermixed with distractors. The code has no problem determining whether it was a target or a distractor that has been clicked, but if the mouse click is held down for more than an instant, it will keep adding to the counter for that respective stimulus.

The following is from the “each frame” tab in the main trial routine, as there are many stimuli on the screen at a given time and the routine ends on a timer:

for clickable in active_targs:
    if mouse.isPressedIn(clickable):
        clicked_targs.append(clickable.name)
        thisExp.addData("clicked_targs",clicked_targs)
        nTargClicks = len(clicked_targs)
for clickable in active_dists:
    if mouse.isPressedIn(clickable):
        clicked_dists.append(clickable.name)
        thisExp.addData("clicked_dists",clicked_dists)
        nDistClicks = len(clicked_dists)

The issue seems to be that it checks if the mouse is pressed on each frame, and if it is, it adds to the counter; so, if the mouse is pressed for more than one frame, the counter continues incrementing, rather than registering it as only one click.

To give some additional info which may be helpful, the clickable stimuli are boxes. Each box is determined to be a target or a distractor based on its position and its contents, and is added to a target or a distractor master list. There are 860 boxes total, which appear across 14 screens (60 boxes on each screen). Depending on how many times the routine has looped, the active targets and distractors are pulled from master lists and represented in “active_targs” and “active_dists” respectfully.

Essentially, the fix I am looking for would only increment the counters when the mouse has been ‘unpressed,’ so that way it does not keep adding to the counter if the mouse is pressed for more than a single frame.

If anybody has any ideas or solutions, please let me know. Also, if there is more information you would like about the code or the experiment, do not hesitate to reach out.

Thank you.

Hi @blund2, if you don’t care if a box was clicked twice (not counting multiple “clicks” without release) you could pragmatically fix this by only counting unique values in clicked_targs. Like this

for clickable in active_targs:
    if mouse.isPressedIn(clickable):
        clicked_targs.append(clickable.name)
        clicked_targs = list(dict.fromkeys(clicked_targs))
        thisExp.addData("clicked_targs", clicked_targs)
        nTargClicks = len(clicked_targs)
for clickable in active_dists:
    if mouse.isPressedIn(clickable):
        clicked_dists.append(clickable.name)
        clicked_dists = list(dict.fromkeys(clicked_dists))
        thisExp.addData("clicked_dists", clicked_dists)
        nDistClicks = len(clicked_dists)

If you care about “proper” multiple clicks, this might help: Prevent multiple mouse clicks when holding it

Hi @ajus, that solution made it so that the counter would not increment after 1 because the clickables all have the same name, therefore they’re not unique. However, the link you provided was just what I needed and my code is working properly now. Thank you for your help!