Fixed Record slower with tkinter

Yippi
This commit is contained in:
LE LOUER Lucas 2023-08-05 21:46:11 +02:00
parent 06a734229c
commit b752f416f4
7 changed files with 85 additions and 6710 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
data/
.idea
.idea
__pycache__

Binary file not shown.

File diff suppressed because it is too large Load Diff

20
hotkey.py Normal file
View File

@ -0,0 +1,20 @@
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()

View File

@ -1,7 +1,7 @@
from pynput import mouse, keyboard
from pynput.mouse import Button
from pynput.keyboard import Key
from tkinter import *
from keyboard import is_pressed
import time
import threading
import json
@ -11,21 +11,23 @@ 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}
record = False
playback = False
def startRecord():
global start_time
global mouse_listener
global keyboard_listener
global macroEvents
global record
record = True
macroEvents = {'events': []}
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')
with mouse_listener, keyboard_listener:
mouse_listener.join()
keyboard_listener.join()
mouse_listener.start()
keyboard_listener.start()
def stopRecord():
mouse_listener.stop()
@ -80,8 +82,6 @@ def on_press(key):
except AttributeError:
macroEvents["events"].append(
{'type': 'keyboardEvent', 'key': str(key), 'timestamp': time.time() - start_time, 'pressed': True})
if key.char == 'o':
stopRecord()
start_time = time.time()
@ -97,6 +97,7 @@ def on_release(key):
def playRec():
playback = True
print('record playing')
for i in range(len(macroEvents["events"])):
time.sleep(macroEvents["events"][i]["timestamp"])
@ -125,3 +126,20 @@ def playRec():
keyboardControl.press(keyToPress)
else:
keyboardControl.release(keyToPress)
playback = False
while True:
if is_pressed('1'):
if record == False:
startRecord()
if is_pressed('2'):
if record == True:
record = False
stopRecord()
if is_pressed('3'):
if playback == False:
playRec()

2
macroLaunch.bat Normal file
View File

@ -0,0 +1,2 @@
@echo off
python macro.py

View File

@ -1,8 +1,22 @@
import threading
from tkinter import *
from tkinter.ttk import *
from pynput import keyboard
import subprocess
import atexit
import os
import signal
import time
from macro import startRecord, stopRecord, playRec
keyboardControl = keyboard.Controller()
def cleanup():
if 'macro_process' in globals():
macro_process.terminate()
atexit.register(cleanup)
macro_process = subprocess.Popen(['python', 'macro.py'])
# Window Setup
window = Tk()
@ -16,18 +30,32 @@ window.config(menu=my_menu)
def startRecordingAndChangeImg():
global stopBtn
global lenghtOfRecord
recordBtn.pack_forget()
stopBtn = Button(window, image=stopImg, command=stopRecordingAndChangeImg)
stopBtn.pack(side=RIGHT, padx=50)
startRecord()
window.wait_variable()
keyboardControl.press('1')
keyboardControl.release('1')
lenghtOfRecord = time.time()
def stopRecordingAndChangeImg():
global recordBtn
global lenghtOfRecord
stopBtn.pack_forget()
recordBtn = Button(window, image=recordImg, command=startRecordingAndChangeImg)
recordBtn.pack(side=RIGHT, padx=50)
stopRecord()
keyboardControl.press('2')
keyboardControl.release('2')
lenghtOfRecord = (time.time() - lenghtOfRecord) + 0.5
def replay():
recordBtn.configure(state=DISABLED)
keyboardControl.press('3')
keyboardControl.release('3')
threading.Thread(target=buttonDisabledToEnable).start()
def buttonDisabledToEnable():
time.sleep(lenghtOfRecord)
recordBtn.configure(state=NORMAL)
# Menu Bar
@ -41,7 +69,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=playRec)
playBtn = Button(window, image=playImg, command=replay)
playBtn.pack(side=LEFT, padx=50)
# Record Button
@ -51,5 +79,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()