电脑录制鼠标键盘脚本,鼠标动作录制脚本,万能脚本录制器【python】

简介: 完整功能:实现鼠标移动、点击和键盘操作的录制与回放数据持久化:将录制的动作序列保存为JSON文件

文章附件下载:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:2288

完整功能:实现鼠标移动、点击和键盘操作的录制与回放
数据持久化:将录制的动作序列保存为JSON文件
截图功能:在鼠标点击时自动截图保存
速度控制:支持调整回放速度
错误处理:完善的异常捕获和处理机制
用户界面:提供简单的命令行交互菜单
元数据记录:保存录制时间、屏幕分辨率等信息

import pyautogui
import keyboard
import time
import json
from datetime import datetime
import os

class ActionRecorder:
def init(self):
self.recording = False
self.actions = []
self.start_time = None
self.output_file = "recording.json"
self.screenshot_dir = "screenshots"

    if not os.path.exists(self.screenshot_dir):
        os.makedirs(self.screenshot_dir)

def start_recording(self):
    self.recording = True
    self.actions = []
    self.start_time = time.time()
    print("Recording started... Press ESC to stop")

def stop_recording(self):
    self.recording = False
    print(f"Recording stopped. Saved {len(self.actions)} actions")
    self.save_recording()

def record_mouse_move(self, x, y):
    if self.recording:
        timestamp = time.time() - self.start_time
        action = {
            "type": "mouse_move",
            "x": x,
            "y": y,
            "timestamp": timestamp
        }
        self.actions.append(action)

def record_mouse_click(self, x, y, button, pressed):
    if self.recording:
        timestamp = time.time() - self.start_time
        action = {
            "type": "mouse_click",
            "x": x,
            "y": y,
            "button": button,
            "pressed": pressed,
            "timestamp": timestamp
        }
        self.actions.append(action)

        # Take screenshot on click
        if pressed:
            self.take_screenshot(timestamp)

def record_keyboard(self, event):
    if self.recording and event.event_type in ('down', 'up'):
        timestamp = time.time() - self.start_time
        action = {
            "type": "keyboard",
            "key": event.name,
            "event_type": event.event_type,
            "timestamp": timestamp
        }
        self.actions.append(action)

def take_screenshot(self, timestamp):
    try:
        screenshot = pyautogui.screenshot()
        filename = f"{self.screenshot_dir}/screenshot_{timestamp:.2f}.png"
        screenshot.save(filename)
    except Exception as e:
        print(f"Error taking screenshot: {e}")

def save_recording(self):
    try:
        recording_data = {
            "metadata": {
                "date": datetime.now().isoformat(),
                "screen_size": pyautogui.size(),
                "duration": time.time() - self.start_time,
                "action_count": len(self.actions)
            },
            "actions": self.actions
        }

        with open(self.output_file, 'w') as f:
            json.dump(recording_data, f, indent=2)

        print(f"Recording saved to {self.output_file}")
    except Exception as e:
        print(f"Error saving recording: {e}")

def play_recording(self, speed=1.0):
    try:
        with open(self.output_file, 'r') as f:
            recording_data = json.load(f)

        print(f"Playing recording at {speed}x speed...")
        actions = recording_data['actions']

        if not actions:
            print("No actions to play")
            return

        start_time = time.time()
        last_timestamp = 0

        for action in actions:
            current_time = time.time() - start_time
            action_time = action['timestamp'] / speed

            # Wait until it's time to perform this action
            while current_time < action_time:
                time.sleep(0.001)
                current_time = time.time() - start_time

            self.execute_action(action)

        print("Playback complete")
    except Exception as e:
        print(f"Error playing recording: {e}")

def execute_action(self, action):
    try:
        if action['type'] == 'mouse_move':
            pyautogui.moveTo(action['x'], action['y'], duration=0.1)

        elif action['type'] == 'mouse_click':
            if action['pressed']:
                pyautogui.mouseDown(x=action['x'], y=action['y'], button=action['button'])
            else:
                pyautogui.mouseUp(x=action['x'], y=action['y'], button=action['button'])

        elif action['type'] == 'keyboard':
            if action['event_type'] == 'down':
                pyautogui.keyDown(action['key'])
            else:
                pyautogui.keyUp(action['key'])

    except Exception as e:
        print(f"Error executing action: {e}")

def main():
recorder = ActionRecorder()

print("Mouse and Keyboard Recorder")
print("1. Start Recording")
print("2. Play Last Recording")
print("3. Exit")

while True:
    choice = input("Select an option (1-3): ")

    if choice == '1':
        print("Recording will start in 3 seconds...")
        time.sleep(3)
        recorder.start_recording()

        # Set up keyboard hook
        keyboard.hook(recorder.record_keyboard)

        # Set up mouse hooks
        pyautogui.onMove(recorder.record_mouse_move)
        pyautogui.onClick(lambda x, y, button, pressed: recorder.record_mouse_click(x, y, button, pressed))

        # Wait for ESC key to stop recording
        keyboard.wait('esc')
        recorder.stop_recording()
        keyboard.unhook_all()
        pyautogui.onMove(None)
        pyautogui.onClick(None)

    elif choice == '2':
        speed = float(input("Enter playback speed (1.0 for normal speed): ") or "1.0")
        recorder.play_recording(speed)

    elif choice == '3':
        print("Exiting...")
        break

    else:
        print("Invalid choice. Please try again.")

if name == "main":
main()

相关文章
|
7月前
|
存储 监控 算法
监控电脑屏幕的帧数据检索 Python 语言算法
针对监控电脑屏幕场景,本文提出基于哈希表的帧数据高效检索方案。利用时间戳作键,实现O(1)级查询与去重,结合链式地址法支持多条件检索,并通过Python实现插入、查询、删除操作。测试表明,相较传统列表,检索速度提升80%以上,存储减少15%,具备高实时性与可扩展性,适用于大规模屏幕监控系统。
222 5
|
8月前
|
机器学习/深度学习 算法 调度
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
397 1
|
9月前
|
API 数据安全/隐私保护 开发者
深度分析苏宁API接口,用Python脚本实现
深度分析苏宁API接口,用Python脚本实现
|
9月前
|
JSON API 开发者
深度分析阿里妈妈API接口,用Python脚本实现
阿里妈妈是阿里巴巴旗下营销平台,提供淘宝联盟、直通车等服务,支持推广位管理、商品查询等API功能。本文详解其API调用方法,重点实现商品推广信息(佣金、优惠券)获取,并提供Python实现方案。
|
9月前
|
JSON API 数据安全/隐私保护
深度分析虾皮城API接口,用Python脚本实现
虾皮开放平台提供丰富的API接口,支持商品管理、订单处理及促销信息查询等功能。本文详解API认证机制与调用方法,基于Python实现商品价格及到手价获取方案,适用于电商数据分析与运营。
|
9月前
|
JSON 算法 API
深度分析小红书城API接口,用Python脚本实现
小红书作为以UGC内容为核心的生活方式平台,其非官方API主要通过移动端抓包解析获得,涵盖内容推荐、搜索、笔记详情、用户信息和互动操作等功能。本文分析了其接口体系、认证机制及请求规范,并提供基于Python的调用框架,涉及签名生成、登录态管理与数据解析。需注意非官方接口存在稳定性与合规风险,使用时应遵守平台协议及法律法规。
|
9月前
|
前端开发 Shell API
深度分析58同城API接口,用Python脚本实现
58同城为国内知名分类信息平台,涵盖房产、招聘、二手车等多领域。本文基于网页抓包与解析,分享其非官方接口的Python实现方案,分析核心接口特性与反爬应对策略,适用于数据学习与信息聚合。注意:非官方接口存在风险,使用需遵守平台规则。
|
9月前
|
JSON API 数据格式
深度分析大麦网API接口,用Python脚本实现
大麦网为国内领先演出票务平台,提供演唱会、话剧、体育赛事等票务服务。本文基于抓包分析其非官方接口,并提供Python调用方案,涵盖演出列表查询、详情获取及城市列表获取。需注意非官方接口存在稳定性风险,使用时应遵守平台规则,控制请求频率,防范封禁与法律风险。适用于个人学习、演出信息监控等场景。
|
9月前
|
JSON API 数据安全/隐私保护
【干货满满】分享微店API接口到手价,用python脚本实现
微店作为知名社交电商平台,其开放平台提供商品查询、订单管理等API接口。本文介绍如何通过微店API获取商品到手价(含优惠、券等),涵盖认证机制、Python实现及关键说明。
|
9月前
|
JSON API 数据安全/隐私保护
【干货满满】分享拼多多API接口到手价,用python脚本实现
拼多多开放平台提供商品价格查询API,通过“pdd.ddk.goods.detail”接口可获取商品基础价、优惠券、拼团价等信息。结合client_id、client_secret及签名机制实现身份认证,支持推广位ID获取专属优惠。本文提供完整Python实现,涵盖签名生成、接口调用与价格解析逻辑,适用于比价工具、导购平台等场景。

推荐镜像

更多