Search code examples
python-3.xmousepsychopy

How can I wait for a doubleclick?


I want to wait for doubleclick until the next screen is shown. Herefore, I created the variable doubleclick, which starts at zero and +1 is added whenever the mouse is clicked. As soon as the mouse is clicked twice the loop is supposed to stop.

def Instruction(x):
    """Function Instruction(x) presents instruction text in string x"""
    instrText.setText(x)
    myMouse.clickReset()
    doubleclick = 0
    while True:
        instrText.draw()
        myWin.flip()
        if myMouse.getPressed()[0]:
            doubleclick += 1
        if doubleclick == 2:
            myMouse.clickReset()
            break

The loop stops after only one click and the next screen is called.


Solution

  • That's because the while loop runs thousands of times per second, so you would have to press extremely fast for myMouse.getPressed()[0] not to return True multiple times in a row.

    This is a quite manual way to code it. It does not care whether the second press is e.g. 19 seconds after the first or whether they were pressed at the same (approximate) location.

    def Instruction(x):
        """Function Instruction(x) presents instruction text in string x"""
        instrText.text = x
        myMouse.clickReset()
        instrText.draw()
        myWin.flip()
    
        # Wait for button press
        while not myMouse.getPressed()[0]:
            pass
    
        # Button was pressed, now wait for release
        while myMouse.getPressed()[0]:
            pass
    
        # Button was released, now wait for a press
        while not myMouse.getPressed()[0]:
            myMouse.clickReset()
    
        # The function ends here, right after second button down
    

    If you want to add the criterion that they should be pressed within a short timeframe, you would do add a bit more logic and tracking of the present state:

    # Set things up
    from psychopy import visual, event
    import time
    win = visual.Window()
    myMouse = event.Mouse()
    
    interval = 1.0  # window for second click to count as double
    press_registered = False
    release_registered = False
    
    while True:
        # Get press time for first press in a potential double click
        if not press_registered and myMouse.getPressed()[0]:
            t_press1 = time.time()
            press_registered = True
    
        # Detect that the (potential first) click has finished
        if press_registered and not myMouse.getPressed()[0]:
            press_registered = False
            release_registered = True
    
        # If there has been a first click...
        if release_registered:
            # Listen for second click within the interval
            if time.time() - t_press1 < interval:
                if myMouse.getPressed()[0]:
                    break
            # Time out, reset
            else:
                press_registered = False
                release_registered = False