简体   繁体   English

Python 自动答题器,如何监听鼠标事件

[英]Python auto clicker, how to listen for mouse events

I can't figure out how to make auto clicks start when I press the left mouse button and stop when I release it.我不知道如何在按下鼠标左键时开始自动点击并在松开鼠标时停止。 Maybe someone knows how to solve it?也许有人知道如何解决?

Perhaps with the help of pynput it is not advisable to do this, but it is better to use pyautogui , or there are some other solutions.也许在pynput的帮助下这样做并不可取,但最好使用pyautogui ,或者还有其他一些解决方案。

# importing time and threading
import time
import threading
from pynput.mouse import Button, Controller

# pynput.keyboard is used to watch events of
# keyboard for start and stop of auto-clicker
from pynput.keyboard import Listener, KeyCode


# four variables are created to
# control the auto-clicker
delay = 0.277
button = Button.left
start_stop_key = KeyCode(char='+') #The left mouse button should be here
stop_key = KeyCode(char='-')


# threading.Thread is used
# to control clicks
class ClickMouse(threading.Thread):

    # delay and button is passed in class
    # to check execution of auto-clicker
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False

    # method to check and run loop until
    # it is true another loop will check
    # if it is set to true or not,
    # for mouse click it set to button
    # and delay.
    def run(self):
        while self.program_running:
            while self.running:
                mouse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)


# instance of mouse controller is created
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()


# on_press method takes
# key as argument
def on_press(key):
    # start_stop_key will stop clicking
    # if running flag is set to true
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
            print("click end")
        else:
            click_thread.start_clicking()
            print("click start")

    # here exit method is called and when
    # key is pressed it terminates auto clicker
    elif key == stop_key:
        click_thread.exit()
        listener.stop()


with Listener(on_press=on_press) as listener:
    listener.join()

I searched for a solution but didn't understand anything.我搜索了一个解决方案,但什么都不懂。

I found a solution.我找到了解决办法。

It turned out that using the left mouse button when it is pressed is not the best solution, but rather not working.事实证明,在按下鼠标左键时使用鼠标左键并不是最好的解决方案,而是行不通。

You need to use an unoccupied key, in my case it is the middle mouse key.您需要使用一个未占用的键,在我的例子中是鼠标中键。 I replaced the function on_press with the function on_click , which was presented in the pynput documentation, but thereby lost the functionality of completing the script on the key.我用 pynput 文档中提供的 function on_press替换了pynput on_click ,但因此失去了在键上完成脚本的功能。

Here is the code:这是代码:

# importing time and threading
import time
import threading
from pynput.mouse import Button, Controller
from pynput import mouse


delay = 0.277
button = Button.left


class ClickMouse(threading.Thread):

    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False


    def run(self):
        while self.program_running:
            while self.running:
                muse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)


# instance of mouse controller is created
muse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()


def on_click(x, y, button, pressed):
    if button == button.middle:
        if pressed:
            click_thread.start_clicking()
            print("click start")
        elif not pressed:
            click_thread.stop_clicking()
            print("click end")


with mouse.Listener(on_click=on_click) as listener:
    listener.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM