diff --git a/docs/extensions.md b/docs/extensions.md index daba745..02373c4 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -9,6 +9,7 @@ extensions are - [International](international.md): Adds international keycodes - [LED](led.md): Adds backlight support. This is for monocolor backlight, not RGB +- [LockStatus](lock_status.md): Exposes host-side locks like caps or num lock. - [MediaKeys](media_keys.md): Adds support for media keys such as volume - [RGB](rgb.md): RGB lighting for underglow. Will work on most matrix RGB as will be treated the same as underglow. diff --git a/docs/lock_status.md b/docs/lock_status.md new file mode 100644 index 0000000..0c713c0 --- /dev/null +++ b/docs/lock_status.md @@ -0,0 +1,23 @@ +# Lock Status +This extension exposes host-side locks like caps or num lock. + +## Enabling the extension +```python +from kmk.extensions.lock_status import LockStatus + +locks = LockStatus() +keyboard.extensions.append(locks) + +``` + +## Read Lock Status +Lock states can be retrieved with getter methods and are truth valued -- `True` +when the lock is enabled and `False` otherwise. + +|Method |Description | +|--------------------------|------------| +|`locks.get_caps_lock() ` |Num Lock | +|`locks.get_caps_lock() ` |Caps Lock | +|`locks.get_scroll_lock() `|Scroll Lock | +|`locks.get_compose() ` |Compose | +|`locks.get_kana() ` |Kana | diff --git a/kmk/extensions/lock_status.py b/kmk/extensions/lock_status.py new file mode 100644 index 0000000..8402d51 --- /dev/null +++ b/kmk/extensions/lock_status.py @@ -0,0 +1,64 @@ +import usb_hid + +from kmk.extensions import Extension +from kmk.hid import HIDUsage + + +class LockCode: + NUMLOCK = 0x01 + CAPSLOCK = 0x02 + SCROLLLOCK = 0x04 + COMPOSE = 0x08 + KANA = 0x10 + RESERVED = 0x20 + + +class LockStatus(Extension): + def __init__(self): + self.report = 0x00 + self.hid = None + for device in usb_hid.devices: + if device.usage == HIDUsage.KEYBOARD: + self.hid = device + + def __repr__(self): + return ('LockStatus(report={})').format(self.report) + + def during_bootup(self, sandbox): + return + + def before_matrix_scan(self, sandbox): + return + + def after_matrix_scan(self, sandbox): + return + + def before_hid_send(self, sandbox): + return + + def after_hid_send(self, sandbox): + report = self.hid.get_last_received_report() + if report[0] != self.report: + self.report = report[0] + return + + def on_powersave_enable(self, sandbox): + return + + def on_powersave_disable(self, sandbox): + return + + def get_num_lock(self): + return bool(self.report & LockCode.NUMLOCK) + + def get_caps_lock(self): + return bool(self.report & LockCode.CAPSLOCK) + + def get_scroll_lock(self): + return bool(self.report & LockCode.SCROLLLOCK) + + def get_compose(self): + return bool(self.report & LockCode.COMPOSE) + + def get_kana(self): + return bool(self.report & LockCode.KANA)