微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pynput 鼠标和键盘在同一个程序

如何解决Pynput 鼠标和键盘在同一个程序

我正在尝试同时使用键盘和鼠标运行 Pynput。我能够让鼠标正确注册和启动/停止和退出程序。但是,我正在努力让键盘正确运行。 ---最终目标是让它可以循环运行并能够打开自动点击功能代码如下:

import time
import threading

from pynput.mouse import Button,Controller as MouseController
from pynput.keyboard import Key,Controller as KeyboardController
from pynput.keyboard import Listener,KeyCode

delay = 0.31
keyboard_delay = 1
button = Button.left
start_stop_key = KeyCode(char=']')
start_stop_cirlces = KeyCode(char='[')
exit_key = KeyCode(char=';')



class ClickMouse(threading.Thread):
    print('Welcome to the ClickMouse Auto Click Bot! \n\nCurrent delay setting for the click function is: {} seconds between clicks \n\nIn order to Start/Stop the Auto Click function- \n-Press the {} key \n\nTo exit the program- \n-Press the {} key \n\nEnjoy!\n\n\n~~Created by CrudeExistence.'.format(delay,start_stop_key,exit_key))
    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:
                mouse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)

class runcirlces(threading.Thread):
    print('The program is running and ready to run cirlces.')
    def __init__(self,delay):
        super(runcirlces,self).__init__()
        self.delay = keyboard_delay
        self.running = False
        self.program_running = True

    def start_circles(self):
        self.running = True

    def stop_circles(self):
        self.running = False

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

    def run(self):
        while self.program_running:
            while self.running:
                keyboard.press('w')
                time.sleep(self.delay)
                keyboard.release('w')
                keybaord.press('d')
                time.sleep(self.delay)
                keybaord.release('d')
                keybaord.press('s')
                time.sleep(self.delay)
                keyboard.release('s')
                keyboard.press('a')
                time.sleep(self.delay)
                keyboard.release('a')
            time.sleep(0.1)

keyboard = KeyboardController()
mouse = MouseController()
click_thread = ClickMouse(delay,button)
cirlce_thread = runcirlces(delay)
click_thread.start()
circle_thread.start()

def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == start_stop_cirlces:
        if circle_thread.running:
            circle_thread.stop_circles()
        else:
            circle_thread.start_circles()
    elif key == exit_key:
        click_thread.exit()
        listener.stop()


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

运行此脚本时出现以下错误

回溯(最近一次调用最后一次):文件 “C:\Users\Admin\Desktop\AutoClickTest.py”,第 85 行,在 circle_thread.start() NameError: name 'circle_thread' 未定义

但是,我已经命名了 circle_thread 并分配了它。我已经仔细检查了拼写,但终生无法弄清楚是什么导致它跳闸。任何人都可以查看代码并帮助我查看并理解我搞砸的地方吗?

第 85 行是 circle_thread.start()

自动点击器运行完美。只是键盘部分无法工作并引发错误或使脚本崩溃。

解决方法

你拼错了。 cirlce_thread 显然。再检查一下。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。