Python 3.x 我怎样才能等待双击?
我想等待双击,直到显示下一个屏幕。因此,我创建了变量doubleclick,它从零开始,每次单击鼠标时都会添加+1。只要鼠标点击两次,循环就应该停止Python 3.x 我怎样才能等待双击?,python-3.x,mouse,psychopy,Python 3.x,Mouse,Psychopy,我想等待双击,直到显示下一个屏幕。因此,我创建了变量doubleclick,它从零开始,每次单击鼠标时都会添加+1。只要鼠标点击两次,循环就应该停止 def Instruction(x): """Function Instruction(x) presents instruction text in string x""" instrText.setText(x) myMouse.clickReset() doubleclick = 0 while Tru
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]
才能连续多次返回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
#把事情安排好
从psychopy导入视觉、事件
导入时间
win=visual.Window()
myMouse=event.Mouse()
间隔=1.0#第二次单击的窗口计数为双精度
按注册=错误
release\u registered=False
尽管如此:
#获取潜在双击中第一次按下的按下时间
如果没有,请按\u和myMouse.getPressed()[0]:
t_press1=时间。时间()
按注册=真
#检测(潜在的第一次)点击是否已完成
如果按注册而不是myMouse.getPressed()[0]:
按注册=错误
release\u registed=True
#如果有第一次点击。。。
如果已注册发布单元:
#收听间隔内的第二次单击
如果time.time()-t按1<间隔:
如果myMouse.getPressed()[0]:
打破
#超时,重置
其他:
按注册=错误
release\u registered=False
这是因为while
循环每秒运行数千次,因此您必须以极快的速度按下myMouse.getPressed()[0]
才能连续多次返回True
。听鼠标在点击之间释放。啊,谢谢,这很有意义。但很抱歉,我如何在点击之间收听鼠标释放?或者我将如何在代码中实现等待两次单击?我在下面添加了两个解决方案。