我看过pyglet小鼠事件,它很好地处理常见事件,如单击、拖动和释放鼠标按钮。我想要处理双击事件,但这似乎不那么简单。
我应该仅仅监视mouse_press和mouse_release事件,并比较时间间隔和位置来检测双击事件吗?
我不想重新发明轮子。是否有检测pyglet双击事件的“最佳实践”?
到目前为止,这种方法是我得到的最好的方法:
import time
import pyglet
class MyDisplay:
def __init__(self):
self.window = pyglet.window.Window(100, 100)
@self.window.event
def on_mouse_release(x, y, button, modifiers):
self.last_mouse_release = (x, y, button, time.clock())
@self.window.event
def on_mouse_press(x, y, button, modifiers):
if hasattr(self, 'last_mouse_release'):
if (x, y, button) == self.last_mouse_release[:-1]:
"""Same place, same button"""
if time.clock() - self.last_mouse_release[-1] < 0.2:
print "Double-click"
发布于 2014-04-12 16:42:44
从源代码来看,pyglet似乎使用time.time()度量了单击之间的时间。
为了描述我说的内容,这里有一个从pyglet源代码中摘录的小片段:
t = time.time()
if t - self._click_time < 0.25:
self._click_count += 1
else:
self._click_count = 1
self._click_time = time.time()
(函数的完整版本可以找到这里。请注意,在完整版本中,代码是针对选择文本的。)
源代码还提示使用GUI工具包,以便监视单击事件。
https://stackoverflow.com/questions/22968164
复制相似问题