鼠标录制程序

简介: 【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()
目录
相关文章
|
Windows
12.3 实现模拟鼠标录制回放
本节将向读者介绍如何使用键盘鼠标操控模拟技术,键盘鼠标操控模拟技术是一种非常实用的技术,可以自动化执行一些重复性的任务,提高工作效率,在Windows系统下,通过使用各种键盘鼠标控制函数实现动态捕捉和模拟特定功能的操作。有时我们经常需要进行重复性的鼠标操作,例如繁琐的点击、拖拽。这些任务可能消耗大量时间和精力,为了解决这个问题,可自行设计并实现一个简单而强大的鼠标录制回放工具,旨在帮助用户轻松录制鼠标动作,通过借助鼠标录制回放工具,用户可以轻松实现自动化操作,从而解放双手。
12.3 实现模拟鼠标录制回放
|
5月前
|
监控 Linux iOS开发
LabVIEW监控操纵杆,键盘或鼠标
LabVIEW监控操纵杆,键盘或鼠标
54 0
|
5月前
|
JSON 数据格式 Python
优秀!Python版按键精灵,电脑鼠标、键盘手势动作一键复制操作,优雅极了!
优秀!Python版按键精灵,电脑鼠标、键盘手势动作一键复制操作,优雅极了!
194 0
【屏幕快捷截图无法弹出编辑框】
【屏幕快捷截图无法弹出编辑框】
103 0
【屏幕快捷截图无法弹出编辑框】
|
数据采集
LabVIEW采集鼠标、键盘数据
鼠标、键盘输入数据是应用程序最基本的交互操作方式,本篇博文将分享LabVIEW如何获取这些数据的方法。
按下鼠标右键的时候如何截图
点击右键,出现要操作的菜单项 鼠标移动到,想要操作的菜单项上 这时候按下鼠标左键 最后再按下crtl+a,有的电脑是crtl+alt+a,就截图成功了,这时候是所有屏幕的截图 打开word文件,或者打开画图工具,ctrl+v,就把整张截图粘贴上面了 可以跟进自己需求,再次截图,选择自己想要的部分
148 0
按下鼠标右键的时候如何截图
|
C++
Qt界面优化:鼠标双击特效
Qt界面优化:鼠标双击特效
239 1
Qt界面优化:鼠标双击特效
|
Windows
Camtasia2023电脑版屏幕录像和编辑软件
Camtasia屏幕录像机使用方便,视频编辑功能强大。与iSpring一样,可以捕获屏幕的任何部分,使用网络摄像头录制和插入视频,并支持音频、图像和文本的截屏。Camtasia包括绿屏在内的全套动画和视频效果。为了让课程更沉浸,你可以添加自己的音乐,或者从Camtasia在免版税音乐库中选择曲目。
166 0
|
编解码 算法 数据安全/隐私保护
TechSmith Camtasia Studio2022专门录制屏幕动作的工具
Camtasia Studio是TechSmith旗下一款专门录制屏幕动作的工具,它能在任何颜色模式下轻松地记录屏幕动作,包括影像、音效、鼠标移动轨迹、解说声音等等。
342 0