按下鼠标即可打印 [英] Print while mouse pressed

查看:62
本文介绍了按下鼠标即可打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyMouse(Event)来检测是否按下了鼠标按钮:

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

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"),直到不再按下鼠标为止……我尝试:

This works so far, but now i want to loop print("click") until mouse isnt pressed anymore ... i tried:

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")

有人可以帮助我吗?谢谢!

Someone can help me? Thanks!

推荐答案

我认为,正如Oli在他的评论中指出的那样,当按住鼠标按钮时,点击不会持续不断,因此您必须print循环播放.在同一线程上运行while循环可防止在释放鼠标时触发click事件,因此,我想到的唯一方法就是从单独的线程开始print("click").

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.

我不是Python程序员,但是我有一个可以在我的机器上运行的工具(Windows 8.1上为Python 2.7)

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()

这篇关于按下鼠标即可打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆