python实现一个录制键盘和鼠标操作记录类似按键精灵回放代码

代码语言:python

所属分类:其他

代码描述:python实现一个录制键盘和鼠标操作记录 类似按键精灵回放代码

代码标签: python 录制 键盘 鼠标 操作 记录 类似 按键 精灵 回放 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

#pip install pynput
import time
from pynput import mouse, keyboard
import json # 用于保存和加载记录

# --- 全局变量 ---
recorded_events = []
is_recording = False
is_replaying = False
start_time = None
last_event_time = None # 将在录制开始时初始化为一个列表: [timestamp]

# --- 控制器 ---
mouse_controller = mouse.Controller()
keyboard_controller = keyboard.Controller()

# --- 录制相关函数 ---
def on_move(x, y):
    # global last_event_time, recorded_events # Not strictly needed for modifying mutable contents
    if is_recording and not is_replaying:
        current_time = time.time()
        # last_event_time 应该是一个列表,如 [timestamp]
        delay = current_time - last_event_time[0] if last_event_time and last_event_time[0] is not None else 0
        last_event_time[0] = current_time
        recorded_events.append({'type': 'mouse_move', 'x': x, 'y': y, 'delay': delay})
        # print(f"Mouse moved to ({x}, {y}) after {delay:.2f}s")

def on_click(x, y, button, pressed):
    # global last_event_time, recorded_events # Not strictly needed
    if is_recording and not is_replaying:
        current_time = time.time()
        delay = current_time - last_event_time[0] if last_event_time and last_event_time[0] is not None else 0
        last_event_time[0] = current_time
        action = 'pressed' if pressed else 'released'
        recorded_events.append({
            'type': 'mouse_click', 'x': x, 'y': y,
            'button': str(button),
            'action': action, 'delay': delay
        })
        # print(f"Mouse {action} at ({x}, {y}) with {button} after {delay:.2f}s")

def on_scroll(x, y, dx, dy):
    # global last_event_time, recorded_events # Not strictly needed
    if is_recording and not is_replaying:
        current_time = time.time()
        delay = current_time - last_event_time[0] if last_event_time and last_event_time[0] is not None else 0
        last_event_time[0] = current_time
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0