from pynput.mouse import Listener as MouseListener
from pynput import keyboard
import logging
import time
import pydirectinput
mode = input("1 - Record TAS\n2 - Replay TAS\nChoice: ")
if mode == "1":
filename = f"{input('Name for record: ')}.tas"
file = open(filename, 'w')
file.close()
file = open(filename, 'r+')
print("Record will start in 3 seconds...")
time.sleep(3)
presstime = 0
releasetime = 0
movetime = time.time()
keytime = {}
pressed_keys = set()
def on_move(x, y):
global movetime
file.write(f"sleep {time.time() - movetime}\n")
file.write(f"move {x} {y}\n")
movetime = time.time()
file.flush()
def on_click(x, y, button, pressed):
global presstime
global releasetime
if pressed:
file.write(f'press {x} {y} {button}\n')
presstime = time.time()
file.flush()
else:
releasetime = time.time()
file.write(f"sleep {releasetime - presstime}\n")
file.write("release\n")
file.flush()
def on_scroll(x, y, dx, dy):
file.write(f'scroll ({x}, {y})({dx}, {dy})\n')
file.flush()
def on_press(key):
global pressed_keys
pressed_keys.add(key)
file.write(f"pkey {key}\n")
file.flush()
def on_release(key):
global pressed_keys
pressed_keys.remove(key)
file.write(f"prelease {key}\n")
file.flush()
with keyboard.Listener(on_press=on_press, on_release=on_release) as klistener:
with MouseListener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
klistener.join()
listener.join()
elif mode == "2":
filename = input("Enter TAS name: ")+".tas"
file = open(filename, 'r')
script = file.readlines()
print("Replay will start in 3 seconds...")
time.sleep(3)
for task in script:
if "sleep" in task:
task = task.replace("sleep ", "").replace("\n", "")
time.sleep(float(task))
elif "move" in task:
task = task.replace("move ", "").replace("\n", "")
task = task.split(" ")
x = int(task[0])
y = int(task[1])
pydirectinput.moveTo(x, y, duration=0.0, _pause=False)
elif "press" in task and "left" in task:
task = task.replace("press ", "").replace("\n", "")
task = task.split(" ")
x = int(task[0])
y = int(task[1])
pydirectinput.moveTo(x, y, duration=0.0, _pause=False)
pydirectinput.mouseDown(_pause=False)
elif "press" in task and "right" in task:
task = task.replace("press ", "").replace("\n", "")
task = task.split(" ")
x = int(task[0])
y = int(task[1])
pydirectinput.moveTo(x, y, duration=0.0, _pause=False)
pydirectinput.mouseDown(button='right', _pause=False)
elif "release" in task and "prelease" not in task:
pydirectinput.mouseUp(_pause=False)
pydirectinput.mouseUp(button='right', _pause=False)
elif "pkey" in task:
task = task.replace("pkey ", "").replace("\n", "").replace("Key.", "").replace("_l", "").replace("_r", "").replace("'", "")
pydirectinput.keyDown(task, _pause=False)
elif "prelease" in task:
task = task.replace("prelease ", "").replace("\n", "").replace("Key.", "").replace("_l", "").replace("_r", "").replace("'", "")
pydirectinput.keyUp(task, _pause=False)
print("Replay ended...")