Print while mouse pressed

前端 未结 1 474
梦谈多话
梦谈多话 2020-12-12 00:19

i am using PyMouse(Event) for detecting if mouse button is pressed:

from pymouse import PyMouseEvent

class DetectMouseClick(PyMouseEvent):
    def __init__(         


        
相关标签:
1条回答
  • 2020-12-12 01:12

    I think as Oli points out in his comment there isn't a constant stream of clicks when the mouse button is held down so you'll have to have the print in a loop. Having the while loop running on the same thread prevents the click event firing when the mouse is released so the only way I can think of to achieve what you are after is to print("click") from a separate thread.

    I'm not a Python programmer but I've had a stab which works on my machine (Python 2.7 on Windows 8.1):

    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()
    
    0 讨论(0)
提交回复
热议问题