我正在使用 PyMouse(Event) 来检测鼠标按钮是否被按下:
from pymouse import PyMouseEvent
class DetectMouseClick(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
def click(self, x, y, button, press):
if button == 1:
if press:
print("click")
else:
self.stop()
O = DetectMouseClick()
O.run()
到目前为止这是有效的,但现在我想循环 print("click") 直到不再按下鼠标...我试过:
def click(self, x, y, button, press):
if button == 1:
if press:
do = 1
while do == 1:
print("down")
if not press:
do = 0
还有……喜欢:
while press:
print("click")
有人可以帮助我吗?谢谢!
请您参考如下方法:
我认为正如 Oli 在他的评论中指出的那样,当按住鼠标按钮时不会有持续的点击流,因此您必须在循环中使用 print。在同一线程上运行 while 循环可以防止在释放鼠标时触发点击事件,所以我能想到的唯一方法是 print("click") 来自一个单独的线程。
我不是 Python 程序员,但我有一个可以在我的机器上运行的工具(Windows 8.1 上的 Python 2.7):
from pymouse import PyMouseEvent
from threading import Thread
class DetectMouseClick(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
def print_message(self):
while self.do == 1:
print("click")
def click(self, x, y, button, press):
if button == 1:
if press:
print("click")
self.do = 1
self.thread = Thread(target = self.print_message)
self.thread.start()
else:
self.do = 0
print("end")
else:
self.do = 0
self.stop()
O = DetectMouseClick()
O.run()
