Added save file

This commit is contained in:
LE LOUER Lucas 2023-08-06 14:16:55 +02:00
parent b752f416f4
commit accc5eb3b5
2 changed files with 49 additions and 31 deletions

View File

@ -1,7 +1,8 @@
from pynput import mouse, keyboard
from pynput.mouse import Button
from pynput.keyboard import Key
from keyboard import is_pressed
from keyboard import is_pressed, read_key
from tkinter import filedialog
import time
import threading
import json
@ -13,13 +14,12 @@ special_keys = {"Key.shift": Key.shift, "Key.tab": Key.tab, "Key.caps_lock": Key
record = False
playback = False
fileAlreadySaved = False
saveFile = False
def startRecord():
global start_time
global mouse_listener
global keyboard_listener
global macroEvents
global record
global start_time, mouse_listener, keyboard_listener, macroEvents, record, recordLenght
record = True
macroEvents = {'events': []}
start_time = time.time()
@ -32,19 +32,29 @@ def startRecord():
def stopRecord():
mouse_listener.stop()
keyboard_listener.stop()
json_macroEvents = json.dumps(macroEvents, indent=4)
with open(os.path.join("C:/Users/Lucas/Desktop/autre/projey python/macro-recorder/data/test.json"), "w") as macroRecord:
macroRecord.write(json_macroEvents)
print('record stopped')
# def saveMacro():
# json_macroEvents = json.dumps(macroEvents, indent=4)
# with open(os.path.join("C:/Users/lucas/OneDrive/Bureau/projet/data", macroName + ".json"), "w") as macroRecord:
# macroRecord.write(json_macroEvents)
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
@ -97,6 +107,7 @@ def on_release(key):
def playRec():
global playback
playback = True
print('record playing')
for i in range(len(macroEvents["events"])):
@ -132,14 +143,23 @@ def playRec():
while True:
if is_pressed('1'):
if record == False:
if record == False:
if is_pressed('1'):
print("pressed 1")
startRecord()
if is_pressed('2'):
if record == True:
if playback == False:
if is_pressed('3'):
playRec()
if (record == False and playback == False):
if saveFile == False:
if is_pressed('ctrl+s'):
saveFile = True
saveMacro()
if record == True:
if is_pressed('2'):
print(read_key())
print("pressed 2")
record = False
stopRecord()
if is_pressed('3'):
if playback == False:
playRec()

View File

@ -4,8 +4,6 @@ from tkinter.ttk import *
from pynput import keyboard
import subprocess
import atexit
import os
import signal
import time
keyboardControl = keyboard.Controller()
@ -23,6 +21,7 @@ window = Tk()
window.title("MacroRecorder")
window.geometry("350x200")
window.iconbitmap("assets/logo.ico")
window.resizable(False,False)
my_menu = Menu(window)
window.config(menu=my_menu)
@ -31,21 +30,20 @@ window.config(menu=my_menu)
def startRecordingAndChangeImg():
global stopBtn
global lenghtOfRecord
recordBtn.pack_forget()
stopBtn.pack(side=RIGHT, padx=50)
keyboardControl.press('1')
keyboardControl.release('1')
lenghtOfRecord = time.time()
recordBtn.configure(image=stopImg, command=stopRecordingAndChangeImg)
def stopRecordingAndChangeImg():
global recordBtn
global lenghtOfRecord
stopBtn.pack_forget()
recordBtn = Button(window, image=recordImg, command=startRecordingAndChangeImg)
recordBtn.pack(side=RIGHT, padx=50)
keyboardControl.press('2')
keyboardControl.release('2')
lenghtOfRecord = (time.time() - lenghtOfRecord) + 0.5
lenghtOfRecord = (time.time() - lenghtOfRecord) + 1
recordBtn.configure(image=recordImg, command=startRecordingAndChangeImg)
playBtn.configure(state=NORMAL)
def replay():
recordBtn.configure(state=DISABLED)
@ -69,7 +67,7 @@ file_menu.add_command(label="Settings", command=window.quit)
# Play Button
playImg = PhotoImage(file=r"assets/button/play.png")
playBtn = Button(window, image=playImg, command=replay)
playBtn = Button(window, image=playImg, command=replay, state=DISABLED)
playBtn.pack(side=LEFT, padx=50)
# Record Button
@ -79,7 +77,7 @@ recordBtn.pack(side=RIGHT, padx=50)
# Stop Button
stopImg = PhotoImage(file=r"assets/button/stop.png")
stopBtn = Button(window, image=stopImg, command=stopRecordingAndChangeImg)
window.mainloop()