Untitled
unknown
plain_text
a month ago
7.5 kB
1
Indexable
Never
import time import ttkbootstrap as ttk from ttkbootstrap.toast import ToastNotification import tkinter as tk import threading import keyboard class AutoClicker: def __init__(self): # Get the screen width and height self.screen_width = 1920 self.screen_height = 1080 self.window = ttk.Window(themename='journal', position=(300, 150), resizable=(False, False)) self.window.title('AutoClicker') self.window.geometry("1280x720") # self.window.attributes('-alpha', 1) self.exit_flag = False self.menu_frame = ttk.Frame(master=self.window) self.menu_frame.pack() self.menu_frame.place(x=0, y=0) self.recordings = {} self.menu_button = ttk.Menubutton(master=self.menu_frame, text="recordings", width=10) self.menu_button.pack() self.menu = tk.Menu(self.menu_button) self.menu_button['menu'] = self.menu self.menu.add_radiobutton(label="play recorded", value='play recorded', command=self.open_play_recordings_window) self.play_recordings_window = ttk.Toplevel(title='Save Recording', position=(500, 300)) self.play_recordings_window.geometry(self.center_window(1280, 720)) self.play_recordings_window.protocol("WM_DELETE_WINDOW", self.close_play_recordings_window) self.play_recordings_window.withdraw() self.play_button_image = ttk.PhotoImage(name="play_button_image", file=r"images/play_button.png") self.play_button = ttk.Button(master=self.window, image=self.play_button_image, command=self.play_button_press) self.play_button.pack() self.play_recordings_window_iterations_label = ttk.Label(master=self.play_recordings_window, text="Number of iterations") self.play_recordings_window_iterations_label.pack() self.notification = ToastNotification( title="Recording will start shortly", message="To finish recording press ']'", alert=True ) self.keyboard_events = [] self.keystroak_time = None self.recording_started = threading.Event() self.recording_stopped = threading.Event() def open_play_recordings_window(self): self.window.withdraw() self.play_recordings_window.deiconify() def close_play_recordings_window(self): self.play_recordings_window.withdraw() self.window.deiconify() def create_recording_countdown_window(self): recording_countdown_window = ttk.Toplevel(alpha=1, position=(500, 300), topmost=True) recording_countdown_window.geometry("200x100") recording_state_image_display = ttk.Label(master=recording_countdown_window, text="Recording starts in:") recording_state_image_display.pack() amount = 3 countdown = ttk.Text(master=recording_countdown_window, width=50, height=100) countdown.pack() for i in range(amount, 0, -1): countdown.delete(1.0, tk.END) countdown.insert(tk.END, str(i)) countdown.update() time.sleep(1) self.keystroak_time = time.time() recording_countdown_window.destroy() def play_button_press(self): self.window.iconbitmap(r"images/play_button_icon.ico") self.window.iconify() def start_stop_recording(): self.recording_started.set() self.recording_stopped.clear() keyboard.wait(']') self.recording_stopped.set() self.recording_started.clear() def keyboard_hook(event): if self.recording_started.is_set() and not self.recording_stopped.is_set(): current_time = time.time() self.keyboard_events.append({ "event":event, "time":current_time - self.keystroak_time }) self.keystroak_time = current_time keyboard.hook(keyboard_hook) self.notification.show_toast() self.create_recording_countdown_window() start_stop_thread = threading.Thread(target=start_stop_recording) start_stop_thread.start() start_stop_thread.join() keyboard.unhook_all() self.notification.hide_toast() self.save_recording(self.keyboard_events) def save_recording(self, keyboard_events): save_window = ttk.Toplevel(title='Save Recording', position=(500, 300), topmost=True) save_window.geometry(self.center_window(400, 100)) save_label = ttk.Label(master=save_window, text="Enter a name for recording:") save_label.pack() recording_name_entry = ttk.Entry(master=save_label) recording_name_entry.pack() recording_name_entry.focus_set() def save_name(): recording_title = recording_name_entry.get() self.recordings[recording_title] = {'keyboard_events':keyboard_events} if len(self.recordings) == 1: button = ttk.Button( master=self.play_recordings_window, text=recording_title, command=lambda title=recording_title: self.play_saved_recording(title) ) button.place(x=50, y=50, width=100, height=100) elif len(self.recordings) == 2: button = ttk.Button( master=self.play_recordings_window, text=recording_title, command=lambda title=recording_title: self.play_saved_recording(title) ) button.place(x=200, y=200, width=100, height=100) elif len(self.recordings) == 3: button = ttk.Button( master=self.play_recordings_window, text=recording_title, command=lambda title=recording_title: self.play_saved_recording(title) ) button.place(x=400, y=400, width=100, height=100) save_window.destroy() self.window.deiconify() save_button = ttk.Button(master=save_window, text='Save', command=save_name) save_button.pack() def play_saved_recording(self, title): self.play_recordings_window.iconify() self.exit_flag = False def keyboard_thread(): # keyboard.play(self.recordings[title]['keyboard_events']) for event in self.keyboard_events: key_stroke = event['event'] time_between_strokes = event['time'] time.sleep(time_between_strokes) if key_stroke.event_type == keyboard.KEY_DOWN: keyboard.press(key_stroke.name) elif key_stroke.event_type == keyboard.KEY_UP: keyboard.release(key_stroke.name) if keyboard.is_pressed('/'): self.exit_flag = True print(self.recordings[title]['keyboard_events']) while not self.exit_flag: keyboard_thread() self.window.deiconify() def center_window(self, width, height): x = (self.screen_width - width) // 2 y = (self.screen_height - height) // 2 return f"{width}x{height}+{x}+{y}" if __name__ == "__main__": test = AutoClicker() test.window.mainloop()