我如何等待双击?

How can I wait for a doubleclick?

我想等待双击,直到显示下一个屏幕。因此,我创建了变量 doubleclick,它从零开始,每当单击鼠标时都会添加 +1。一旦鼠标被点击两次,循环就会停止。

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

点击一次后循环停止,调用下一个屏幕。

那是因为 while 循环每秒运行数千次,所以你必须按 非常快 才能 myMouse.getPressed()[0] 而不是 return True 连续多次。

这是一种相当手动的编码方式。它不关心第二次按下是否是例如。第一次后 19 秒或是否在相同(近似)位置按下。

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

如果您想添加应在短时间内按下的标准,您需要添加更多逻辑和对当前状态的跟踪:

# 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