import time
from pynput.mouse import Button
from pynput.keyboard import Controller, Key
from pynput.keyboard import Listener as Keyboard_Listener
from pynput.mouse import Listener as Mouse_Listener
import os
from threading import Thread
click_location=[]
time_difference=0
click_count=1
click_time=[]
path_clicked={}
command_list=[]
title ='\n'+ 'import pyautogui'+'\n' + 'import time' + '\n' +'\n'
def on_click(x, y , button, pressed):
if pressed:
global click_count
global time_difference
t=time.time()
click_time.append(t)
click_location.append((x,y))
if len(click_location)!=1:
time_difference=click_time[1]-click_time[0]
if click_location[0]==click_location[1]:
if time_difference<=0.3:
click_count=2
else:
click_count=1
else:
click_count=1
click_time.pop(0)
click_location.pop(0)
if button == Button.left:
button_name = 'Button_left'
elif button == Button.middle:
button_name = 'Button_middle'
elif button == Button.right:
button_name = 'Button_right'
else:
button_name = 'Unknown'
if 'Key.esc' in all_key:
return False
else:
if button_name == 'Button_left' and click_count == 1:
command = 'pyautogui.click{0}'.format((x,y))+'\n' + 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.append(command)
elif button_name == 'Button_left' and click_count== 2:
command = 'pyautogui.doubleClick{0}'.format((x,y)) +'\n'+ 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.pop(-1)
command_list.append(command)
elif button_name == 'Button_right' and click_count == 1:
command = 'pyautogui.rightClick{0}'.format((x,y))+'\n'+ 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.append(command)
elif button_name == 'Button_middle' and click_count == 1:
command = 'pyautogui.click(button= \'middle\')'+'\n'+ 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.append(command)
def on_scroll(x, y, dx, dy):
if 'Key.esc' in all_key:
return False
else:
command = 'pyautogui.scroll({0})'.format(dy*120) + '\n'
command_list.append(command)
print('pyautogui.scroll({0})'.format(dy*120))
def on_release(key):
all_key.append(str(key))
if len(str(key))>3:
keyword = '\'' + str(key)[4:] + '\''
else:
keyword = str(key)
command = keyword + '\n'
if key == Key.esc:
return False
else:
command_list.append(command)
print(command_list)
def text_create(name, msg):
desktop_path = os.path.abspath('.')
full_path = desktop_path + '\\' + name + '~' + '.txt'
file = open(full_path, 'a')
file.write(msg)
file.close
def clear_txt(name):
desktop_path = os.path.abspath('.')
full_path = desktop_path + '\\' + name + '.txt'
global distinguish_py
distinguish_py = desktop_path + '\\' + name + '.py'
if os.path.exists(full_path):
file = open(full_path, 'r+')
file.truncate()
file.close
def hot_key(name):
desktop_path = os.path.abspath('.')
global full_path
full_path = desktop_path + '\\' + name +'~'+ '.txt'
replace_path = desktop_path + '\\' + name + '.txt'
b = open(replace_path, 'a')
f = open(full_path, 'r+')
line = f.readline()
hotkey_list = []
func_key_list = ['ctrl', 'alt' , 'shift','cmd']
while line:
line = f.readline()
if line[:1]=='\'':
for func_key in func_key_list:
if func_key in hotkey_list and func_key not in line:
command = 'pyautogui.hotkey(\'' + func_key + '\''+ ','+ line.strip() + ')' +'\n'
b.write(command)
hotkey_list = []
if func_key in line:
hotkey_list.append(func_key)
else:
b.write(line)
b.close
f.close
def change_suffix(name):
desktop_path = os.path.abspath('.')
full_path = desktop_path + '\\' + name + '.txt'
portion = os.path.splitext(full_path)
if portion[1] == '.txt':
newname = portion[0] + '.py'
os.rename(full_path, newname)
def mouse_click(x, y):
pass
def mouse_dclick(x, y):
pass
def mouse_drag():
(x1,y1)= on_click(x,y)
(x2,y2)= on_dclick(x, y)
if (x1y1) != (x2,y2):
command = 'pyautogui.click(941,34, button=\'left\')' + '\n' + 'pyautogui.dragRel(0,100, button=\'left\', duration=5)'
command_list.append(command)
print(command)
if __name__=='__main__':
num = 0
export_file = 'record'
clear_txt(export_file)
while os.path.isfile(distinguish_py):
num=int(num)+1
export_file = 'record'+ '(' + str(num) +')'
distinguish_py = os.path.abspath('.') + '\\' + export_file + '.py'
text_create(export_file, title)
global all_key
all_key = []
global keyboard_listen_thread
global mouse_listen_thread
mouse_listen_thread = Mouse_Listener(on_click=on_click, on_scroll=on_scroll)
mouse_listen_thread.start()
keyboard_listen_thread = Keyboard_Listener(on_release=on_release)
keyboard_listen_thread.start()
keyboard_listen_thread.join()
for command in command_list:
text_create(export_file, command)
hot_key(export_file)
change_suffix(export_file)
os.remove(full_path)
print("All commands have been recorded!")
本作品採用《CC 協議》,轉載必須註明作者和本文連結