鼠标录制程序

简介: 【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()
目录
相关文章
利用MCI命令进行 播放录制音乐,以及弹出光驱,音量控制获得播放进度等等操作。。。开发必备。
关于MCI的命令以及操作 很复杂 各种宏 这里简单的列举了几个例子  实际开发中 如果用到MCI那么我们去研究微软的 Mmsystem.h头文件、、、  MCI的使用时要先打开设备 然后再操作设备 。
1148 0
|
编解码 算法 数据安全/隐私保护
TechSmith Camtasia Studio2022专门录制屏幕动作的工具
Camtasia Studio是TechSmith旗下一款专门录制屏幕动作的工具,它能在任何颜色模式下轻松地记录屏幕动作,包括影像、音效、鼠标移动轨迹、解说声音等等。
384 0
|
C#
WPF技术触屏上的应用系列(三): 视频播放器的使用及视频播放、播放、暂停、可拖动播放进度效果实现
原文: WPF技术触屏上的应用系列(三): 视频播放器的使用及视频播放、播放、暂停、可拖动播放进度效果实现         去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用。
1115 0
qt屏幕抓图
QPixmap::grabWindow(QApplication::desktop()->winId()); QPixmap有一个grabWindow函数,即可
587 0
|
测试技术 Android开发
cocos2d-x 保持屏幕点亮及自动变灰
很早之前遇到的问题,现在记录一下。有一家Android渠道(抱歉,时间太长了已经记不大清楚是哪一家了 oppo/联想/酷派?)在我们提交新版本时拒绝了,理由是:手机背光状态下,屏幕不会自动变灰。 这里为了测试,我将它改为30秒,这样做有什么影响呢?就是开启任何一个应用,在30秒之内,我如果没有任何的操作,就自动锁屏了。
1091 0
【C大事】第七讲:打开文件对话框及播放音乐(下)
<p><span style="font-size:18px">直接上代码:</span></p> <p></p> <pre name="code" class="cpp"><span style="font-size:18px;">switch(id)     {         case IDC_OK:         {             OPENFILENAME ofn;
917 0
win7自带屏幕录像工具
相信win7自带的屏幕录像工具很多朋友都没用过甚至没有听说过,   但是这款实用的小软件确实非常的好用而且精简。   十分适合记录一些操作,想必视频录像的占用容量大,解释不全面,   这款小工具能够详细的列出每一步的操作,而且会为你的操作自动添加出文字注释,   并且十分的方便,不用再担心操作太快而使观看者看不到的情况了,   他的自带功能简单实用。
864 0

热门文章

最新文章