电脑录制鼠标键盘脚本, 鼠标录制器免费版电脑版, 鼠标键盘录制工具【python】

简介: 这个Python脚本实现了一个完整的鼠标键盘录制工具,具有以下功能特点:可以录制鼠标移动、点击和键盘按键操作

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

这个Python脚本实现了一个完整的鼠标键盘录制工具,具有以下功能特点:
可以录制鼠标移动、点击和键盘按键操作
支持保存和加载录制文件(JSON格式)
可以调整回放速度
使用多线程实现平滑的鼠标移动录制
提供简单的命令行界面控制录制和回放
记录操作的时间戳,实现精确回放

源码部分:

import pyautogui
import keyboard
import time
import json
from datetime import datetime
import threading

class ActionRecorder:
def init(self):
self.recording = False
self.playing = False
self.actions = []
self.start_time = None
self.stop_event = threading.Event()

def record_mouse_movement(self):
    last_pos = pyautogui.position()
    while self.recording and not self.stop_event.is_set():
        current_pos = pyautogui.position()
        if current_pos != last_pos:
            timestamp = time.time() - self.start_time
            self.actions.append({
                'type': 'mouse_move',
                'x': current_pos[0],
                'y': current_pos[1],
                'timestamp': timestamp
            })
            last_pos = current_pos
        time.sleep(0.01)

def on_key_event(self, e):
    if self.recording and e.event_type in ('down', 'up'):
        timestamp = time.time() - self.start_time
        self.actions.append({
            'type': 'key_' + e.event_type,
            'key': e.name,
            'timestamp': timestamp
        })

def on_mouse_click(self, x, y, button, pressed):
    if self.recording:
        timestamp = time.time() - self.start_time
        self.actions.append({
            'type': 'mouse_' + ('down' if pressed else 'up'),
            'button': str(button),
            'x': x,
            'y': y,
            'timestamp': timestamp
        })

def start_recording(self):
    if not self.recording:
        self.actions = []
        self.recording = True
        self.start_time = time.time()

        # Start mouse movement recording thread
        mouse_thread = threading.Thread(target=self.record_mouse_movement)
        mouse_thread.daemon = True
        mouse_thread.start()

        # Setup keyboard and mouse listeners
        keyboard.hook(self.on_key_event)
        pyautogui.onMouseDown = lambda x, y, button, pressed: self.on_mouse_click(x, y, button, pressed)
        pyautogui.onMouseUp = lambda x, y, button, pressed: self.on_mouse_click(x, y, button, pressed)

def stop_recording(self):
    if self.recording:
        self.recording = False
        self.stop_event.set()
        keyboard.unhook_all()
        self.stop_event.clear()

def play_actions(self, speed=1.0):
    if not self.playing and self.actions:
        self.playing = True
        start_time = time.time()
        last_timestamp = 0

        for action in self.actions:
            while self.playing:
                current_time = time.time() - start_time
                expected_time = action['timestamp'] / speed

                if current_time >= expected_time:
                    break
                time.sleep(0.001)

            if not self.playing:
                break

            if action['type'] == 'mouse_move':
                pyautogui.moveTo(action['x'], action['y'])
            elif action['type'] == 'mouse_down':
                pyautogui.mouseDown(x=action['x'], y=action['y'], button=action['button'])
            elif action['type'] == 'mouse_up':
                pyautogui.mouseUp(x=action['x'], y=action['y'], button=action['button'])
            elif action['type'] == 'key_down':
                pyautogui.keyDown(action['key'])
            elif action['type'] == 'key_up':
                pyautogui.keyUp(action['key'])

        self.playing = False

def stop_playing(self):
    self.playing = False

def save_recording(self, filename):
    with open(filename, 'w') as f:
        json.dump({
            'actions': self.actions,
            'created_at': datetime.now().isoformat(),
            'screen_size': pyautogui.size()
        }, f, indent=2)

def load_recording(self, filename):
    with open(filename, 'r') as f:
        data = json.load(f)
        self.actions = data['actions']
        return data['screen_size']

def main():
recorder = ActionRecorder()
print("鼠标键盘录制工具")
print("命令: record, stop, play, save , load , exit")

while True:
    cmd = input("> ").strip().lower().split(' ', 1)
    action = cmd[0]

    if action == 'record':
        recorder.start_recording()
        print("开始录制... 按Ctrl+C停止")
    elif action == 'stop':
        recorder.stop_recording()
        print("停止录制")
    elif action == 'play':
        speed = 1.0
        if len(cmd) > 1:
            try:
                speed = float(cmd[1])
            except ValueError:
                pass
        print(f"开始播放 (速度: {speed}x)... 按Ctrl+C停止")
        recorder.play_actions(speed)
    elif action == 'save' and len(cmd) > 1:
        filename = cmd[1]
        if not filename.endswith('.json'):
            filename += '.json'
        recorder.save_recording(filename)
        print(f"录制已保存到 {filename}")
    elif action == 'load' and len(cmd) > 1:
        filename = cmd[1]
        if not filename.endswith('.json'):
            filename += '.json'
        try:
            recorder.load_recording(filename)
            print(f"从 {filename} 加载录制")
        except FileNotFoundError:
            print("文件未找到")
    elif action == 'exit':
        break
    else:
        print("未知命令")

if name == 'main':
main()

相关文章
|
3月前
|
存储 监控 算法
监控电脑屏幕的帧数据检索 Python 语言算法
针对监控电脑屏幕场景,本文提出基于哈希表的帧数据高效检索方案。利用时间戳作键,实现O(1)级查询与去重,结合链式地址法支持多条件检索,并通过Python实现插入、查询、删除操作。测试表明,相较传统列表,检索速度提升80%以上,存储减少15%,具备高实时性与可扩展性,适用于大规模屏幕监控系统。
158 5
|
4月前
|
存储 缓存 测试技术
理解Python装饰器:简化代码的强大工具
理解Python装饰器:简化代码的强大工具
|
5月前
|
JSON 算法 API
深度分析小红书城API接口,用Python脚本实现
小红书作为以UGC内容为核心的生活方式平台,其非官方API主要通过移动端抓包解析获得,涵盖内容推荐、搜索、笔记详情、用户信息和互动操作等功能。本文分析了其接口体系、认证机制及请求规范,并提供基于Python的调用框架,涉及签名生成、登录态管理与数据解析。需注意非官方接口存在稳定性与合规风险,使用时应遵守平台协议及法律法规。
|
5月前
|
程序员 测试技术 开发者
Python装饰器:简化代码的强大工具
Python装饰器:简化代码的强大工具
245 92
|
4月前
|
机器学习/深度学习 编解码 Python
Python图片上采样工具 - RealESRGANer
Real-ESRGAN基于深度学习实现图像超分辨率放大,有效改善传统PIL缩放的模糊问题。支持多种模型版本,推荐使用魔搭社区提供的预训练模型,适用于将小图高质量放大至大图,放大倍率越低效果越佳。
369 3
|
5月前
|
人工智能 自然语言处理 安全
Python构建MCP服务器:从工具封装到AI集成的全流程实践
MCP协议为AI提供标准化工具调用接口,助力模型高效操作现实世界。
1126 1
|
5月前
|
JSON API 开发者
深度分析阿里妈妈API接口,用Python脚本实现
阿里妈妈是阿里巴巴旗下营销平台,提供淘宝联盟、直通车等服务,支持推广位管理、商品查询等API功能。本文详解其API调用方法,重点实现商品推广信息(佣金、优惠券)获取,并提供Python实现方案。
|
5月前
|
JSON API 数据安全/隐私保护
深度分析虾皮城API接口,用Python脚本实现
虾皮开放平台提供丰富的API接口,支持商品管理、订单处理及促销信息查询等功能。本文详解API认证机制与调用方法,基于Python实现商品价格及到手价获取方案,适用于电商数据分析与运营。
|
4月前
|
算法 安全 数据安全/隐私保护
Python随机数函数全解析:5个核心工具的实战指南
Python的random模块不仅包含基础的随机数生成函数,还提供了如randint()、choice()、shuffle()和sample()等实用工具,适用于游戏开发、密码学、统计模拟等多个领域。本文深入解析这些函数的用法、底层原理及最佳实践,帮助开发者高效利用随机数,提升代码质量与安全性。
947 0
|
5月前
|
API 数据安全/隐私保护 开发者
深度分析苏宁API接口,用Python脚本实现
深度分析苏宁API接口,用Python脚本实现

推荐镜像

更多