-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockkeystate.py
32 lines (28 loc) · 957 Bytes
/
lockkeystate.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
import re
import threading
import time
LOCK_KEYS = ['Caps', 'Num', 'Scroll']
class LockKeyState:
def __init__(self, connection, interval=0.2, onchange=None):
self.connection = connection
self.interval = interval
self.onchange = onchange
self.keys = {k: None for k in LOCK_KEYS}
thread = threading.Thread(target=self.run)
thread.daemon = True
thread.start()
def run(self):
while True:
x = self.connection.run('DISPLAY=:0 xset q', hide=True)
keys = {}
for k in LOCK_KEYS:
m = re.search(k + r'\s*Lock:\s*(on|off)', x.stdout)
if m:
keys[k] = m.group(1) == 'on'
else:
keys[k] = None
if keys != self.keys:
self.keys = keys
if self.onchange:
self.onchange(keys)
time.sleep(self.interval)