鼠标录制程序

简介: 【7月更文挑战第28天】

image.png

这个项目是一个鼠标操作录制和回放工具,它有以下作用和意义:

自动化重复任务:通过录制和回放鼠标操作,可以自动化一些重复性的任务,比如网页测试、游戏操作等。
提高效率:对于需要频繁进行相同鼠标操作的用户,使用这个工具可以显著提高工作效率。
学习和研究:对于研究用户行为或进行人机交互研究的用户,这个工具可以作为研究工具,帮助他们分析和理解用户的鼠标操作习惯。
辅助功能:对于有特殊需求的用户,比如需要模拟鼠标操作进行辅助功能测试,这个工具可以提供帮助。

import os
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pyautogui
import time
from pynput import mouse


class MouseRecorder:
    def __init__(self, root):
        self.root = root
        self.root.title("鼠标操作录制与执行")

        self.recording = False
        self.operations = []
        self.script_file = None

        # UI组件
        self.start_button = tk.Button(root, text="开始录制", command=self.start_recording)
        self.start_button.pack()

        self.stop_button = tk.Button(root, text="结束录制", command=self.stop_recording, state="disabled")
        self.stop_button.pack()

        self.play_button = tk.Button(root, text="启动执行", command=self.start_playing, state="disabled")
        self.play_button.pack()

        self.stop_program_button = tk.Button(root, text="停止程序", command=self.stop_program)
        self.stop_program_button.pack()

        self.select_script_button = tk.Button(root, text="选择脚本", command=self.select_script)
        self.select_script_button.pack()

        self.times_label = tk.Label(root, text="重复执行次数:")
        self.times_label.pack()

        self.times_entry = tk.Entry(root)
        self.times_entry.pack()

        self.interval_label = tk.Label(root, text="执行间隔时间(秒):")
        self.interval_label.pack()

        self.interval_entry = tk.Entry(root)
        self.interval_entry.pack()

    def start_recording(self):
        self.recording = True
        self.operations = []
        self.start_button.config(state="disabled")
        self.stop_button.config(state="normal")
        self.play_button.config(state="normal")
        messagebox.showinfo("提示", "开始录制鼠标操作")

        # 定义鼠标事件监听函数
        def on_move(x, y):
            if self.recording:
                self.operations.append(f"move_to {x} {y}")

        def on_click(x, y, button, pressed):
            if self.recording and pressed:  # Only record when the button is pressed
                action = "left" if button == mouse.Button.left else "right"
                self.operations.append(f"{action}_click {x} {y}")

        # 监听鼠标事件
        self.listener = mouse.Listener(on_move=on_move, on_click=on_click)
        self.listener.start()


    def stop_recording(self):
        # 停止监听鼠标事件
        if hasattr(self, 'listener'):
            self.listener.stop()
        self.listener.join()

        self.recording = False
        # 检查是否有操作被记录
        if not self.operations:
            messagebox.showerror("错误", "没有记录到任何操作")
            return

        self.start_button.config(state="normal")
        self.stop_button.config(state="disabled")

        pyautogui.mousemove = None
        pyautogui.mouseclick = None

        self.script_file = f"mouse_script_{time.strftime('%Y%m%d%H%M%S')}.txt"
        with open(self.script_file, "w") as f:
            for op in self.operations:
                f.write(op + "\n")
        messagebox.showinfo("提示", "录制结束,脚本已保存")

        # 打印脚本文件路径,用于调试
        print(f"Script file created: {self.script_file}")

    def start_playing(self):
        try:
            times = int(self.times_entry.get())
            interval = float(self.interval_entry.get())
        except ValueError:
            messagebox.showerror("错误", "请输入有效的数字")
            return

        self.script_file = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
        if not self.script_file:  # 确保文件路径不是空的
            messagebox.showerror("错误", "未选择文件")
            return
            # 确保文件存在且不为空
        if not os.path.isfile(self.script_file) or os.path.getsize(self.script_file) == 0:
            messagebox.showerror("错误", "选择的文件不存在或为空")
            return

        with open(self.script_file, "r") as f:
            operations = f.read().splitlines()
        # 打印操作列表,用于调试
        print(f"Operations to play: {operations}")


        def play_operations():
            print(operations)
            for _ in range(times):
                for op in operations:
                    command, *args = op.split()

                    if command == "move_to":
                        pyautogui.moveTo(int(args[0]), int(args[1]))
                    elif command == "click":
                        pyautogui.click()
                    elif command == "right_click":
                        pyautogui.rightClick()
                    time.sleep(interval)

        self.play_button.config(state="disabled")
        self.stop_program_button.config(state="normal")
        # messagebox.showinfo("提示", "开始执行鼠标操作")

        play_operations()

    def stop_program(self):
        messagebox.showinfo("提示", "程序已停止")
        self.root.destroy()

    def select_script(self):
        self.script_file = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
        if self.script_file:
            self.times_entry.delete(0, tk.END)
            self.times_entry.insert(0, "1")
            self.interval_entry.delete(0, tk.END)
            self.interval_entry.insert(0, "1")

            self.play_button.config(state="normal")

if __name__ == "__main__":
    root = tk.Tk()
    app = MouseRecorder(root)
    root.mainloop()
目录
相关文章
支付宝 - 支付宝怎么解除自动续费?
支付宝 - 支付宝怎么解除自动续费?
8195 1
支付宝 - 支付宝怎么解除自动续费?
|
8月前
|
存储 人工智能 安全
阿里云服务器购买之后如何申请发票?发票申请流程及常见问题解答
当我们完成阿里云服务器的购买后,如何顺利申请发票便成为众多企业用户关注的重点问题。特别是对于初次接触阿里云服务器的用户而言,发票申请流程可能并不熟悉。本文将为大家介绍在购买阿里云服务器后如何申请发票,以及在申请过程中可能遭遇的各类常见问题,助力大家轻松完成发票申请。
1295 6
|
数据可视化 Java 测试技术
Git Flow 现代实操指南含从代码提交到 CI/CD 全流程的实用技巧与长尾关键词解析 Git Flow
本指南结合现代技术趋势,详解Git Flow工作流,涵盖GitHub Actions自动化、Conventional Commits规范、Gitmoji可视化等内容,助你实现代码到CI/CD的全流程管理,提升团队开发效率与代码质量。
837 2
|
传感器 人工智能 监控
AI与物联网的融合:开启智能化未来的新篇章
AI与物联网的融合:开启智能化未来的新篇章
2140 96
|
SQL JavaScript 前端开发
Github 2024-08-05 开源项目周报 Top15
根据 Github Trendings 的统计,本周(2024年8月5日统计)共有15个项目上榜。以下是根据开发语言汇总的项目数量: - Go 项目:4个 - JavaScript 项目:3个 - Python 项目:3个 - Java 项目:2个 - TypeScript 项目:2个 - C 项目:1个 - Shell 项目:1个 - Dockerfile 项目:1个 - 非开发语言项目:1个
901 3
|
存储 安全 Android开发
怎样找回删除的照片?照片恢复完整教程
不小心删除了重要的照片会让人头疼,幸运的是,很多时候我可以进行照片恢复。今天会给大家讨论一下删除照片恢复的概率,并且提供几个有效的照片恢复方法,帮助大家应对照片丢失问题。
怎样找回删除的照片?照片恢复完整教程
|
Python Windows
moviepy:基于 ffmpeg 的视频处理模块
moviepy:基于 ffmpeg 的视频处理模块
1052 0
|
前端开发 NoSQL JavaScript
Websocket 替代方案:如何使用 Firestore 监听实时事件
Websocket 替代方案:如何使用 Firestore 监听实时事件
|
小程序 安全
Fiddler抓取小程序后端请求导入AppScan扫描快捷方法
【8月更文挑战第26天】这是一种利用 Fiddler 捕获小程序后端请求并导入 AppScan 进行安全扫描的方法。首先安装配置 Fiddler 并设置手机代理,通过 Fiddler 捕获请求;接着导出这些请求为 .saz 文件,并在 AppScan 中导入此文件,配置扫描参数后启动扫描以检测安全漏洞。操作时需确保有合法授权并遵循相关法规。尽管不同版本软件操作细节可能有所不同,但整体流程类似。
829 1
|
传感器 机器学习/深度学习 数据采集
无人飞行器的技术工作原理
无人飞行器(UAV,即Unmanned Aerial Vehicle)的工作原理涉及多个复杂的系统和技术。
1220 7

热门文章

最新文章