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

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

准备工作

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

python 复制代码
pip install PyQt5

程序运行界面:

程序代码:

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

常量定义文件(constants.py

python 复制代码
# 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

python 复制代码
# variables.py
# 变量定义文件,用于统一管理系统中的变量

# 系统状态变量
pressing = False  # 按键模拟状态
clicking = False  # 鼠标模拟状态
mouse_position = None  # 鼠标当前位置

主程序文件(main.py

python 复制代码
# 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_mouse

import 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_listening
        self.key.focusOutEvent = self.stop_key_listening

        self.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 = True
            threading.Thread(target=self.press_loop).start()
            self.showMinimized()

    def stop_pressing(self):
        variables.pressing = False
        self.log(self.key_log, "按键已停止")

    def press_loop(self):
        key_count = 0
        while variables.pressing:
            try:
                if self.key_type.currentText() == "单个按键":
                    self.perform_key_press()
                    self.stop_pressing()
                    break
                elif self.key_type.currentText() == "多次按键" and key_count < self.key_count.value():
                    self.perform_key_press()
                    key_count += 1
                elif 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 = True
            threading.Thread(target=self.click_loop).start()
            self.showMinimized()

    def stop_clicking(self):
        variables.clicking = False
        self.log(self.mouse_log, "点击已停止")

    def click_loop(self):
        click_count = 0
        mouse_controller = pynput_mouse.Controller()
        while variables.clicking:
            try:
                if self.click_type.currentText() == "单次点击":
                    self.perform_click(mouse_controller)
                    self.stop_clicking()
                    break
                elif self.click_type.currentText() == "多次点击" and click_count < self.click_count.value():
                    self.perform_click(mouse_controller)
                    click_count += 1
                elif 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.position
        self.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_CLASS

    app = 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中会发布出来。

相关推荐
修己xj8 小时前
Anki:让记忆更高效、更智能的开源力量
开源
冷雨夜中漫步8 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴8 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再8 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
m0_7369191010 小时前
C++代码风格检查工具
开发语言·c++·算法
喵手10 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_9449347310 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy10 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
黎雁·泠崖11 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_7634724612 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法