python鼠标宏 记录键鼠操作并重播

本文介绍了如何使用Python库pynput实现鼠标和键盘事件的录制,包括点击、移动和按键操作。作者还提供了重播功能,用户可以从文本文件中读取记录并执行。软件可用于自动化任务,但开发者是一名在校学生,功能可能不全面。
摘要由CSDN通过智能技术生成

这是我制作的软件里的鼠标宏记录以及重播 官网:fcyang.top 我已将此功能做成图形化并打包 如有需要可以直接进入我的网站下载软件使用 完全免费安全无广告 好了 以下是记录部分以及重播部分

记录部分:
 

from pynput import keyboard, mouse
import time
from pynput import mouse, keyboard
from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController


records = []  # 存储记录的列表
last_time = time.time()  # 上一个事件的时间戳

def on_click(x, y, button, pressed):
    global last_time
    current_time = time.time()
    interval = int((current_time - last_time) * 1000)  # 计算时间间隔并转换为毫秒
    action = 'mouse left down' if pressed and button == Button.left else 'mouse left up'
    records.append([interval, 'EM', action, [x, y]])  # 添加记录
    last_time = current_time

def on_move(x, y):
    global last_time
    current_time = time.time()
    interval = int((current_time - last_time) * 1000)
    records.append([interval, 'EM', 'mouse move', [x, y]])  # 添加记录
    last_time = current_time

def on_press(key):
    global last_time
    if key == Key.esc:  # 如果按下 ESC 键,则停止监听
        return False  # Returning False stops the listener
    current_time = time.time()
    interval = int((current_time - last_time) * 1000)
    key_char = ''
    if hasattr(key, 'char') and key.char:
        key_char = key.char.lower()  # 将字符转换为小写
        key_desc = [key.vk, key_char, 0]
    else:
        key_desc = [key.value.vk, key.name.upper(), 0]
    records.append([interval, 'EK', 'key down', key_desc])  # 添加记录
    last_time = current_time

def on_release(key):
    global last_time
    current_time = time.time()
    interval = int((current_time - last_time) * 1000)
    key_char = ''
    if hasattr(key, 'char') and key.char:
        key_char = key.char.lower()  # 将字符转换为小写
        key_desc = [key.vk, key_char, 0]
    else:
        key_desc = [key.value.vk, key.name.upper(), 0]
    records.append([interval, 'EK', 'key up', key_desc])  # 添加记录
    last_time = current_time

# 设置监听器
mouse_listener = mouse.Listener(on_click=on_click, on_move=on_move)
keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release)

mouse_listener.start()  # 启动鼠标监听器
keyboard_listener.start()  # 启动键盘监听器

# 等待键盘监听器停止(按下 ESC 键)
keyboard_listener.join() 

# 将记录保存到文件
with open('scripts.txt', 'w') as f:
    for record in records:
        f.write(str(record) + '\n')

print("记录完毕")

重播部分:

import time
from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController

mouse_controller = MouseController()
keyboard_controller = KeyboardController()

def get_key(key_code, key_char):
    # 将字符串形式的特殊键转换为pynput的Key对象
    special_keys = {
        'ALT': Key.alt,
        'ALT_GR': Key.alt_gr,
        'ALT_L': Key.alt_l,
        'ALT_R': Key.alt_r,
        'BACKSPACE': Key.backspace,
        'CAPS_LOCK': Key.caps_lock,
        'CMD': Key.cmd,
        'CTRL_L': Key.ctrl_l,
        'CTRL_R': Key.ctrl_r,
        'DELETE': Key.delete,
        'DOWN': Key.down,
        'END': Key.end,
        'ENTER': Key.enter,
        'ESC': Key.esc,
        'F1': Key.f1,
        'F2': Key.f2,
        'F3': Key.f3,
        'F4': Key.f4,
        'F5': Key.f5,
        'F6': Key.f6,
        'F7': Key.f7,
        'F8': Key.f8,
        'F9': Key.f9,
        'F10': Key.f10,
        'F11': Key.f11,
        'F12': Key.f12,
        'HOME': Key.home,
        'INSERT': Key.insert,
        'LEFT': Key.left,
        'NUM_LOCK': Key.num_lock,
        'PAGE_DOWN': Key.page_down,
        'PAGE_UP': Key.page_up,
        'RIGHT': Key.right,
        'SCROLL_LOCK': Key.scroll_lock,
        'SHIFT': Key.shift,
        'SHIFT_R': Key.shift_r,
        'SPACE': Key.space,
        'TAB': Key.tab,
        'UP': Key.up,
        'PRINT_SCREEN': Key.print_screen,
        'MENU': Key.menu,
    }
    if key_char.upper() in special_keys:
        return special_keys[key_char.upper()]
    else:
        return key_char

with open('scripts.txt', 'r') as f:
    for line in f:
        record = eval(line.strip())
        time.sleep(record[0] / 1000)  # 将毫秒转换为秒
        if record[1] == 'EM':  # 鼠标事件
            x, y = record[3]
            if 'move' in record[2]:
                mouse_controller.position = (x, y)
            elif 'left down' in record[2]:
                mouse_controller.press(Button.left)
            elif 'left up' in record[2]:
                mouse_controller.release(Button.left)
        elif record[1] == 'EK':  # 键盘事件
            key_code, key_char, _ = record[3]
            key = get_key(key_code, key_char)
            if 'down' in record[2]:
                keyboard_controller.press(key)
            elif 'up' in record[2]:
                keyboard_controller.release(key)

直接导入即可使用!非常方便 可能有一些按键仍然无法使用 修改重播部分里的get_key即可!

如果您觉得这个记录于重播很好用 不妨来试试我的软件 去Fuchen官网 (fcyang.top)即可下载  软件中包含这个功能还有一些其他的工具 全部免费开放使用

我是一名高二在校生 部分功能做的可能没有那么全面 个人能力以及时间精力有限 请谅解!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值