Added load file button
This commit is contained in:
parent
accc5eb3b5
commit
4ed6716486
99
macro.py
99
macro.py
|
@ -5,12 +5,30 @@ from keyboard import is_pressed, read_key
|
|||
from tkinter import filedialog
|
||||
import time
|
||||
import threading
|
||||
from multiprocessing import Process
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
|
||||
appdata_local = os.getenv('LOCALAPPDATA')+"/MacroRecorder"
|
||||
appdata_local = appdata_local.replace('\\', "/")
|
||||
|
||||
macroEvents = {"events": []}
|
||||
|
||||
mouseControl = mouse.Controller()
|
||||
keyboardControl = keyboard.Controller()
|
||||
special_keys = {"Key.shift": Key.shift, "Key.tab": Key.tab, "Key.caps_lock": Key.caps_lock, "Key.ctrl": Key.ctrl, "Key.ctrl_l": Key.ctrl_l, "Key.alt": Key.alt, "Key.cmd": Key.cmd, "Key.cmd_r": Key.cmd_r, "Key.alt_r": Key.alt_r, "Key.ctrl_r": Key.ctrl_r, "Key.shift_r": Key.shift_r, "Key.enter": Key.enter, "Key.backspace": Key.backspace, "Key.f19": Key.f19, "Key.f18": Key.f18, "Key.f17": Key.f17, "Key.f16": Key.f16, "Key.f15": Key.f15, "Key.f14": Key.f14, "Key.f13": Key.f13, "Key.media_volume_up": Key.media_volume_up, "Key.media_volume_down": Key.media_volume_down, "Key.media_volume_mute": Key.media_volume_mute, "Key.media_play_pause": Key.media_play_pause, "Key.f6": Key.f6, "Key.f5": Key.f5, "Key.right": Key.right, "Key.down": Key.down, "Key.left": Key.left, "Key.up": Key.up, "Key.page_up": Key.page_up, "Key.page_down": Key.page_down, "Key.home": Key.home, "Key.end": Key.end, "Key.delete": Key.delete, "Key.space": Key.space}
|
||||
special_keys = {"Key.esc": Key.esc, "Key.shift": Key.shift, "Key.tab": Key.tab, "Key.caps_lock": Key.caps_lock,
|
||||
"Key.ctrl": Key.ctrl, "Key.ctrl_l": Key.ctrl_l, "Key.alt": Key.alt, "Key.cmd": Key.cmd,
|
||||
"Key.cmd_r": Key.cmd_r, "Key.alt_r": Key.alt_r, "Key.ctrl_r": Key.ctrl_r, "Key.shift_r": Key.shift_r,
|
||||
"Key.enter": Key.enter, "Key.backspace": Key.backspace, "Key.f19": Key.f19, "Key.f18": Key.f18,
|
||||
"Key.f17": Key.f17, "Key.f16": Key.f16, "Key.f15": Key.f15, "Key.f14": Key.f14, "Key.f13": Key.f13,
|
||||
"Key.media_volume_up": Key.media_volume_up, "Key.media_volume_down": Key.media_volume_down,
|
||||
"Key.media_volume_mute": Key.media_volume_mute, "Key.media_play_pause": Key.media_play_pause,
|
||||
"Key.f6": Key.f6, "Key.f5": Key.f5, "Key.right": Key.right, "Key.down": Key.down, "Key.left": Key.left,
|
||||
"Key.up": Key.up, "Key.page_up": Key.page_up, "Key.page_down": Key.page_down, "Key.home": Key.home,
|
||||
"Key.end": Key.end, "Key.delete": Key.delete, "Key.space": Key.space}
|
||||
|
||||
record = False
|
||||
playback = False
|
||||
|
@ -29,23 +47,24 @@ def startRecord():
|
|||
mouse_listener.start()
|
||||
keyboard_listener.start()
|
||||
|
||||
|
||||
def stopRecord():
|
||||
global macroEvents, record
|
||||
mouse_listener.stop()
|
||||
keyboard_listener.stop()
|
||||
json_macroEvents = json.dumps(macroEvents, indent=4)
|
||||
open(os.path.join(appdata_local+"/temprecord.json"), "w").write(json_macroEvents)
|
||||
print('record stopped')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def saveMacro():
|
||||
global saveFile
|
||||
global fileAlreadySaved
|
||||
global macroPath
|
||||
json_macroEvents = json.dumps(macroEvents, indent=4)
|
||||
if fileAlreadySaved == False:
|
||||
macroSaved = filedialog.asksaveasfile(filetypes = [('Json Files', '*.json')], defaultextension = '.json')
|
||||
macroSaved = filedialog.asksaveasfile(filetypes=[('Json Files', '*.json')], defaultextension='.json')
|
||||
if macroSaved is not None:
|
||||
macroPath = macroSaved.name
|
||||
macroSaved.write(json_macroEvents)
|
||||
|
@ -57,7 +76,6 @@ def saveMacro():
|
|||
saveFile = False
|
||||
|
||||
|
||||
|
||||
def on_move(x, y):
|
||||
global start_time
|
||||
macroEvents["events"].append({'type': 'cursorMove', 'x': x, 'y': y, 'timestamp': time.time() - start_time})
|
||||
|
@ -132,7 +150,8 @@ def playRec():
|
|||
elif macroEvents["events"][i]["type"] == "scrollEvent":
|
||||
mouseControl.scroll(macroEvents["events"][i]["dx"], macroEvents["events"][i]["dy"])
|
||||
elif macroEvents["events"][i]["type"] == "keyboardEvent":
|
||||
keyToPress = macroEvents["events"][i]["key"] if 'Key.' not in macroEvents["events"][i]["key"] else special_keys[macroEvents["events"][i]["key"]]
|
||||
keyToPress = macroEvents["events"][i]["key"] if 'Key.' not in macroEvents["events"][i]["key"] else \
|
||||
special_keys[macroEvents["events"][i]["key"]]
|
||||
if macroEvents["events"][i]["pressed"] == True:
|
||||
keyboardControl.press(keyToPress)
|
||||
else:
|
||||
|
@ -140,26 +159,60 @@ def playRec():
|
|||
playback = False
|
||||
|
||||
|
||||
|
||||
|
||||
while True:
|
||||
if record == False:
|
||||
if is_pressed('1'):
|
||||
print("pressed 1")
|
||||
startRecord()
|
||||
if playback == False:
|
||||
if is_pressed('3'):
|
||||
playRec()
|
||||
|
||||
if (record == False and playback == False):
|
||||
if saveFile == False:
|
||||
if is_pressed('ctrl+s'):
|
||||
if is_pressed('o'):
|
||||
keyboardControl.release('o')
|
||||
startRecord()
|
||||
|
||||
if (record == False and playback == False and len(macroEvents['events']) != 0):
|
||||
if is_pressed('p'):
|
||||
keyboardControl.release('p')
|
||||
playback = True
|
||||
playbackstarted = subprocess.Popen(['python', 'play.py'])
|
||||
|
||||
if (record == False and playback == True):
|
||||
if is_pressed('escape'):
|
||||
keyboardControl.release(keyboard.Key.esc)
|
||||
playback = False
|
||||
playbackstarted.terminate()
|
||||
|
||||
if (record == False and playback == False and len(macroEvents['events']) != 0):
|
||||
if is_pressed('ctrl+alt+s'):
|
||||
keyboardControl.release(Key.ctrl_l)
|
||||
keyboardControl.release(Key.alt)
|
||||
keyboardControl.release('s')
|
||||
if saveFile == False:
|
||||
saveFile = True
|
||||
fileAlreadySaved = False
|
||||
saveMacro()
|
||||
if (record == False and playback == False and len(macroEvents['events']) != 0):
|
||||
if is_pressed('ctrl+s'):
|
||||
if saveFile == False:
|
||||
saveFile = True
|
||||
saveMacro()
|
||||
|
||||
if record == True:
|
||||
if is_pressed('2'):
|
||||
print(read_key())
|
||||
print("pressed 2")
|
||||
if is_pressed('ctrl+n'):
|
||||
if (record == False and playback == False and len(macroEvents['events']) != 0):
|
||||
macroEvents = {"events": []}
|
||||
fileAlreadySaved = False
|
||||
|
||||
if is_pressed('ctrl+l'):
|
||||
if (record == False and playback == False):
|
||||
keyboardControl.release(Key.ctrl)
|
||||
keyboardControl.release('l')
|
||||
macroFile = filedialog.askopenfile(filetypes=[('Json Files', '*.json')], defaultextension='.json')
|
||||
macroContent = open(macroFile.name)
|
||||
data = json.load(macroContent)
|
||||
macroEvents = data
|
||||
json_macroEvents = json.dumps(macroEvents, indent=4)
|
||||
open(os.path.join(appdata_local+"/temprecord.json"), "w").write(json_macroEvents)
|
||||
macroFile.close()
|
||||
keyboardControl.press(Key.f12)
|
||||
keyboardControl.release(Key.f12)
|
||||
|
||||
if (record == True and playback == False):
|
||||
if is_pressed('escape'):
|
||||
keyboardControl.release(Key.esc)
|
||||
record = False
|
||||
stopRecord()
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
from pynput import mouse, keyboard
|
||||
from pynput.mouse import Button
|
||||
from pynput.keyboard import Key
|
||||
import time
|
||||
import os
|
||||
import json
|
||||
|
||||
appdata_local = os.getenv('LOCALAPPDATA')+"/MacroRecorder"
|
||||
appdata_local = appdata_local.replace('\\', "/")
|
||||
|
||||
|
||||
with open(os.path.join(appdata_local+"/temprecord.json")) as f:
|
||||
macroEvents = json.load(f)
|
||||
|
||||
mouseControl = mouse.Controller()
|
||||
keyboardControl = keyboard.Controller()
|
||||
special_keys = {"Key.esc": Key.esc, "Key.shift": Key.shift, "Key.tab": Key.tab, "Key.caps_lock": Key.caps_lock,
|
||||
"Key.ctrl": Key.ctrl, "Key.ctrl_l": Key.ctrl_l, "Key.alt": Key.alt, "Key.cmd": Key.cmd,
|
||||
"Key.cmd_r": Key.cmd_r, "Key.alt_r": Key.alt_r, "Key.ctrl_r": Key.ctrl_r, "Key.shift_r": Key.shift_r,
|
||||
"Key.enter": Key.enter, "Key.backspace": Key.backspace, "Key.f19": Key.f19, "Key.f18": Key.f18,
|
||||
"Key.f17": Key.f17, "Key.f16": Key.f16, "Key.f15": Key.f15, "Key.f14": Key.f14, "Key.f13": Key.f13,
|
||||
"Key.media_volume_up": Key.media_volume_up, "Key.media_volume_down": Key.media_volume_down,
|
||||
"Key.media_volume_mute": Key.media_volume_mute, "Key.media_play_pause": Key.media_play_pause,
|
||||
"Key.f6": Key.f6, "Key.f5": Key.f5, "Key.right": Key.right, "Key.down": Key.down, "Key.left": Key.left,
|
||||
"Key.up": Key.up, "Key.page_up": Key.page_up, "Key.page_down": Key.page_down, "Key.home": Key.home,
|
||||
"Key.end": Key.end, "Key.delete": Key.delete, "Key.space": Key.space}
|
||||
|
||||
keyboardControl.press('p')
|
||||
keyboardControl.release('p')
|
||||
for i in range(len(macroEvents["events"])):
|
||||
time.sleep(macroEvents["events"][i]["timestamp"])
|
||||
if macroEvents["events"][i]["type"] == "cursorMove":
|
||||
mouseControl.position = (macroEvents["events"][i]["x"], macroEvents["events"][i]["y"])
|
||||
elif macroEvents["events"][i]["type"] == "leftClickEvent":
|
||||
if macroEvents["events"][i]["pressed"] == True:
|
||||
mouseControl.press(Button.left)
|
||||
else:
|
||||
mouseControl.release(Button.left)
|
||||
elif macroEvents["events"][i]["type"] == "rightClickEvent":
|
||||
if macroEvents["events"][i]["pressed"] == True:
|
||||
mouseControl.press(Button.right)
|
||||
else:
|
||||
mouseControl.release(Button.right)
|
||||
elif macroEvents["events"][i]["type"] == "middleClickEvent":
|
||||
if macroEvents["events"][i]["pressed"] == True:
|
||||
mouseControl.press(Button.middle)
|
||||
else:
|
||||
mouseControl.release(Button.middle)
|
||||
elif macroEvents["events"][i]["type"] == "scrollEvent":
|
||||
mouseControl.scroll(macroEvents["events"][i]["dx"], macroEvents["events"][i]["dy"])
|
||||
elif macroEvents["events"][i]["type"] == "keyboardEvent":
|
||||
keyToPress = macroEvents["events"][i]["key"] if 'Key.' not in macroEvents["events"][i]["key"] else \
|
||||
special_keys[macroEvents["events"][i]["key"]]
|
||||
if macroEvents["events"][i]["pressed"] == True:
|
||||
keyboardControl.press(keyToPress)
|
||||
else:
|
||||
keyboardControl.release(keyToPress)
|
||||
|
||||
|
||||
keyboardControl.press(Key.esc)
|
||||
keyboardControl.release(Key.esc)
|
134
software.py
134
software.py
|
@ -5,9 +5,48 @@ from pynput import keyboard
|
|||
import subprocess
|
||||
import atexit
|
||||
import time
|
||||
import os
|
||||
|
||||
|
||||
playback2 = False
|
||||
record2 = False
|
||||
recordSet = False
|
||||
keyboardControl = keyboard.Controller()
|
||||
|
||||
|
||||
appdata_local = os.getenv('LOCALAPPDATA')+"/MacroRecorder"
|
||||
appdata_local = appdata_local.replace('\\', "/")
|
||||
if os.path.isdir(appdata_local) == False:
|
||||
os.mkdir(appdata_local)
|
||||
|
||||
|
||||
def on_release(key):
|
||||
global record2, playback2, recordBtn, playBtn, recordSet
|
||||
try:
|
||||
if key.char == 'o':
|
||||
if (record2 == False and playback2 == False):
|
||||
startRecordingAndChangeImg(False)
|
||||
if key.char == 'p':
|
||||
if (record2 == False and playback2 == False and recordSet == True):
|
||||
replay(False)
|
||||
recordBtn.configure(state=DISABLED)
|
||||
except AttributeError:
|
||||
if key == keyboard.Key.esc:
|
||||
if (record2 == True and playback2 == False):
|
||||
stopRecordingAndChangeImg(False)
|
||||
if (record2 == False and playback2 == True):
|
||||
playback2 = False
|
||||
recordBtn.configure(state=NORMAL)
|
||||
playBtn.configure(image=playImg)
|
||||
file_menu.entryconfig('Load', state=NORMAL)
|
||||
if key == keyboard.Key.f12:
|
||||
playBtn.configure(state=NORMAL)
|
||||
file_menu.entryconfig('Save', state=NORMAL, command=saveMacro)
|
||||
file_menu.entryconfig('Save as', state=NORMAL, command=saveMacroAs)
|
||||
file_menu.entryconfig('New', state=NORMAL, command=newMacro)
|
||||
recordSet = True
|
||||
|
||||
|
||||
def cleanup():
|
||||
if 'macro_process' in globals():
|
||||
macro_process.terminate()
|
||||
|
@ -27,43 +66,82 @@ my_menu = Menu(window)
|
|||
window.config(menu=my_menu)
|
||||
|
||||
|
||||
def startRecordingAndChangeImg():
|
||||
global stopBtn
|
||||
global lenghtOfRecord
|
||||
keyboardControl.press('1')
|
||||
keyboardControl.release('1')
|
||||
lenghtOfRecord = time.time()
|
||||
recordBtn.configure(image=stopImg, command=stopRecordingAndChangeImg)
|
||||
def startRecordingAndChangeImg(pressKey=True):
|
||||
global stopBtn, record2, playback2
|
||||
playBtn.configure(state=DISABLED)
|
||||
if playback2 == False:
|
||||
record2 = True
|
||||
file_menu.entryconfig('Load', state=DISABLED)
|
||||
if pressKey:
|
||||
keyboardControl.press('o')
|
||||
keyboardControl.release('o')
|
||||
recordBtn.configure(image=stopImg, command=stopRecordingAndChangeImg)
|
||||
|
||||
|
||||
def stopRecordingAndChangeImg():
|
||||
global recordBtn
|
||||
global lenghtOfRecord
|
||||
keyboardControl.press('2')
|
||||
keyboardControl.release('2')
|
||||
lenghtOfRecord = (time.time() - lenghtOfRecord) + 1
|
||||
def stopRecordingAndChangeImg(pressKey=True):
|
||||
global recordBtn, record2, recordSet
|
||||
record2 = False
|
||||
recordSet = True
|
||||
if pressKey:
|
||||
keyboardControl.press(keyboard.Key.esc)
|
||||
keyboardControl.release(keyboard.Key.esc)
|
||||
recordBtn.configure(image=recordImg, command=startRecordingAndChangeImg)
|
||||
playBtn.configure(state=NORMAL)
|
||||
file_menu.entryconfig('Save', state=NORMAL, command=saveMacro)
|
||||
file_menu.entryconfig('Save as', state=NORMAL, command=saveMacroAs)
|
||||
file_menu.entryconfig('New', state=NORMAL, command=newMacro)
|
||||
file_menu.entryconfig('Load', state=NORMAL)
|
||||
|
||||
def replay():
|
||||
recordBtn.configure(state=DISABLED)
|
||||
keyboardControl.press('3')
|
||||
keyboardControl.release('3')
|
||||
threading.Thread(target=buttonDisabledToEnable).start()
|
||||
def replay(pressKey=True):
|
||||
global playback2, recordBtn, recordSet
|
||||
playBtn.configure(image=stopImg)
|
||||
if recordSet == True:
|
||||
playback2 = True
|
||||
file_menu.entryconfig('Load', state=DISABLED)
|
||||
if pressKey:
|
||||
keyboardControl.press('p')
|
||||
keyboardControl.release('p')
|
||||
recordBtn.configure(state=DISABLED)
|
||||
|
||||
def buttonDisabledToEnable():
|
||||
time.sleep(lenghtOfRecord)
|
||||
recordBtn.configure(state=NORMAL)
|
||||
|
||||
|
||||
def saveMacro():
|
||||
keyboardControl.press(keyboard.Key.ctrl_l)
|
||||
keyboardControl.press('s')
|
||||
keyboardControl.release(keyboard.Key.ctrl_l)
|
||||
keyboardControl.release('s')
|
||||
|
||||
def saveMacroAs():
|
||||
keyboardControl.press(keyboard.Key.ctrl_l)
|
||||
keyboardControl.press(keyboard.Key.alt)
|
||||
keyboardControl.press('s')
|
||||
|
||||
|
||||
|
||||
def newMacro():
|
||||
global recordSet
|
||||
keyboardControl.press(keyboard.Key.ctrl)
|
||||
keyboardControl.press('n')
|
||||
keyboardControl.release(keyboard.Key.ctrl)
|
||||
keyboardControl.release('n')
|
||||
recordBtn.configure(image=recordImg, command=startRecordingAndChangeImg)
|
||||
file_menu.entryconfig('Save', state=DISABLED)
|
||||
file_menu.entryconfig('Save as', state=DISABLED)
|
||||
playBtn.configure(state=DISABLED)
|
||||
recordSet = False
|
||||
|
||||
def loadMacro():
|
||||
if (playback2 == False and record2 == False):
|
||||
keyboardControl.press(keyboard.Key.ctrl)
|
||||
keyboardControl.press('l')
|
||||
|
||||
# Menu Bar
|
||||
file_menu = Menu(my_menu, tearoff=0)
|
||||
my_menu.add_cascade(label="File", menu=file_menu)
|
||||
file_menu.add_command(label="New")
|
||||
file_menu.add_command(label="Save", command=window.quit, state=DISABLED)
|
||||
file_menu.add_command(label="Save as", command=window.quit, state=DISABLED)
|
||||
file_menu.add_separator()
|
||||
file_menu.add_command(label="Settings", command=window.quit)
|
||||
file_menu.add_command(label="New", state=DISABLED)
|
||||
file_menu.add_command(label="Load", command=loadMacro)
|
||||
file_menu.add_command(label="Save", state=DISABLED)
|
||||
file_menu.add_command(label="Save as", state=DISABLED)
|
||||
|
||||
# Play Button
|
||||
playImg = PhotoImage(file=r"assets/button/play.png")
|
||||
|
@ -79,5 +157,7 @@ recordBtn.pack(side=RIGHT, padx=50)
|
|||
stopImg = PhotoImage(file=r"assets/button/stop.png")
|
||||
|
||||
|
||||
keyboardListener = keyboard.Listener(on_release=on_release)
|
||||
keyboardListener.start()
|
||||
|
||||
window.mainloop()
|
||||
window.mainloop()
|
Loading…
Reference in New Issue