Update code to be less fragile
This commit is contained in:
parent
7f1e7a2708
commit
9c7b14e865
@ -54,8 +54,9 @@ class LEDLockStatus(LockStatus):
|
||||
leds.set_brightness(0, leds=[1])
|
||||
|
||||
def after_hid_send(self, sandbox):
|
||||
super().after_hid_send() # Critically important. Do not forget
|
||||
self.set_lock_leds()
|
||||
super().after_hid_send(sandbox) # Critically important. Do not forget
|
||||
if self.report_updated:
|
||||
self.set_lock_leds()
|
||||
|
||||
keyboard.extensions.append(leds)
|
||||
keyboard.extensions.append(LEDLockStatus())
|
||||
|
@ -17,10 +17,11 @@ class LockStatus(Extension):
|
||||
def __init__(self):
|
||||
self.report = None
|
||||
self.hid = None
|
||||
self._report_updated = False
|
||||
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})'
|
||||
@ -40,8 +41,15 @@ class LockStatus(Extension):
|
||||
def after_hid_send(self, sandbox):
|
||||
if self.hid:
|
||||
report = self.hid.get_last_received_report()
|
||||
if report[0] != self.report:
|
||||
if report and report[0] != self.report:
|
||||
self.report = report[0]
|
||||
self._report_updated = True
|
||||
else:
|
||||
self._report_updated = False
|
||||
else:
|
||||
# _report_updated shouldn't ever be True if hid is
|
||||
# falsy, but I would rather be safe than sorry.
|
||||
self._report_updated = False
|
||||
return
|
||||
|
||||
def on_powersave_enable(self, sandbox):
|
||||
@ -50,17 +58,28 @@ class LockStatus(Extension):
|
||||
def on_powersave_disable(self, sandbox):
|
||||
return
|
||||
|
||||
@property
|
||||
def report_updated(self):
|
||||
return self._report_updated
|
||||
|
||||
def check_state(self, lock_code):
|
||||
if self.report:
|
||||
return bool(self.report & lock_code)
|
||||
else:
|
||||
# Just in case, default to False if we don't know anything
|
||||
return False
|
||||
|
||||
def get_num_lock(self):
|
||||
return bool(self.report & LockCode.NUMLOCK)
|
||||
return self.check_state(LockCode.NUMLOCK)
|
||||
|
||||
def get_caps_lock(self):
|
||||
return bool(self.report & LockCode.CAPSLOCK)
|
||||
return self.check_state(LockCode.CAPSLOCK)
|
||||
|
||||
def get_scroll_lock(self):
|
||||
return bool(self.report & LockCode.SCROLLLOCK)
|
||||
return self.check_state(LockCode.SCROLLLOCK)
|
||||
|
||||
def get_compose(self):
|
||||
return bool(self.report & LockCode.COMPOSE)
|
||||
return self.check_state(LockCode.COMPOSE)
|
||||
|
||||
def get_kana(self):
|
||||
return bool(self.report & LockCode.KANA)
|
||||
return self.check_state(LockCode.KANA)
|
||||
|
Loading…
Reference in New Issue
Block a user