diff --git a/AutoClicker.py b/AutoClicker.py index 3a863fa..b6bd200 100644 --- a/AutoClicker.py +++ b/AutoClicker.py @@ -1,195 +1,191 @@ """ First publication date : 2020 / 10 / 30 - Author : 張哲銘(CHANG, CHE-MING) + Author : 張哲銘(Che-Ming Chang) """ + import tkinter as tk import tkinter.ttk as ttk from pynput import keyboard from pynput.mouse import Button, Controller -root = tk.Tk() -root.title('AutoClicker') -screen_width = root.winfo_screenwidth() -screen_height = root.winfo_screenheight() -root.geometry(str(screen_width//4) + 'x' + str(screen_height//3)) - -time_frame = tk.Frame(root) -time_frame.pack() - -mouse_button_frame = tk.Frame(root) -mouse_button_frame.pack() - -top_frame = tk.Frame(root) -top_frame.pack() - -middle_frame = tk.Frame(root) -middle_frame.pack() - -bottom_frame = tk.Frame(root) -bottom_frame.pack() - -mouse_button_list = { - 0 : Button.left, - 1 : Button.middle, - 2 : Button.right - } - -key_list = { - 0 : keyboard.Key.f1, - 1 : keyboard.Key.f2, - 2 : keyboard.Key.f3, - 3 : keyboard.Key.f4, - 4 : keyboard.Key.f5, - 5 : keyboard.Key.f6, - 6 : keyboard.Key.f7, - 7 : keyboard.Key.f8, - 8 : keyboard.Key.f9, - 9 : keyboard.Key.f10, - 10 : keyboard.Key.f11, - 11 : keyboard.Key.f12 - } - -break_loop = True -time_interval = 1.0 -user_defined_mouse_button = Button.left -user_defined_start = keyboard.Key.f1 -user_defined_stop = keyboard.Key.f2 -user_defined_end = keyboard.Key.f3 -mouse = Controller() - -def click_loop(event = None): - global break_loop, time_interval - if break_loop == False: - mouse.press(user_defined_mouse_button) - mouse.release(user_defined_mouse_button) - root.after(int(time_interval*1000), click_loop) - -def on_press(key): - global break_loop, user_defined_start, user_defined_end - if key == user_defined_start and break_loop: - break_loop = False - click_loop() - if key == user_defined_stop and break_loop == False: - break_loop = True - if key == user_defined_end: - root.destroy() +class AutoClicker: + + mouse_button_list = { + 0 : Button.left, + 1 : Button.middle, + 2 : Button.right + } + + key_list = { + 0 : keyboard.Key.f1, + 1 : keyboard.Key.f2, + 2 : keyboard.Key.f3, + 3 : keyboard.Key.f4, + 4 : keyboard.Key.f5, + 5 : keyboard.Key.f6, + 6 : keyboard.Key.f7, + 7 : keyboard.Key.f8, + 8 : keyboard.Key.f9, + 9 : keyboard.Key.f10, + 10 : keyboard.Key.f11, + 11 : keyboard.Key.f12 + } + + # Default value + break_loop = True + time_interval = 1.0 + user_defined_mouse_button = Button.left + user_defined_start = keyboard.Key.f1 + user_defined_stop = keyboard.Key.f2 + user_defined_end = keyboard.Key.f3 + + def __init__(self, master): + self.master = master + master.title("AutoClicker") + master.geometry(str(master.winfo_screenwidth() // 4) + 'x' + str(master.winfo_screenheight() // 3)) + + self.time_frame = tk.Frame(master) + self.time_frame.pack() + + self.mouse_button_frame = tk.Frame(master) + self.mouse_button_frame.pack() -listener = keyboard.Listener(on_press = on_press) -listener.start() + self.top_frame = tk.Frame(master) + self.top_frame.pack() + + self.middle_frame = tk.Frame(master) + self.middle_frame.pack() + + self.bottom_frame = tk.Frame(master) + self.bottom_frame.pack() + + self.label_time = ttk.Label(self.time_frame, text = "Time interval : 1.0(s)") + self.label_time.grid(column = 1, row = 0) + + self.label_mouse_button = ttk.Label(self.mouse_button_frame, text = "Mouse button : Left") + self.label_mouse_button.grid(column = 1, row = 0) + + self.label_start = ttk.Label(self.top_frame, text = "Start key : F1") + self.label_start.grid(column = 1, row = 0) -def set_time(): - global _time, time_interval - try: - if float(_time.get()) < 0: + self.label_stop = ttk.Label(self.middle_frame, text = "Stop key : F2") + self.label_stop.grid(column = 1, row = 0) + + self.label_end = ttk.Label(self.bottom_frame, text = "End key : F3") + self.label_end.grid(column = 1, row = 0) + + self.time_string = tk.StringVar() + self.time_string.set("1.0") + tk.Entry(self.time_frame, textvariable = self.time_string, width = 18).grid(column = 1, row = 1) + + self.mouse_button_index = ttk.Combobox(self.mouse_button_frame, width = 15, textvariable = tk.StringVar(), state = "readonly") + self.mouse_button_index['values'] = (Button.left, Button.middle, Button.right) + self.mouse_button_index.grid(column = 1, row = 1) + self.mouse_button_index.current(0) + + self.key_start_index = ttk.Combobox(self.top_frame, width = 15, textvariable = tk.StringVar(), state = "readonly") + self.key_start_index['values'] = (keyboard.Key.f1, keyboard.Key.f2, keyboard.Key.f3, keyboard.Key.f4, keyboard.Key.f5, keyboard.Key.f6, keyboard.Key.f7, keyboard.Key.f8, keyboard.Key.f9, keyboard.Key.f10, keyboard.Key.f11, keyboard.Key.f12) + self.key_start_index.grid(column = 1, row = 1) + self.key_start_index.current(0) + + self.key_stop_index = ttk.Combobox(self.middle_frame, width = 15, textvariable = tk.StringVar(), state = "readonly") + self.key_stop_index['values'] = (keyboard.Key.f1, keyboard.Key.f2, keyboard.Key.f3, keyboard.Key.f4, keyboard.Key.f5, keyboard.Key.f6, keyboard.Key.f7, keyboard.Key.f8, keyboard.Key.f9, keyboard.Key.f10, keyboard.Key.f11, keyboard.Key.f12) + self.key_stop_index.grid(column = 1, row = 1) + self.key_stop_index.current(1) + + self.key_end_index = ttk.Combobox(self.bottom_frame, width = 15, textvariable = tk.StringVar(), state = "readonly") + self.key_end_index['values'] = (keyboard.Key.f1, keyboard.Key.f2, keyboard.Key.f3, keyboard.Key.f4, keyboard.Key.f5, keyboard.Key.f6, keyboard.Key.f7, keyboard.Key.f8, keyboard.Key.f9, keyboard.Key.f10, keyboard.Key.f11, keyboard.Key.f12) + self.key_end_index.grid(column = 1, row = 1) + self.key_end_index.current(2) + + ttk.Button(self.time_frame, text = "Set", command = self.set_time).grid(column = 2, row = 1) + ttk.Button(self.mouse_button_frame, text = "Set", command = self.set_mouse_button).grid(column = 2, row = 1) + ttk.Button(self.top_frame, text = "Set", command = self.key_list_start).grid(column = 2, row = 1) + ttk.Button(self.middle_frame, text = "Set", command = self.key_list_stop).grid(column = 2, row = 1) + ttk.Button(self.bottom_frame, text = "Set", command = self.key_list_end).grid(column = 2, row = 1) + + self.mouse = Controller() + + self.key_listener = keyboard.Listener(on_press = self.on_press) + self.key_listener.start() + + def click_loop(self): + if self.break_loop == False: + self.mouse.press(self.user_defined_mouse_button) + self.mouse.release(self.user_defined_mouse_button) + self.master.after(int(self.time_interval * 1000), self.click_loop) + + def on_press(self, key): + if key == self.user_defined_start and self.break_loop: + self.break_loop = False + self.click_loop() + if key == self.user_defined_stop and self.break_loop == False: + self.break_loop = True + if key == self.user_defined_end: + self.master.destroy() + + def set_time(self): + try: + if float(self.time_string.get()) <= 0: + return + self.time_interval = float(self.time_string.get()) + except: return - time_interval = float(_time.get()) - except: - return - label_time.config(text = "Time interval:" + _time.get() + "(s)") - -def set_mouse_button(): - global user_defined_mouse_button, mouse_button_list - user_defined_mouse_button = mouse_button_list.get(mouse_button_index.current(), None) - label_mouse_button.config(text = "Mouse button:" + str(user_defined_mouse_button).replace('Button.', '').title()) - -def key_list_start(): - global user_defined_start, user_defined_stop, user_defined_end, key_list - temp = user_defined_start - label_start.config(text = "Start key:F" + str(key_start_index.current()+1)) - user_defined_start = key_list.get(key_start_index.current(), None) - if user_defined_start == user_defined_stop or user_defined_start == user_defined_end: - if user_defined_start == user_defined_stop: - num = ''.join([x for x in str(temp) if x.isdigit()]) - label_stop.config(text = "Stop key:F" + num) - key_stop_index.current(int(num)-1) - user_defined_stop = temp - else: - num = ''.join([x for x in str(temp) if x.isdigit()]) - label_end.config(text = "End key:F" + num) - key_end_index.current(int(num)-1) - user_defined_end = temp + self.label_time.config(text = "Time interval : " + self.time_string.get() + "(s)") + + def set_mouse_button(self): + self.user_defined_mouse_button = self.mouse_button_list.get(self.mouse_button_index.current(), None) + self.label_mouse_button.config(text = "Mouse button : " + str(self.user_defined_mouse_button).replace('Button.', '').title()) + + def key_list_start(self): + temp_key = self.user_defined_start + self.label_start.config(text = "Start key : F" + str(self.key_start_index.current() + 1)) + self.user_defined_start = self.key_list.get(self.key_start_index.current(), None) + if self.user_defined_start == self.user_defined_stop or self.user_defined_start == self.user_defined_end: + if self.user_defined_start == self.user_defined_stop: + num = ''.join([x for x in str(temp_key) if x.isdigit()]) + self.label_stop.config(text = "Stop key : F" + num) + self.key_stop_index.current(int(num) - 1) + self.user_defined_stop = temp_key + else: + num = ''.join([x for x in str(temp_key) if x.isdigit()]) + self.label_end.config(text = "End key : F" + num) + self.key_end_index.current(int(num) - 1) + self.user_defined_end = temp_key + + def key_list_stop(self): + temp_key = self.user_defined_stop + self.label_stop.config(text = "Stop key : F" + str(self.key_stop_index.current() + 1)) + self.user_defined_stop = self.key_list.get(self.key_stop_index.current(), None) + if self.user_defined_stop == self.user_defined_start or self.user_defined_stop == self.user_defined_end: + if self.user_defined_stop == self.user_defined_start: + num = ''.join([x for x in str(temp_key) if x.isdigit()]) + self.label_start.config(text = "Start key : F" + num) + self.key_start_index.current(int(num) - 1) + self.user_defined_start = temp_key + else: + num = ''.join([x for x in str(temp_key) if x.isdigit()]) + self.label_end.config(text = "End key : F" + num) + self.key_end_index.current(int(num) - 1) + self.user_defined_end = temp_key + + def key_list_end(self): + temp_key = self.user_defined_end + self.label_end.config(text = "End key : F" + str(self.key_end_index.current() + 1)) + self.user_defined_end = self.key_list.get(self.key_end_index.current(), None) + if self.user_defined_end == self.user_defined_start or self.user_defined_end == self.user_defined_stop: + if self.user_defined_end == self.user_defined_start: + num = ''.join([x for x in str(temp_key) if x.isdigit()]) + self.label_start.config(text = "Start key : F" + num) + self.key_start_index.current(int(num) - 1) + self.user_defined_start = temp_key + else: + num = ''.join([x for x in str(temp_key) if x.isdigit()]) + self.label_stop.config(text = "Stop key : F" + num) + self.key_stop_index.current(int(num) - 1) + self.user_defined_stop = temp_key -def key_list_stop(): - global user_defined_start, user_defined_stop, user_defined_end, key_list - temp = user_defined_stop - label_stop.config(text = "Stop key:F" + str(key_stop_index.current()+1)) - user_defined_stop = key_list.get(key_stop_index.current(), None) - if user_defined_stop == user_defined_start or user_defined_stop == user_defined_end: - if user_defined_stop == user_defined_start: - num = ''.join([x for x in str(temp) if x.isdigit()]) - label_start.config(text = "Start key:F" + num) - key_start_index.current(int(num)-1) - user_defined_start = temp - else: - num = ''.join([x for x in str(temp) if x.isdigit()]) - label_end.config(text = "End key:F" + num) - key_end_index.current(int(num)-1) - user_defined_end = temp - -def key_list_end(): - global user_defined_start, user_defined_stop, user_defined_end, key_list - temp = user_defined_end - label_end.config(text = "End key:F" + str(key_end_index.current()+1)) - user_defined_end = key_list.get(key_end_index.current(), None) - if user_defined_end == user_defined_start or user_defined_end == user_defined_stop: - if user_defined_end == user_defined_start: - num = ''.join([x for x in str(temp) if x.isdigit()]) - label_start.config(text = "Start key:F" + num) - key_start_index.current(int(num)-1) - user_defined_start = temp - else: - num = ''.join([x for x in str(temp) if x.isdigit()]) - label_stop.config(text = "Stop key:F" + num) - key_stop_index.current(int(num)-1) - user_defined_stop = temp - -label_time = ttk.Label(time_frame, text = "Time interval:1.0(s)") -label_time.grid(column = 1, row = 0) - -label_mouse_button = ttk.Label(mouse_button_frame, text = "Mouse button:Left") -label_mouse_button.grid(column = 1, row = 0) - -label_start = ttk.Label(top_frame, text = "Start key:F1") -label_start.grid(column = 1, row = 0) - -label_stop = ttk.Label(middle_frame, text = "Stop key:F2") -label_stop.grid(column = 1, row = 0) - -label_end = ttk.Label(bottom_frame, text = "End key:F3") -label_end.grid(column = 1, row = 0) - -_time = tk.StringVar() -_time.set("1.0") -content = tk.Entry(time_frame, textvariable = _time, width = 18).grid(column=1, row=1) - -mouse_button = tk.StringVar() -mouse_button_index = ttk.Combobox(mouse_button_frame, width = 15, textvariable = mouse_button, state = "readonly") -mouse_button_index['values'] = (Button.left, Button.middle, Button.right) -mouse_button_index.grid(column = 1, row = 1) -mouse_button_index.current(0) - -key_start = tk.StringVar() -key_start_index = ttk.Combobox(top_frame, width = 15, textvariable = key_start, state = "readonly") -key_start_index['values'] = (keyboard.Key.f1, keyboard.Key.f2, keyboard.Key.f3, keyboard.Key.f4, keyboard.Key.f5, keyboard.Key.f6, keyboard.Key.f7, keyboard.Key.f8, keyboard.Key.f9, keyboard.Key.f10, keyboard.Key.f11, keyboard.Key.f12) -key_start_index.grid(column = 1, row = 1) -key_start_index.current(0) - -key_stop = tk.StringVar() -key_stop_index = ttk.Combobox(middle_frame, width = 15, textvariable = key_stop, state = "readonly") -key_stop_index['values'] = (keyboard.Key.f1, keyboard.Key.f2, keyboard.Key.f3, keyboard.Key.f4, keyboard.Key.f5, keyboard.Key.f6, keyboard.Key.f7, keyboard.Key.f8, keyboard.Key.f9, keyboard.Key.f10, keyboard.Key.f11, keyboard.Key.f12) -key_stop_index.grid(column = 1, row = 1) -key_stop_index.current(1) - -key_end = tk.StringVar() -key_end_index = ttk.Combobox(bottom_frame, width = 15, textvariable = key_end, state = "readonly") -key_end_index['values'] = (keyboard.Key.f1, keyboard.Key.f2, keyboard.Key.f3, keyboard.Key.f4, keyboard.Key.f5, keyboard.Key.f6, keyboard.Key.f7, keyboard.Key.f8, keyboard.Key.f9, keyboard.Key.f10, keyboard.Key.f11, keyboard.Key.f12) -key_end_index.grid(column = 1, row = 1) -key_end_index.current(2) - -action_time = ttk.Button(time_frame, text = "Set", command = set_time).grid(column = 2, row = 1) -action_mouse_button = ttk.Button(mouse_button_frame, text = "Set", command = set_mouse_button).grid(column = 2, row = 1) -action_start = ttk.Button(top_frame, text = "Set", command = key_list_start).grid(column = 2, row = 1) -action_stop = ttk.Button(middle_frame, text = "Set", command = key_list_stop).grid(column = 2, row = 1) -action_end = ttk.Button(bottom_frame, text = "Set", command = key_list_end).grid(column = 2, row = 1) - -root.mainloop() +if __name__ == '__main__': + root = tk.Tk() + AutoClicker(root) + root.mainloop()