-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutoClicker.py
216 lines (182 loc) · 9.42 KB
/
AutoClicker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
First publication date : 2020 / 10 / 30
Author : 張哲銘(Che-Ming Chang)
"""
import tkinter as tk
import tkinter.ttk as ttk
from pynput import keyboard
from pynput.mouse import Button, Controller
import time
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()
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)
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()
self.click_count = 0
self.start_time = time.time()
self.timer_label = ttk.Label(self.master, text="Time elapsed: 0s")
self.timer_label.pack()
self.click_count_label = ttk.Label(self.master, text="Click count: 0")
self.click_count_label.pack()
def click_loop(self):
if not self.break_loop:
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)
self.click_count += 1
self.click_count_label.config(text="Click count: {}".format(self.click_count))
def update_timer(self):
elapsed_time = round(time.time() - self.start_time)
self.timer_label.config(text="Time elapsed: {}s".format(elapsed_time))
self.master.after(1000, self.update_timer)
def on_press(self, key):
if key == self.user_defined_start and self.break_loop:
self.break_loop = False
self.start_time = time.time()
self.update_timer()
self.click_loop()
if key == self.user_defined_stop and not self.break_loop:
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
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
if __name__ == '__main__':
root = tk.Tk()
AutoClicker(root)
root.mainloop()