From 9abe689fe2c96a716a652545cbb48e6284a30b51 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sat, 20 Mar 2021 20:06:35 -0400 Subject: [PATCH 1/2] add led_status and led_on --- adafruit_hid/keyboard.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/adafruit_hid/keyboard.py b/adafruit_hid/keyboard.py index 64c03a2..d386f9e 100644 --- a/adafruit_hid/keyboard.py +++ b/adafruit_hid/keyboard.py @@ -22,6 +22,11 @@ class Keyboard: """Send HID keyboard reports.""" + LED_NUM_LOCK = 0x01 + LED_CAPS_LOCK = 0x02 + LED_SCROLL_LOCK = 0x04 + LED_COMPOSE = 0x08 + # No more than _MAX_KEYPRESSES regular keys may be pressed at once. def __init__(self, devices): @@ -143,3 +148,12 @@ def _remove_keycode_from_report(self, keycode): for i in range(_MAX_KEYPRESSES): if self.report_keys[i] == keycode: self.report_keys[i] = 0 + + @property + def led_status(self): + """Returns the last received report""" + return self._keyboard_device.last_received_report + + def led_on(self, led_code): + """Returns whether an LED is on based on the led code""" + return bool(self.led_status[0] & led_code) From c260b91c94f08e3b4d4aa22bc997cdabcea6bb15 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sat, 20 Mar 2021 22:44:52 -0400 Subject: [PATCH 2/2] add example and docstrings --- adafruit_hid/keyboard.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/adafruit_hid/keyboard.py b/adafruit_hid/keyboard.py index d386f9e..d55a916 100644 --- a/adafruit_hid/keyboard.py +++ b/adafruit_hid/keyboard.py @@ -23,9 +23,13 @@ class Keyboard: """Send HID keyboard reports.""" LED_NUM_LOCK = 0x01 + """LED Usage ID for Num Lock""" LED_CAPS_LOCK = 0x02 + """LED Usage ID for Caps Lock""" LED_SCROLL_LOCK = 0x04 + """LED Usage ID for Scroll Lock""" LED_COMPOSE = 0x08 + """LED Usage ID for Compose""" # No more than _MAX_KEYPRESSES regular keys may be pressed at once. @@ -155,5 +159,22 @@ def led_status(self): return self._keyboard_device.last_received_report def led_on(self, led_code): - """Returns whether an LED is on based on the led code""" + """Returns whether an LED is on based on the led code + + Examples:: + + import usb_hid + from adafruit_hid.keyboard import Keyboard + from adafruit_hid.keycode import Keycode + import time + + # Press and release CapsLock. + kbd.press(Keycode.CAPS_LOCK) + time.sleep(.09) + kbd.release(Keycode.CAPS_LOCK) + + # Check status of the LED_CAPS_LOCK + print(kbd.led_on(Keyboard.LED_CAPS_LOCK)) + + """ return bool(self.led_status[0] & led_code)