小白想用ChatGPT写个代码,实现记录操作后台虚拟播放,类似于雷电模拟器里面的脚本宏,目前遇到了bug,原始代码如下:
from pywinauto.application import Application
import time
import json
import threading
import pyautogui
from pynput import keyboard
from pynput import mouse
# 配置
app_path = r'D:\Program Files (x86)\KuGou\KGMusic\KuGou.exe' # 可执行程序路径,更改为你的目标应用程序路径
# 打开目标应用程序
app = Application(backend="uia").start(app_path)
time.sleep(2) # 等待程序启动
actions = []
recording = False
def on_keyboard_press(key):
global recording
try:
key_char = key.char
except AttributeError:
key_char = key.name # 对于特殊键,使用它的名称作为字符
if key_char == 'f9':
recording = True
actions.clear()
print("开始记录...")
elif key_char == 'f10':
recording = False
print("停止记录...")
with open('actions.json', 'w') as f:
json.dump(actions, f)
print("记录已保存.")
elif key_char == 'f11':
print("开始回放...")
playback_actions()
print("回放完成.")
elif recording:
actions.append(('keyboard', 'press', key_char, time.time()))
def on_keyboard_release(key):
global recording
if recording:
try:
key_char = key.char
except AttributeError:
key_char = key.name
actions.append(('keyboard', 'release', key_char, time.time()))
def on_mouse_click(x, y, button, pressed):
global recording
if recording:
button_str = str(button).split('.')[-1] # 转换 Button.left 到 'left'
actions.append(('mouse', 'click', x, y, button_str, pressed, time.time()))
def on_mouse_move(x, y):
global recording
if recording:
actions.append(('mouse', 'move', x, y, time.time()))
# 记录动作
def record_actions():
keyboard_listener = keyboard.Listener(on_press=on_keyboard_press, on_release=on_keyboard_release)
mouse_listener = mouse.Listener(on_click=on_mouse_click, on_move=on_mouse_move)
with keyboard_listener, mouse_listener:
keyboard_listener.join()
mouse_listener.join()
# 回放函数
def playback_actions():
start_time = time.time()
for action in actions:
elapsed = action[-1] - actions[0][-1]
while (time.time() - start_time) < elapsed:
time.sleep(0.01)
if action[0] == 'mouse':
if action[1] == 'move':
_, _, x, y, _ = action
pyautogui.moveTo(x, y)
elif action[1] == 'click':
_, _, x, y, button_str, pressed, _ = action
pyautogui.moveTo(x, y)
if pressed:
pyautogui.mouseDown(button=button_str)
else:
pyautogui.mouseUp(button=button_str)
elif action[0] == 'keyboard':
_, key_action, key_str, _ = action
if key_action == 'press':
pyautogui.keyDown(key_str)
else:
pyautogui.keyUp(key_str)
# 启动记录线程
thread = threading.Thread(target=record_actions)
thread.start()
from pywinauto.application import Application
import time
import json
import threading
import pyautogui
from pynput import keyboard
from pynput import mouse
# 配置
app_path = r'D:\Program Files (x86)\KuGou\KGMusic\KuGou.exe' # 可执行程序路径,更改为你的目标应用程序路径
# 打开目标应用程序
app = Application(backend="uia").start(app_path)
time.sleep(2) # 等待程序启动
actions = []
recording = False
def on_keyboard_press(key):
global recording
try:
key_char = key.char
except AttributeError:
key_char = key.name # 对于特殊键,使用它的名称作为字符
if key_char == 'f9':
recording = True
actions.clear()
print("开始记录...")
elif key_char == 'f10':
recording = False
print("停止记录...")
with open('actions.json', 'w') as f:
json.dump(actions, f)
print("记录已保存.")
elif key_char == 'f11':
print("开始回放...")
playback_actions()
print("回放完成.")
elif recording:
actions.append(('keyboard', 'press', key_char, time.time()))
def on_keyboard_release(key):
global recording
if recording:
try:
key_char = key.char
except AttributeError:
key_char = key.name
actions.append(('keyboard', 'release', key_char, time.time()))
def on_mouse_click(x, y, button, pressed):
global recording
if recording:
button_str = str(button).split('.')[-1] # 转换 Button.left 到 'left'
actions.append(('mouse', 'click', x, y, button_str, pressed, time.time()))
def on_mouse_move(x, y):
global recording
if recording:
actions.append(('mouse', 'move', x, y, time.time()))
# 记录动作
def record_actions():
keyboard_listener = keyboard.Listener(on_press=on_keyboard_press, on_release=on_keyboard_release)
mouse_listener = mouse.Listener(on_click=on_mouse_click, on_move=on_mouse_move)
with keyboard_listener, mouse_listener:
keyboard_listener.join()
mouse_listener.join()
# 回放函数
def playback_actions():
start_time = time.time()
for action in actions:
elapsed = action[-1] - actions[0][-1]
while (time.time() - start_time) < elapsed:
time.sleep(0.01)
if action[0] == 'mouse':
if action[1] == 'move':
_, _, x, y, _ = action
pyautogui.moveTo(x, y)
elif action[1] == 'click':
_, _, x, y, button_str, pressed, _ = action
pyautogui.moveTo(x, y)
if pressed:
pyautogui.mouseDown(button=button_str)
else:
pyautogui.mouseUp(button=button_str)
elif action[0] == 'keyboard':
_, key_action, key_str, _ = action
if key_action == 'press':
pyautogui.keyDown(key_str)
else:
pyautogui.keyUp(key_str)
# 启动记录线程
thread = threading.Thread(target=record_actions)
thread.start()