贴一篇录制和回放鼠标操作的脚本

发布于 2023-01-26  333 次阅读


  • 作者不是我鸭,是上一期那个"神奇的朋友":执伞丶问春秋(b站),因为做不来鼠标抬起的判断,就用鼠标按下事件,代替了鼠标点击检测的按下和抬起事件,导致使用过程中出现按住左键拖动鼠标移动的操作就会出bug.要是有大佬顺手修了bug也赏我一份鸭
import time
import win32api
import win32gui
import win32con
import keyboard
import threading
import pyautogui
import csv
import os

pyautogui.PAUSE = 0.0001

def create_folder():
    try:
        os.mkdir('C:\\模拟光标')
    except FileExistsError:
        os.chdir('C:\\模拟光标')
        pass
    else:
        os.chdir('C:\\模拟光标')

def mode_1():
    n = 1
    while True:
        try:
            f = open('{}.csv'.format(n),'x+')
        except FileExistsError:
            n += 1
        else:
            print('{}.csv文件已成功创建'.format(n))
            f.close()
            break
    print('按下"space"以开始文件写入')
    keyboard.wait('space')
    print('文件写入将于5秒后开始,请在此时间内最小化终端窗口')
    for i in range(0,5):
        print('倒计时{}秒'.format(5-i))
        time.sleep(1)
    x = 0
    y = 0
    time_through = 0
    print('开始获取光标位置')
    time_pass = time.time()
    repeat = 0
    while True:
        with open('{}.csv'.format(n),'a+') as f:
            csv_writer = csv.writer(f)
            mouse_left = win32api.GetKeyState(0x01)
            if mouse_left<0:
                width,height = win32api.GetCursorPos()
                if width != x and height!= y:
                    x = width
                    y = height
                    time_all = time.time()
                    if repeat == 0:
                        time_through = time_all - time_pass
                    else:
                        time_through = time_all - time_through - time_pass
                        time_pass = time_all - time_through
                    repeat = 1
                    print('在{}秒后点击{}、{}'.format(time_through,width,height))
                    list_writedown = [time_through,width,height]
                    csv_writer.writerow(list_writedown)

def mode_2():
    #a接收要执行的文件的基础名
    while True:
        while True:
            a = input('请输入文件基本名\n')
            try:
                f = open('{}.csv'.format(a),'r')
            except FileNotFoundError:
                print('未在"C:\模拟光标"文件夹中找到名为{}.csv的文件,请重新输入'.format(a))
                continue
            else:
                print('对应文件已找到')
                f.close()
                break
        s = int(input('请输入文件重复次数\n'))
        print('按下"space"以开始执行文件')
        keyboard.wait('space')
        print('文件执行将于5秒后开始,请在此时间内最小化终端窗口')
        for i in range(0,5):
            print('倒计时{}秒'.format(5-i))
            time.sleep(1)
        print('开始执行文件')
        for m in range(s):
            time.sleep(2)
            with open('{}.csv'.format(a),'r') as f:
                reader = list(csv.reader(f,delimiter=','))
                for p in range(0,len(reader),2):
                    time_through = float(reader[p][0])
                    x = int(reader[p][1])
                    y = int(reader[p][2])
                    time.sleep(time_through)
                    pyautogui.click(x,y)
            time.sleep(3)
            print('已执行完成{}次'.format(m+1))
        print('已完成此次文件执行任务')

if __name__ == '__main__':
    mode1 = threading.Thread(target=mode_1,daemon=True)
    mode2 = threading.Thread(target=mode_2,daemon=True)
    create_folder()
    while True:
        k = input('输入mode1写入文件\n输入mode2执行文件\n按下"pause"终止程序\n')
        if k == 'mode1':
            mode1.start()
            break
        elif k == 'mode2':
            mode2.start()
            break
        else:
            print('输入错误,请重新输入')
            continue
    keyboard.wait('pause')
    print('已停止程序并关闭')
届ける言葉を今は育ててる
最后更新于 2024-02-07