parent
4f55eac141
commit
9badc3b3c9
|
@ -1,3 +1,5 @@
|
|||
data/
|
||||
data
|
||||
.idea
|
||||
__pycache__
|
||||
venv
|
||||
software.spec
|
20
hotkey.py
20
hotkey.py
|
@ -1,20 +0,0 @@
|
|||
from pynput import keyboard
|
||||
from macro import startRecord, stopRecord, playRec
|
||||
keyboardControl = keyboard.Controller()
|
||||
|
||||
|
||||
|
||||
def on_press(key):
|
||||
if key.char == "2":
|
||||
key
|
||||
if key.char == "3":
|
||||
playRec()
|
||||
|
||||
|
||||
keyboard_listener = keyboard.Listener(on_release=on_release)
|
||||
|
||||
with keyboard_listener:
|
||||
keyboard_listener.join()
|
||||
|
||||
|
||||
|
80
macro.py
80
macro.py
|
@ -43,7 +43,6 @@ def startRecord():
|
|||
start_time = time.time()
|
||||
mouse_listener = mouse.Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll)
|
||||
keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
|
||||
print('record started')
|
||||
mouse_listener.start()
|
||||
keyboard_listener.start()
|
||||
|
||||
|
@ -54,26 +53,6 @@ def stopRecord():
|
|||
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')
|
||||
if macroSaved is not None:
|
||||
macroPath = macroSaved.name
|
||||
macroSaved.write(json_macroEvents)
|
||||
macroSaved.close()
|
||||
fileAlreadySaved = True
|
||||
else:
|
||||
open(os.path.join(macroPath), "w").write(json_macroEvents)
|
||||
time.sleep(0.5)
|
||||
saveFile = False
|
||||
|
||||
|
||||
def on_move(x, y):
|
||||
|
@ -103,7 +82,13 @@ def on_scroll(x, y, dx, dy):
|
|||
|
||||
|
||||
def on_press(key):
|
||||
global start_time
|
||||
global start_time, playback, keyboard_listener
|
||||
if (record == False and playback == True):
|
||||
if is_pressed('escape'):
|
||||
keyboardControl.release(keyboard.Key.esc)
|
||||
playback = False
|
||||
keyboard_listener.stop()
|
||||
else:
|
||||
try:
|
||||
macroEvents["events"].append(
|
||||
{'type': 'keyboardEvent', 'key': key.char, 'timestamp': time.time() - start_time, 'pressed': True})
|
||||
|
@ -125,10 +110,14 @@ def on_release(key):
|
|||
|
||||
|
||||
def playRec():
|
||||
global playback
|
||||
global playback, keyboard_listener
|
||||
playback = True
|
||||
print('record playing')
|
||||
keyboard_listener = keyboard.Listener(on_press=on_press)
|
||||
keyboard_listener.start()
|
||||
macroEvents = json.load(open(os.path.join(appdata_local + "/temprecord.json"), "r"))
|
||||
for i in range(len(macroEvents["events"])):
|
||||
if playback == False:
|
||||
return
|
||||
time.sleep(macroEvents["events"][i]["timestamp"])
|
||||
if macroEvents["events"][i]["type"] == "cursorMove":
|
||||
mouseControl.position = (macroEvents["events"][i]["x"], macroEvents["events"][i]["y"])
|
||||
|
@ -150,8 +139,7 @@ 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:
|
||||
|
@ -159,6 +147,9 @@ def playRec():
|
|||
playback = False
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
while True:
|
||||
if (record == False and playback == False):
|
||||
if is_pressed('o'):
|
||||
|
@ -169,47 +160,14 @@ while True:
|
|||
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()
|
||||
print('playback started')
|
||||
playRec()
|
||||
|
||||
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'):
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
@echo off
|
||||
python macro.py
|
61
play.py
61
play.py
|
@ -1,61 +0,0 @@
|
|||
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)
|
69
software.py
69
software.py
|
@ -1,6 +1,8 @@
|
|||
import json
|
||||
import threading
|
||||
from tkinter import *
|
||||
from tkinter.ttk import *
|
||||
from tkinter import filedialog
|
||||
from pynput import keyboard
|
||||
import subprocess
|
||||
import atexit
|
||||
|
@ -11,6 +13,8 @@ import os
|
|||
playback2 = False
|
||||
record2 = False
|
||||
recordSet = False
|
||||
saveFile = False
|
||||
fileAlreadySaved = False
|
||||
keyboardControl = keyboard.Controller()
|
||||
|
||||
|
||||
|
@ -20,6 +24,7 @@ if os.path.isdir(appdata_local) == False:
|
|||
os.mkdir(appdata_local)
|
||||
|
||||
|
||||
|
||||
def on_release(key):
|
||||
global record2, playback2, recordBtn, playBtn, recordSet
|
||||
try:
|
||||
|
@ -39,12 +44,7 @@ def on_release(key):
|
|||
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():
|
||||
|
@ -105,18 +105,49 @@ def replay(pressKey=True):
|
|||
|
||||
|
||||
|
||||
def saveMacro():
|
||||
keyboardControl.press(keyboard.Key.ctrl_l)
|
||||
keyboardControl.press('s')
|
||||
keyboardControl.release(keyboard.Key.ctrl_l)
|
||||
keyboardControl.release('s')
|
||||
def saveMacroAs(e=None):
|
||||
global macroPath, fileAlreadySaved
|
||||
if (record2 == False and playback2 == False and recordSet == True):
|
||||
macroSaved = filedialog.asksaveasfile(filetypes=[('Json Files', '*.json')], defaultextension='.json')
|
||||
if macroSaved is not None:
|
||||
macroContent = open(os.path.join(appdata_local + "/temprecord.json"), "r")
|
||||
macroEvents = json.load(macroContent)
|
||||
json_macroEvents = json.dumps(macroEvents, indent=4)
|
||||
open(macroSaved.name, "w").write(json_macroEvents)
|
||||
macroPath = macroSaved.name
|
||||
macroSaved.close()
|
||||
fileAlreadySaved = True
|
||||
|
||||
def saveMacroAs():
|
||||
keyboardControl.press(keyboard.Key.ctrl_l)
|
||||
keyboardControl.press(keyboard.Key.alt)
|
||||
keyboardControl.press('s')
|
||||
|
||||
|
||||
def saveMacro(e=None):
|
||||
if(record2 == False and playback2 == False and recordSet == True):
|
||||
if fileAlreadySaved == True:
|
||||
macroContent = open(os.path.join(appdata_local + "/temprecord.json"), "r")
|
||||
macroSaved = open(os.path.join(macroPath), "w")
|
||||
macroEvents = json.load(macroContent)
|
||||
json_macroEvents = json.dumps(macroEvents, indent=4)
|
||||
macroSaved.write(json_macroEvents)
|
||||
print('saved')
|
||||
else:
|
||||
saveMacroAs()
|
||||
|
||||
def loadMacro(e=None):
|
||||
global macroPath, recordSet
|
||||
if (record2 == False and playback2 == False):
|
||||
macroFile = filedialog.askopenfile(filetypes=[('Json Files', '*.json')], defaultextension='.json')
|
||||
macroContent = open(macroFile.name)
|
||||
macroPath = macroFile.name
|
||||
macroEvents = json.load(macroContent)
|
||||
json_macroEvents = json.dumps(macroEvents, indent=4)
|
||||
open(os.path.join(appdata_local + "/temprecord.json"), "w").write(json_macroEvents)
|
||||
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
|
||||
macroFile.close()
|
||||
|
||||
|
||||
def newMacro():
|
||||
global recordSet
|
||||
|
@ -127,13 +158,10 @@ def newMacro():
|
|||
recordBtn.configure(image=recordImg, command=startRecordingAndChangeImg)
|
||||
file_menu.entryconfig('Save', state=DISABLED)
|
||||
file_menu.entryconfig('Save as', state=DISABLED)
|
||||
file_menu.entryconfig('New', 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)
|
||||
|
@ -156,6 +184,9 @@ recordBtn.pack(side=RIGHT, padx=50)
|
|||
# Stop Button
|
||||
stopImg = PhotoImage(file=r"assets/button/stop.png")
|
||||
|
||||
window.bind('<Control-Shift-S>', saveMacroAs)
|
||||
window.bind('<Control-s>', saveMacro)
|
||||
window.bind('<Control-l>', loadMacro)
|
||||
|
||||
keyboardListener = keyboard.Listener(on_release=on_release)
|
||||
keyboardListener.start()
|
||||
|
|
Loading…
Reference in New Issue