Mouse click remains in event buffer

mouse.getPressed() returns [1, 0, 0] even after I call event.clearEvents().

This causes a subsequent portion of the script to execute improperly. That portion is supposed to check for a mouse click and execute once it detects a click. But now the click from earlier is retained, preventing the script from executing as intended.

In particular, since the script still interprets a mouse key as being pressed, it sets getPol = 0 and thus exits the loop before win.flip() can execute. So that section of the script gets skipped and none of the intended objects get displayed.

Looking for thoughts on why event.clearEvents() would not reset the output of mouse.getPressed() to [0, 0, 0]. Other thoughts on how to get around this issue also welcome. Thank you.

event.clearEvents()
getPol = 1
while getPol:

    win.clearBuffer()
    quest.draw()
    labelLeft.draw()
    labelRight.draw()
    scaleInst.draw()
    scale.draw()
                
    if event.getKeys(['escape']):
        print('Stopped! during judg')
        core.quit()
                            
    if mouse.mouseMoved():
        newPos = mouse.getPos()
        slider.pos += ( (newPos[0] - slider.pos[0]), 0)
        if slider.pos[0] <= sliderMin:
            slider.pos[0] = sliderMin
        elif slider.pos[0] >= sliderMax:
            slider.pos[0] = sliderMax
                        
    slider.draw()

    event.clearEvents()

    a = mouse.getPressed()
    print a

    if any(mouse.getPressed()):
        ratingCoord = float(slider.pos[0])
        rating = float(ratingCoord - sliderMin) / float(sliderMax - sliderMin)
        polRating = rating
        getPol = 0
        win.clearBuffer()
        
    win.flip()

Mouse presses are a bit different to the standard PsychoPy keyboard events. Think of .getPressed() as telling you the current state of the mouse button, not that it was previously clicked. i.e. because we often hold the mouse button down (e.g. to drag something), clearing the event cue doesn’t clear the fact that the mouse button is still held down, as that is something we generally want to know about.

Thanks. However, the mouse button is never held down in this experiment. There’s a click that occurs before these lines of code, so I thought it might be possible that what the user perceives as a short click might be perceived by the script as the mouse being held down (since the script runs so quickly). To test that, I inserted a 500ms delay between the prior lines of code and this segment, and the program still thinks the mouse button is being pressed.

Just for general background, this section of code is running on every screen refresh, likely with a maximum interval of 16.7 ms between iterations. Even if the subject isn’t intentionally holding down the mouse button, it’s more than possible that it will be detected as being pressed multiple times within the duration of a single click. That might not apply here, as you exit the loop upon the detection of a click, but worth bearing in mind in general, as a mouse click is not an instantaneous event. It could apply here if this code proceeds immediately from a previous mouse click.

You note that you’ve tried things with a 500 ms delay between the previous mouse click and this code, but I guess we would need to see all of that to make sense of what is happening.

That’s not quite the case: your win.flip() is unconditional in this code. You just don’t notice it in this particular case, as if a click is detected, you call also win.clearBuffer(), which erases all of the previous drawing so that only a blank screen is flipped.

Your initial win.clearBuffer() is also unnecessary, as the back buffer is automatically cleared after a win.flip(). This isn’t really a command that is necessary in most PsychoPy code.

Thanks. Problem solved. I increased the delay to 2 seconds, and that reliably resets the output of mouse.getPressed() to [0, 0, 0], which enables the code snippet to run as intended.