This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.py
133 lines (133 loc) · 4.66 KB
/
hook.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
from pynput import mouse
import pynput
from pynput.mouse import Listener
import threading
import pyautogui
import pickle
import tkinter as tk
from tkinter import simpledialog
import time
pressedList = []
root = tk.Tk()
default = 'AutoClicker'
root.title(default)
root.geometry('240x220')
title = lambda tl: root.title(tl)
def makelisten():
title('Listening')
def mousehook(x, y, button, pressed):
if pressed:
pressedList.append([x,y])
if not pressed:
return False
with pynput.mouse.Listener(on_click=mousehook) as listener:
listener.join()
title(default)
def keyhook():
title('Listening')
def keyboardhook(key):
pressedList.append(key)
return False
with pynput.keyboard.Listener(on_press=keyboardhook) as listener:
listener.join()
title(default)
def record():
title('Recording')
n = simpledialog.askfloat("Ввод", "Количество секунд", parent=root,minvalue=0)
if n is not None:
tm = time.time()
tmvectors = []
def on_click(x, y, button, pressed):
if time.time() - tm <= n:
if pressed:
tmvectors.append(time.time() - tm)
try:
newvector = (tmvectors[-2] - tmvectors[-1]) * -1
pressedList.append(newvector)
except:
pass
pressedList.append([x,y])
else:
return False
def on_press(key):
if time.time() - tm <= n:
tmvectors.append(time.time() - tm)
try:
newvector = (tmvectors[-2] - tmvectors[-1]) * -1
pressedList.append(newvector)
except:
pass
pressedList.append(key)
else:
return False
def start_keyboard():
with pynput.keyboard.Listener(on_press=on_press) as listener:
listener.join()
def start_mouse():
with pynput.mouse.Listener(on_click=on_click) as listener:
listener.join()
t1 = threading.Thread(target=start_mouse)
t2 = threading.Thread(target=start_keyboard)
t1.start()
t2.start()
def killall():
try:
t1._stop()
t2._stop()
t3._stop()
print('Запись остановлена')
except:
print('Запись остановлена')
title(default)
t3 = threading.Timer(n, killall)
t3.start()
else: title(default)
def repeat():
title('Repeating')
n = simpledialog.askinteger("Ввод", "Количество повторений", parent=root,minvalue=0)
if n is not None and len(pressedList) != 0:
for j in range(n):
ms = mouse.Controller()
kb = pynput.keyboard.Controller()
for i in range(len(pressedList)):
if type(pressedList[i]) is list:
pyautogui.moveTo(pressedList[i])
ms.press(mouse.Button.left)
ms.release(mouse.Button.left)
elif type(pressedList[i]) is float:
time.sleep(pressedList[i])
else:
kb.press(pressedList[i])
kb.release(pressedList[i])
title(default)
else: title(default)
def clear():
del pressedList[:]
title(default)
def read():
title('Reading')
global pressedList
try:
n = simpledialog.askstring(title='Loader', prompt='Load configuration')
with open ('presets/'+n+'.cfg', 'rb') as fp:
pressedList = pickle.load(fp)
except: pass
title(default)
def save():
title('Saving')
try:
n = simpledialog.askstring(title='Saver', prompt='Save configuration')
with open('presets/'+n+'.cfg', 'wb') as fp:
pickle.dump(pressedList, fp)
except: pass
title(default)
display= lambda: print(pressedList)
tk.Button(text='Click', command = makelisten, width = 10).pack()
tk.Button(text='Key', command = keyhook, width = 10).pack()
tk.Button(text='Repeat', command = repeat, width = 10).pack()
tk.Button(text='Clear', command= clear, width = 10).pack()
tk.Button(text='Display', command= display, width = 10).pack()
tk.Button(text='Read', command= read, width = 10).pack()
tk.Button(text='Save', command= save, width = 10).pack()
tk.Button(text='Record', command= record, width = 10).pack()
root.mainloop()