-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_utility.py
53 lines (43 loc) · 1.5 KB
/
keyboard_utility.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
import infi.systray
import keyboard
from threading import Thread
class KeyboardBlocker:
def __init__(self):
self.blocked = False
self.blocked_icon = "icons\mouse.ico"
self.unblocked_icon = "icons\keyboard.ico"
def block_keyboard(self):
self.blocked = True
for i in range(150):
keyboard.block_key(i)
def unblock_keyboard(self, icon, *args):
self.blocked = False
for i in range(150):
keyboard.unblock_key(i)
icon.update(icon=self.unblocked_icon, hover_text="Keyboard unblocked")
def toggle_keyboard_block(self, icon):
if self.blocked:
self.unblock_keyboard(icon)
icon.update(icon=self.unblocked_icon, hover_text="Keyboard unblocked")
else:
self.block_keyboard()
icon.update(icon=self.blocked_icon, hover_text="Keyboard blocked")
def on_quit(self, icon):
pass # Do nothing here
def get_menu_options(self):
return (
("Block Keyboard", None, self.toggle_keyboard_block),
("Unblock Keyboard", None, self.unblock_keyboard),
)
def run(self):
infi.systray.SysTrayIcon(
self.unblocked_icon,
"Keyboard unblocked",
self.get_menu_options(),
on_quit=self.on_quit,
).start()
if __name__ == "__main__":
kb_blocker = KeyboardBlocker()
t = Thread(target=kb_blocker.run)
t.start()
t.join() # Wait for the thread to finish before exiting