-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtakugen.py
130 lines (105 loc) · 4.41 KB
/
takugen.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
from pynput.keyboard import Key, Controller, Listener
from pynput import keyboard
from collections import deque
import time
import threading
import settings
class Language:
def __init__(self, language='none', vowels=[], unicode_table={}, hasSecondary=False):
self._language = language
self._vowels = vowels
self._unicode_table = unicode_table
self._hasSecondary = hasSecondary
class Translator(threading.Thread):
controller = Controller()
isTypingUnicode = False
previousKeys = deque([' ',' ',' '])
language = None
previousCharCount = 1
_is_running = False
def __init__(self, language):
threading.Thread.__init__(self)
self.language = language
def removeCurrentCharacter(self, currentKey):
self.controller.release(currentKey)
self.keyTap(Key.backspace)
time.sleep(0.1)
def unicodeTyper(self, tr_unicode):
self.isTypingUnicode = True
with self.controller.pressed(Key.ctrl), self.controller.pressed(Key.shift):
self.controller.type('u'+tr_unicode)
self.isTypingUnicode = False
def translateKeyPress(self, keyPress, tr_unicode):
tr_unicode = self.checkForCombination(keyPress, tr_unicode)
tr_unicode = tr_unicode['primary'] if not self.language._hasSecondary else self.selectPrimaryOrSecondary(keyPress, tr_unicode)
self.removeCurrentCharacter(keyPress)
# used for single hangul jama
if len(tr_unicode) == 4:
self.previousCharCount = 1
self.unicodeTyper(tr_unicode)
# used for double hangul jama
else:
self.previousCharCount = 2
self.unicodeTyper(tr_unicode[0:4])
self.unicodeTyper(tr_unicode[4:])
def keyTap(self, key):
self.controller.press(key)
self.controller.release(key)
# returns primary or secondary
def selectPrimaryOrSecondary(self, keyPress, tr_unicode):
if self.previousKeys[2] in self.language._vowels:
return tr_unicode['secondary']
else:
return tr_unicode['primary']
def checkForCombination(self, keyPress, tr_unicode):
# Try for 2-keys combinations
if self.previousKeys[2] in tr_unicode:
# Try for 3-keys combinations
if self.previousKeys[1] in tr_unicode[self.previousKeys[2]]:
# time.sleep() provides enough time for OS to receive backspace event
if self.previousCharCount == 2:
self.keyTap(Key.backspace)
time.sleep(0.1)
self.keyTap(Key.backspace)
time.sleep(0.1)
self.keyTap(Key.backspace)
time.sleep(0.1)
return tr_unicode[self.previousKeys[2]][self.previousKeys[1]]
else:
# time.sleep() provides enough time for OS to receive backspace event
if self.previousCharCount == 2:
self.keyTap(Key.backspace)
time.sleep(0.1)
self.keyTap(Key.backspace)
time.sleep(0.1)
return tr_unicode[self.previousKeys[2]]
# Default to single key press
else:
return tr_unicode
def on_press(self, key):
# do if the program is not typing unicodes
if not self.isTypingUnicode and isinstance(key, keyboard._xorg.KeyCode) and not (type(self.language) is None):
try:
tr_key, tr_unicode = key.char, self.language._unicode_table[key.char]
self.translateKeyPress(tr_key, tr_unicode)
self.previousKeys.popleft()
self.previousKeys.append(key.char)
except KeyError:
print("cannot find key '{0}' in table".format(key.char))
if settings.APP_CONFIG['DEBUG']:
print('previousKeys: ' + str(self.previousKeys))
def on_release(self, key):
if key == Key.esc:
self.stop()
return False
def stop(self):
print("Stopping")
self._is_running = False
def run(self):
print(f'Starting {self.language._language}')
self._is_running = True
while(self._is_running):
# Collect events until released
with Listener(on_press=self.on_press, on_release=self.on_release) as listener:
listener.join()
print("Task finished")