Add callback to lock status

This commit is contained in:
Hendrix 2022-08-06 17:27:14 -04:00 committed by xs5871
parent ad6f4e5f05
commit 0b23da551a
2 changed files with 38 additions and 4 deletions

View File

@ -16,8 +16,38 @@ when the lock is enabled and `False` otherwise.
|Method |Description |
|--------------------------|------------|
|`locks.get_caps_lock() ` |Num Lock |
|`locks.get_num_lock() ` |Num Lock |
|`locks.get_caps_lock() ` |Caps Lock |
|`locks.get_scroll_lock() `|Scroll Lock |
|`locks.get_compose() ` |Compose |
|`locks.get_kana() ` |Kana |
## React to Lock Status Changes
Lock Status will accept a callback function that is invoked when the status of
a lock changes. When the function is invoked, it will have a Lock Status object
passed to it.
```python
import board
from kb import KMKKeyboard
from kmk.extensions.lock_status import LockStatus
from kmk.extensions.LED import LED
keyboard = KMKKeyboard()
keyboard.extensions.append(LED(led_pin=[board.GP27, board.GP28]))
def toggle_lock_leds(self):
if self.get_caps_lock():
keyboard.leds.set_brightness(50, leds=[0])
else:
keyboard.leds.set_brightness(0, leds=[0])
if self.get_scroll_lock():
keyboard.leds.set_brightness(50, leds=[1])
else:
keyboard.leds.set_brightness(0, leds=[1])
keyboard.extensions.append(LockStatus(toggle_lock_leds))
```

View File

@ -14,12 +14,14 @@ class LockCode:
class LockStatus(Extension):
def __init__(self):
self.report = 0x00
def __init__(self, fn=None):
self.report = None
self.hid = None
self.fn = fn
for device in usb_hid.devices:
if device.usage == HIDUsage.KEYBOARD:
self.hid = device
self.hid.get_last_received_report()
def __repr__(self):
return f'LockStatus(report={self.report})'
@ -41,6 +43,8 @@ class LockStatus(Extension):
report = self.hid.get_last_received_report()
if report[0] != self.report:
self.report = report[0]
if self.fn:
self.fn(self)
return
def on_powersave_enable(self, sandbox):
@ -62,4 +66,4 @@ class LockStatus(Extension):
return bool(self.report & LockCode.COMPOSE)
def get_kana(self):
return bool(self.report & LockCode.KANA)
return bool(self.report & LockCode.KANA)