按下鼠标时打印

2024-10-01 02:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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")直到鼠标不再按下。。。我试过了:

^{pr2}$

还有smth。比如:

while press:
    print("click")

有人能帮我吗?谢谢!在


Tags: fromselfeventifinitdefbutton鼠标
1条回答
网友
1楼 · 发布于 2024-10-01 02:36:30

我想正如奥利在他的评论中指出的那样,当鼠标按下时,不会有一个连续的点击流,所以你必须循环使用print。在同一个线程上运行while循环可以防止鼠标释放时触发click事件,因此我可以想到的唯一方法是从一个单独的线程print("click")。在

我不是一个Python程序员,但我有一个可以在我的机器上运行的stab(在windows8.1上运行python2.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()

相关问题 更多 >