-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumlockui.py
More file actions
46 lines (34 loc) · 1.29 KB
/
numlockui.py
File metadata and controls
46 lines (34 loc) · 1.29 KB
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
# Copyright (C) 2020 Daniel Fitzgerald
import ctypes
from ctypes import wintypes
from win32con import VK_NUMLOCK
import win32con
import logging
import tknumlock
from threading import Thread
byref = ctypes.byref
user32 = ctypes.windll.user32
MSG_ID_NUM_LOCK = 1
def main():
tk = tknumlock.NumLockTk()
# Tk requires to run on main thread, therefore hot key messenger must run on a separate thread.
Thread(target=(lambda: register_hot_key(tk))).start()
tk.mainloop()
def register_hot_key(tk):
# Register Num Lock as hot key.
# Reference: http://timgolden.me.uk/python/win32_how_do_i/catch_system_wide_hotkeys.html
logging.log(logging.DEBUG, "Registering num lock as hot key.")
user32.RegisterHotKey(None, MSG_ID_NUM_LOCK, None, VK_NUMLOCK)
try:
msg = wintypes.MSG()
while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam == MSG_ID_NUM_LOCK:
tk.show(is_num_lock_on())
finally:
pass
def is_num_lock_on():
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeystate?redirectedfrom=MSDN
return bool(user32.GetKeyState(VK_NUMLOCK) & 1) # Least significant bit represents toggle state.
if __name__ == '__main__':
main()