-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
67 lines (58 loc) · 1.81 KB
/
handler.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
import logging
from multiprocessing import shared_memory
from time import time
from pynput.keyboard import HotKey, KeyCode, Listener
from provider import translate
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
class HotKeyHandler:
"""
Events handler
"""
count = 0
updated_at = time()
def event_parser(self) -> None:
"""
Parsing event for hotkey
"""
if self.updated_at >= time() - 4:
if self.count == 0:
self.count = 1
self.updated_at = time()
elif self.count == 1:
self.count = 0
self.updated_at = time()
logging.debug("Hotkey has been Triggered!")
shm = shared_memory.SharedMemory(name="Translator")
provider = shm.buf[:10].tobytes()
shm.close()
provider = provider.decode()
provider = provider.strip()
translate(name=provider)
else:
self.count = 0
self.updated_at = time()
else:
self.count = 1
self.updated_at = time()
def for_canonical(self, f) -> str | KeyCode:
"""
Key normalizer
"""
return lambda k: f(self.l.canonical(k))
def run(self) -> None:
"""
Init persistent hotkey handler function
"""
logging.debug("Starting listener for handling keyboard events")
hotkey = HotKey(
keys=HotKey.parse("<cmd>+c"),
on_activate=self.event_parser,
)
with Listener(
on_press=self.for_canonical(hotkey.press),
on_release=self.for_canonical(hotkey.release),
) as self.l:
self.l.join()