-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
118 lines (89 loc) · 3.04 KB
/
main.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
from peewee import SqliteDatabase, Model, CharField, TextField, BigIntegerField
import atexit
from pynput import keyboard
import pyautogui
import re
location = './data.db'
if __name__ == '__main__':
prev_key = None
prev_prev_key = None
result = None
typed_chars = []
is_record = True
db = SqliteDatabase('./data.db')
def exit_handler():
db.close()
print('Exiting Program Gracefully!')
quit(0)
atexit.register(exit_handler)
class Snippet(Model):
key = CharField()
value = TextField()
freq = BigIntegerField()
class Meta:
database = db
if not Snippet.table_exists():
Snippet.create_table()
Snippet.get_or_create(key='hw', value='Hello World!', freq=0)
def check():
global typed_chars
global result
word = ''.join(typed_chars)
pattern = re.compile('[\W_]+')
pattern.sub('', word)
result = Snippet.select().where(Snippet.key == word)
if result.exists():
return True
def assist():
global result
global typed_chars
global is_record
t = result.get().value
is_record = False
for _ in typed_chars:
pyautogui.press('backspace')
pyautogui.typewrite(t, interval=0.0)
pyautogui.press('space')
typed_chars = []
is_record = True
def on_press(key):
global is_record
global prev_key
global prev_prev_key
global typed_chars
if not is_record:
return True
try:
if prev_key == keyboard.Key.ctrl and key.char == 'c':
return False
if key.char == 'c' and prev_key == keyboard.Key.shift_l \
and (prev_prev_key == keyboard.Key.ctrl_l or prev_prev_key == keyboard.Key.cmd_l):
return False
pattern = re.compile('\w')
is_ok = pattern.match(key.char)
if is_ok:
typed_chars.append(key.char)
if key.char == '=':
if check():
pyautogui.press('backspace', interval=0.0)
assist()
except AttributeError:
if key == keyboard.Key.space:
typed_chars = []
if key == keyboard.Key.backspace \
and (not (prev_prev_key == keyboard.Key.ctrl_l or prev_prev_key == keyboard.Key.cmd_l)) \
and len(typed_chars) > 0:
typed_chars.pop()
if key == keyboard.Key.f8:
if check():
assist()
if key == keyboard.Key.backspace \
and (prev_prev_key == keyboard.Key.ctrl_l or prev_prev_key == keyboard.Key.cmd_l):
if hasattr(prev_key, 'char'):
if prev_key.char == '\x01':
typed_chars = []
finally:
prev_prev_key = prev_key
prev_key = key
with keyboard.Listener(on_press=on_press) as listener:
listener.join()