python - 同一程序上的 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' is not defined
但是,我已经命名了 circle_thread 并分配了它。我已经仔细检查了拼写,但我一生都无法弄清楚是什么导致它跳闸。任何人都可以查看代码并帮助我查看和理解我搞砸的地方吗?
第 85 行是 circle_thread.start()
自动点击器完美运行。它只是键盘部分不起作用并引发错误和/或使脚本崩溃。
解决方案
你拼错了。cirlce_thread
显然。再次检查。
推荐阅读
- r - 使用加权 t 检验汇总多列
- sql - 具有多个条目的多个项目的组内最接近的日期时间匹配
- html - 如何将内容移动到 flex 列的左侧
- flutter - Flutter 错误,渲染库捕获的异常
- android - 在 TextInputLayout 中使用 app:helperTextColor
- android - 如何修复我在 android studio 上的 gradle 构建错误?
- javascript - jquery animate 在 chrome 中比 firefox 慢
- javascript - 关闭安全性较低的应用程序的 smtpjs
- oracle - 查询以计算来自不同表的值
- r - 在闪亮的应用程序中基于一个动作按钮隐藏和显示绘图