Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 鼠标按下时打印_Python_Loops_Mouse - Fatal编程技术网

Python 鼠标按下时打印

Python 鼠标按下时打印,python,loops,mouse,Python,Loops,Mouse,我正在使用PyMouse(事件)检测是否按下鼠标按钮: 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:

我正在使用PyMouse(事件)检测是否按下鼠标按钮:

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()
到目前为止,这是可行的,但现在我想循环
打印(“单击”)
,直到不再按下鼠标。。。我试过:

def click(self, x, y, button, press):
    if button == 1:
        if press:
            do = 1
            while do == 1:
                print("down")
                if not press:
                    do = 0
还有smth。比如:

while press:
    print("click")

有人能帮我吗?谢谢

我认为正如奥利在他的评论中指出的那样,当按下鼠标按钮时,不会有持续不断的点击流,因此你必须让
打印
。让
while
循环在同一线程上运行可以防止鼠标释放时触发单击事件,因此我能想到的实现您所追求的目标的唯一方法是从单独的线程打印(“单击”)

我不是Python程序员,但我有一个在我的机器上工作的尝试(Windows8.1上的Python 2.7):


我不认为你可以,因为当你按下按钮时,鼠标本身并没有发出持续不断的“点击”。这是一个机械“问题”,与软件无关。编辑:您应该尝试改用操纵杆;)@Aaroniker-没问题,很高兴收到我关于StackOverflow的第一个答案。我偷窥太久了!
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()