我正在使用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")
有人可以帮帮我吗?谢谢!
答案 0 :(得分:2)
我认为,正如Oli在评论中指出的那样,当按住鼠标按钮时,没有持续的点击流,因此您必须在循环中拥有print
。让while
循环在同一个线程上运行可以防止在释放鼠标时触发click事件,这样我能想到的唯一方法就是从一个单独的线程中print("click")
来实现你所追求的目标。 / p>
我不是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()