Pynput键按下积压

时间:2019-05-20 00:38:26

标签: python pyautogui pynput

(注意:编程初学者,也有堆栈溢出初学者,对不起,如果我做错了什么)

我正在尝试编写一个python(如果有用的话,是python 3.7.3)程序,当按住反斜杠(“ \”)键时,该程序将向左击按钮发送垃圾邮件(用于在线游戏。我将绑定鼠标按钮指向“ \”,这样就不必检测鼠标左键,而会遇到程序检测鼠标点击的问题。 我的代码当前有效,但是当释放“ \”时,根据其按住时间,可能需要几秒钟来停止单击。

这是我的代码:

from pynput.keyboard import Key, Listener
import pyautogui


def key_down(key):
    if str(key) == "'\\\\'":
        pyautogui.click()

def key_up(key):
    if str(key) == "'\\\\'":
        print('key has been lifted')

with Listener(on_press=key_down,on_release=key_up) as l:
    l.join()

我做错什么了吗?有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

对于将来遇到相同问题的任何人:idk如何解决积压的原始问题,但是通过线程解决了。这是我的代码:

注意:“'\\\\'”用于检查是否按下了反斜杠键,但是pynput模块将其存储为“'\\'”,因此由于反斜杠是转义字符,因此需要将其键入“'\\\\'”,使其实际上为“'\\'”

from pynput.keyboard import Key, Listener
import pyautogui
from threading import Thread

shouldClick = False

def click():
    while True:
        while shouldClick:
            pyautogui.click()


def key_down(key):
    global shouldClick
    if str(key) == "'\\\\'":
        shouldClick = True


def key_up(key):
    global shouldClick
    if str(key) == "'\\\\'":
        shouldClick = False
        print('key has been lifted')


def listen():
    with Listener(on_press=key_down,on_release=key_up) as l:
        l.join()


listenThread = Thread(target=listen)
clickThread = Thread(target=click)

listenThread.start()
clickThread.start()

(可选),如果您想将点击限制提高到极限,则可以添加以下代码:

clickThread1 = Thread(target=click)
clickThread2 = Thread(target=click)
clickThread3 = Thread(target=click)
clickThread4 = Thread(target=click)
clickThread5 = Thread(target=click)
clickThread6 = Thread(target=click)
clickThread7 = Thread(target=click)
clickThread8 = Thread(target=click)
clickThread9 = Thread(target=click)
clickThread10 = Thread(target=click)
clickThread11 = Thread(target=click)
clickThread12 = Thread(target=click)
clickThread13 = Thread(target=click)
clickThread14 = Thread(target=click)
clickThread15 = Thread(target=click)
clickThread16 = Thread(target=click)
clickThread17 = Thread(target=click)
clickThread18 = Thread(target=click)
clickThread19 = Thread(target=click)
clickThread20 = Thread(target=click)
clickThread21 = Thread(target=click)
clickThread22 = Thread(target=click)
clickThread23 = Thread(target=click)
clickThread24 = Thread(target=click)
clickThread25 = Thread(target=click)
clickThread26 = Thread(target=click)
clickThread27 = Thread(target=click)
clickThread28 = Thread(target=click)
clickThread29 = Thread(target=click)
clickThread30 = Thread(target=click)
clickThread31 = Thread(target=click)
clickThread32 = Thread(target=click)
clickThread33 = Thread(target=click)
clickThread34 = Thread(target=click)
clickThread35 = Thread(target=click)
clickThread36 = Thread(target=click)
clickThread37 = Thread(target=click)
clickThread38 = Thread(target=click)
clickThread39 = Thread(target=click)

clickThread1.start()
clickThread2.start()
clickThread3.start()
clickThread4.start()
clickThread5.start()
clickThread6.start()
clickThread7.start()
clickThread8.start()
clickThread9.start()
clickThread10.start()
clickThread11.start()
clickThread12.start()
clickThread13.start()
clickThread14.start()
clickThread15.start()
clickThread16.start()
clickThread17.start()
clickThread18.start()
clickThread19.start()
clickThread20.start()
clickThread21.start()
clickThread22.start()
clickThread23.start()
clickThread24.start()
clickThread25.start()
clickThread26.start()
clickThread27.start()
clickThread28.start()
clickThread29.start()
clickThread30.start()
clickThread31.start()
clickThread32.start()
clickThread33.start()
clickThread34.start()
clickThread35.start()
clickThread36.start()
clickThread37.start()
clickThread38.start()
clickThread39.start()