当前位置: 首页 > news >正文

PyQT5 键盘模拟/鼠标连点器的实现

近来在玩一个游戏,找不到合适的鼠标连点器,不是有广告就是功能太复杂,自己写了一个,分享出来,如果有需要的可以自行运行研究。

准备工作

Python版本:Python 3.12.3;运行前确保pyQT5已经安装:

pip install PyQt5

程序运行界面:
在这里插入图片描述在这里插入图片描述

程序代码:

通过引入单独的常量和变量文件,并为每个元素增加中文注释来实现界面语句、变量和常量的统一规划。代码:

常量定义文件(constants.py)

# constants.py
# 常量定义文件,用于统一管理系统中的常量# 界面文本常量
WINDOW_TITLE = "模拟器"  # 窗口标题# 按键模拟标签
KEY_TAB_TITLE = "按键模拟"
KEY_INTERVAL_LABEL = "按键间隔(秒):"
KEY_TYPE_LABEL = "按键类型:"
KEY_LABEL = "按键:"
KEY_COUNT_LABEL = "按键次数 (适用于多次按键类型):"
KEY_LOG_LABEL = "按键日志:"# 鼠标模拟标签
MOUSE_TAB_TITLE = "鼠标模拟"
CLICK_INTERVAL_LABEL = "点击间隔(秒):"
CLICK_TYPE_LABEL = "点击类型:"
CLICK_COUNT_LABEL = "点击次数 (适用于多次点击类型):"
MOUSE_POSITION_LABEL = "鼠标位置:"
MOUSE_LOG_LABEL = "鼠标日志:"# 按钮标签
START_KEY_BUTTON = "开始 (F10)"
STOP_KEY_BUTTON = "停止 (F11)"
START_MOUSE_BUTTON = "开始 (F9)"
STOP_MOUSE_BUTTON = "停止 (F12)"# 错误消息
ERROR_MESSAGE = "错误: "

变量定义文件(variables.py)

# variables.py
# 变量定义文件,用于统一管理系统中的变量# 系统状态变量
pressing = False  # 按键模拟状态
clicking = False  # 鼠标模拟状态
mouse_position = None  # 鼠标当前位置

主程序文件(main.py)

# main.py
# 主程序文件,包含程序的主要逻辑import sys
import ctypes
import threading
import time
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QLineEdit, QTabWidget, QComboBox, QTextEdit, QDoubleSpinBox, QSpinBox, QGridLayout
from PyQt5.QtCore import Qt
import keyboard
from pynput import keyboard as pynput_keyboard, mouse as pynput_mouseimport constants  # 导入常量定义文件
import variables  # 导入变量定义文件class KeyMouseSimulator(QMainWindow):def __init__(self):super().__init__()self.setup_ui()self.bind_hotkeys()def setup_ui(self):self.setWindowTitle(constants.WINDOW_TITLE)  # 设置窗口标题tab_widget = QTabWidget(self)self.setCentralWidget(tab_widget)self.key_tab = QWidget()self.mouse_tab = QWidget()tab_widget.addTab(self.key_tab, constants.KEY_TAB_TITLE)  # 设置按键模拟标签tab_widget.addTab(self.mouse_tab, constants.MOUSE_TAB_TITLE)  # 设置鼠标模拟标签self.setup_key_tab()self.setup_mouse_tab()def setup_key_tab(self):layout = QGridLayout(self.key_tab)layout.addWidget(QLabel(constants.KEY_INTERVAL_LABEL), 0, 0)  # 按键间隔标签self.key_interval = QDoubleSpinBox()self.key_interval.setValue(0.1)layout.addWidget(self.key_interval, 0, 1)layout.addWidget(QLabel(constants.KEY_TYPE_LABEL), 1, 0)  # 按键类型标签self.key_type = QComboBox()self.key_type.addItems(["单个按键", "多次按键", "持续按键"])self.key_type.currentIndexChanged.connect(self.update_key_type)layout.addWidget(self.key_type, 1, 1)layout.addWidget(QLabel(constants.KEY_LABEL), 2, 0)  # 按键标签self.key = QLineEdit("a")layout.addWidget(self.key, 2, 1)self.key.focusInEvent = self.start_key_listeningself.key.focusOutEvent = self.stop_key_listeningself.key_count_label = QLabel(constants.KEY_COUNT_LABEL)  # 按键次数标签self.key_count = QSpinBox()self.key_count.setValue(1)layout.addWidget(QPushButton(constants.START_KEY_BUTTON, clicked=self.start_pressing), 4, 0)  # 开始按钮layout.addWidget(QPushButton(constants.STOP_KEY_BUTTON, clicked=self.stop_pressing), 4, 1)  # 停止按钮self.key_log = QTextEdit()self.key_log.setReadOnly(True)layout.addWidget(self.key_log, 5, 0, 1, 2)def setup_mouse_tab(self):layout = QGridLayout(self.mouse_tab)layout.addWidget(QLabel(constants.CLICK_INTERVAL_LABEL), 0, 0)  # 点击间隔标签self.click_interval = QDoubleSpinBox()self.click_interval.setValue(0.1)layout.addWidget(self.click_interval, 0, 1)layout.addWidget(QLabel(constants.CLICK_TYPE_LABEL), 1, 0)  # 点击类型标签self.click_type = QComboBox()self.click_type.addItems(["单次点击", "多次点击", "持续点击"])self.click_type.currentIndexChanged.connect(self.update_click_type)layout.addWidget(self.click_type, 1, 1)self.click_count_label = QLabel(constants.CLICK_COUNT_LABEL)  # 点击次数标签self.click_count = QSpinBox()self.click_count.setValue(1)layout.addWidget(QPushButton(constants.START_MOUSE_BUTTON, clicked=self.start_clicking), 3, 0)  # 开始按钮layout.addWidget(QPushButton(constants.STOP_MOUSE_BUTTON, clicked=self.stop_clicking), 3, 1)  # 停止按钮layout.addWidget(QLabel(constants.MOUSE_POSITION_LABEL), 4, 0)  # 鼠标位置标签self.mouse_position_var = QLineEdit("未获取")self.mouse_position_var.setReadOnly(True)layout.addWidget(self.mouse_position_var, 4, 1)self.mouse_log = QTextEdit()self.mouse_log.setReadOnly(True)layout.addWidget(self.mouse_log, 5, 0, 1, 2)def bind_hotkeys(self):self.key_type.setCurrentIndex(0)self.click_type.setCurrentIndex(0)keyboard.add_hotkey('F10', self.start_pressing)keyboard.add_hotkey('F11', self.stop_pressing)keyboard.add_hotkey('F9', self.start_clicking)keyboard.add_hotkey('F12', self.stop_clicking)keyboard.add_hotkey('F8', self.get_mouse_position)def update_key_type(self):if self.key_type.currentText() == "多次按键":self.key_tab.layout().addWidget(self.key_count_label, 3, 0)self.key_tab.layout().addWidget(self.key_count, 3, 1)else:self.key_tab.layout().removeWidget(self.key_count_label)self.key_tab.layout().removeWidget(self.key_count)self.key_count_label.setParent(None)self.key_count.setParent(None)def update_click_type(self):if self.click_type.currentText() == "多次点击":self.mouse_tab.layout().addWidget(self.click_count_label, 2, 0)self.mouse_tab.layout().addWidget(self.click_count, 2, 1)else:self.mouse_tab.layout().removeWidget(self.click_count_label)self.mouse_tab.layout().removeWidget(self.click_count)self.click_count_label.setParent(None)self.click_count.setParent(None)def start_key_listening(self, event):self.key_listener = pynput_keyboard.Listener(on_press=self.on_key_press)self.key_listener.start()def stop_key_listening(self, event):if hasattr(self, 'key_listener'):self.key_listener.stop()def on_key_press(self, key):try:self.key.setText(key.char)except AttributeError:self.key.setText(str(key).replace("Key.", ""))self.key.setFocus()def start_pressing(self):if not variables.pressing:variables.pressing = Truethreading.Thread(target=self.press_loop).start()self.showMinimized()def stop_pressing(self):variables.pressing = Falseself.log(self.key_log, "按键已停止")def press_loop(self):key_count = 0while variables.pressing:try:if self.key_type.currentText() == "单个按键":self.perform_key_press()self.stop_pressing()breakelif self.key_type.currentText() == "多次按键" and key_count < self.key_count.value():self.perform_key_press()key_count += 1elif self.key_type.currentText() == "持续按键":self.perform_key_press()time.sleep(self.key_interval.value())except Exception as e:self.log(self.key_log, f"{constants.ERROR_MESSAGE}{e}")self.stop_pressing()def perform_key_press(self):keyboard.press_and_release(self.key.text())self.log(self.key_log, f"按键: {self.key.text()}")def start_clicking(self):if not variables.clicking:variables.clicking = Truethreading.Thread(target=self.click_loop).start()self.showMinimized()def stop_clicking(self):variables.clicking = Falseself.log(self.mouse_log, "点击已停止")def click_loop(self):click_count = 0mouse_controller = pynput_mouse.Controller()while variables.clicking:try:if self.click_type.currentText() == "单次点击":self.perform_click(mouse_controller)self.stop_clicking()breakelif self.click_type.currentText() == "多次点击" and click_count < self.click_count.value():self.perform_click(mouse_controller)click_count += 1elif self.click_type.currentText() == "持续点击":self.perform_click(mouse_controller)time.sleep(self.click_interval.value())except Exception as e:self.log(self.mouse_log, f"{constants.ERROR_MESSAGE}{e}")self.stop_clicking()def perform_click(self, mouse_controller):mouse_controller.click(pynput_mouse.Button.left, 1)self.log(self.mouse_log, "鼠标点击")def get_mouse_position(self):mouse_controller = pynput_mouse.Controller()position = mouse_controller.positionself.mouse_position_var.setText(f"{position[0]}, {position[1]}")self.log(self.mouse_log, f"鼠标位置: {position}")def log(self, log_display, message):log_display.append(message + '\n')log_display.verticalScrollBar().setValue(log_display.verticalScrollBar().maximum())def main():# 提高程序优先级ctypes.windll.kernel32.SetPriorityClass(ctypes.windll.kernel32.GetCurrentProcess(), 0x00000080)  # HIGH_PRIORITY_CLASSapp = QApplication(sys.argv)window = KeyMouseSimulator()window.show()sys.exit(app.exec_())if __name__ == "__main__":main()

说明:

  1. 常量定义文件(constants.py):用于统一管理所有的字符串常量和文本常量,每个常量都附有中文注释解释其用途。
  2. 变量定义文件(variables.py):用于统一管理所有的变量状态,如按键和点击状态。
  3. 主程序文件(main.py):主程序逻辑被清晰地组织在这里,并且引用了常量和变量文件中的内容。每个函数都有详细的中文注释,解释其功能。

通过这种方式,所有的界面语句、变量和常量都得到了统一的管理,便于后期的维护和扩展。
详细内容后期在InsCode中会发布出来。

相关文章:

  • 设计模式(七)创建者模式之建造者模式
  • 树莓派4B学习笔记11:PC端网线SSH连接树莓派_网线连接请求超时问题解决
  • 如何在Java中使用正则表达式进行文本处理
  • 【elementui源码解析】如何实现自动渲染md文档-第四篇
  • 监督学习:从数据中学习预测模型的艺术与科学
  • 《C语言程序设计》考试大纲-硕士研究生入学考试
  • 计网重点面试题-TCP三次握手四次挥手
  • 数据分析-相关性
  • CentOS 7 安装部署Cassandra4.1.5
  • Python基础教程(三十):math模块
  • Windows环境部署MySQL_8.4.0 LTS的部署安装、验证连接以及卸载全过程实操手册
  • 链表中环的入口节点
  • JAVA大型医院绩效考核系统源码:​医院绩效考核实施的难点痛点
  • STL——函数对象,谓词
  • VMware虚拟机三种网络模式设置 - Bridged(桥接模式)
  • AngularJS指令开发(1)——参数详解
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • css选择器
  • Fundebug计费标准解释:事件数是如何定义的?
  • Java编程基础24——递归练习
  • Js基础——数据类型之Null和Undefined
  • js中的正则表达式入门
  • k8s如何管理Pod
  • MySQL QA
  • Python实现BT种子转化为磁力链接【实战】
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • Shell编程
  • Spring Boot快速入门(一):Hello Spring Boot
  • STAR法则
  • 关于使用markdown的方法(引自CSDN教程)
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 数组的操作
  • 一份游戏开发学习路线
  • 正则学习笔记
  • const的用法,特别是用在函数前面与后面的区别
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • ​Python 3 新特性:类型注解
  • $(selector).each()和$.each()的区别
  • (2024,Flag-DiT,文本引导的多模态生成,SR,统一的标记化,RoPE、RMSNorm 和流匹配)Lumina-T2X
  • (TOJ2804)Even? Odd?
  • (第三期)书生大模型实战营——InternVL(冷笑话大师)部署微调实践
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (译)计算距离、方位和更多经纬度之间的点
  • (转)JAVA中的堆栈
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .NET 8.0 发布到 IIS
  • .net Application的目录
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .Net的DataSet直接与SQL2005交互