我如何检查持有的钥匙?

时间:2019-02-14 04:14:31

标签: python-3.x

我是编码的新手,因此决定制作一个随机自动答题器只是为了帮助自己学习。现在我有了它,所以当我按下start_stop_key时,它要么开始连续单击,要么停止。我希望程序仅在按下start_stop_key后按住鼠标左键的情况下才能单击。我将如何去做呢?我完全不知所措。

import time
import threading
import random
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode

delay = random.uniform(0.05,0.15)
button = Button.left
start_stop_key = KeyCode(char='r')
exit_key = KeyCode(char='p')
print("Start key: ",start_stop_key, "\n")
print("Exit key: ",exit_key, "\n")

class ClickMouse(threading.Thread):
    def __init__(self, delay, button):
        super().__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:
                time.sleep(delay)
                mouse.click(self.button)

mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()

def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            print("off","\n")
            click_thread.stop_clicking()
        else:
            print("on","\n")
            click_thread.start_clicking()
    elif key == exit_key:
        click_thread.exit()
        listener.stop()


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

1 个答案:

答案 0 :(得分:0)

好吧,pynput.mouse.Listener帮助我们侦听鼠标事件,函数on_click()接收四个参数(x,y,button,pressed),其中当鼠标按钮被按下时pressedTrue在按住False的同时按下鼠标键。

对于您是要在按住鼠标左键时自动单击,还是要在按住鼠标左键后自动单击,我有点困惑。。 / p>

要在按住鼠标左键后单击:

import time
import threading
import random
from pynput.mouse import Button, Controller, Listener as mouseListener
from pynput.keyboard import Listener as keyListener, KeyCode

class ClickMouse(threading.Thread):
    def __init__(self, delay, button):
        super().__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:
                time.sleep(delay)
                mouse.click(self.button)

def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            print("off","\n")
            mouse_listener.stop()
            click_thread.stop_clicking()
        else:
            print("on","\n")
            mouse_listener.start()
    elif key == exit_key:
        mouse_listener.stop()
        key_listener.stop()
        click_thread.exit()

def on_click(x,y,button,pressed):
    if pressed:
        click_thread.start_clicking()
        mouse_listener.stop()

if __name__ == '__main__':
    delay = random.uniform(0.05,0.15)
    button = Button.left
    start_stop_key = KeyCode(char='r')
    exit_key = KeyCode(char='p')
    print("Start key: ",start_stop_key, "\n")
    print("Exit key: ",exit_key, "\n")

    mouse_listener = mouseListener(on_click=on_click)
    mouse = Controller()
    click_thread = ClickMouse(delay, button)
    click_thread.start()
    with keyListener(on_press=on_press) as key_listener:
        key_listener.join()

在按住鼠标左键的同时单击会有点困难,这是因为释放了物理鼠标,或者程序使鼠标“单击了”(实际上是按下并释放),从而使鼠标侦听器收到了鼠标单击事件,表明鼠标已释放。因此,比较压力机的数量,即压紧和释放是一种可行的方法。由于自动单击始终会同时执行“按下”和“释放”操作,因此当释放的次数比按下的次数多一时,即释放了鼠标。

import time
import threading
import random
from pynput.mouse import Button, Controller, Listener as mouseListener
from pynput.keyboard import Listener as keyListener, KeyCode

class ClickMouse(threading.Thread):
    def __init__(self, delay, button):
        super().__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:
                time.sleep(delay)
                mouse.click(self.button)

def on_press(key): 
    if key == start_stop_key:
        if click_thread.running:
            print("off","\n")
            mouse_listener.stop()
            click_thread.stop_clicking()
        else:
            print("on","\n")
            mouse_listener.start()
    elif key == exit_key:
        mouse_listener.stop()
        key_listener.stop()
        click_thread.exit()

def on_click(x,y,button,pressed):
    global press,release
    if pressed:
        if click_thread.running:
            press += 1
        else:
            click_thread.start_clicking() 
    else:
        if click_thread.running:
            release += 1
            if release > press:
                press,release = 0,0
                click_thread.stop_clicking()

if __name__ == '__main__':
    delay = random.uniform(0.05,0.15)
    button = Button.left
    start_stop_key = KeyCode(char='r')
    exit_key = KeyCode(char='p')
    print("Start key: ",start_stop_key, "\n")
    print("Exit key: ",exit_key, "\n")

    press,release = 0,0
    mouse_listener = mouseListener(on_click=on_click)
    mouse = Controller()
    click_thread = ClickMouse(delay, button)
    click_thread.start()
    with keyListener(on_press=on_press) as key_listener:
        key_listener.join()