Skip to content

Commit 1f4ced5

Browse files
authored
Merge pull request #62 from jfurcean/add-led-status
Add the ability to use the LED indicators for CAPS_LOCK et al.
2 parents 3e2c45b + c260b91 commit 1f4ced5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

adafruit_hid/keyboard.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
class Keyboard:
2323
"""Send HID keyboard reports."""
2424

25+
LED_NUM_LOCK = 0x01
26+
"""LED Usage ID for Num Lock"""
27+
LED_CAPS_LOCK = 0x02
28+
"""LED Usage ID for Caps Lock"""
29+
LED_SCROLL_LOCK = 0x04
30+
"""LED Usage ID for Scroll Lock"""
31+
LED_COMPOSE = 0x08
32+
"""LED Usage ID for Compose"""
33+
2534
# No more than _MAX_KEYPRESSES regular keys may be pressed at once.
2635

2736
def __init__(self, devices):
@@ -143,3 +152,29 @@ def _remove_keycode_from_report(self, keycode):
143152
for i in range(_MAX_KEYPRESSES):
144153
if self.report_keys[i] == keycode:
145154
self.report_keys[i] = 0
155+
156+
@property
157+
def led_status(self):
158+
"""Returns the last received report"""
159+
return self._keyboard_device.last_received_report
160+
161+
def led_on(self, led_code):
162+
"""Returns whether an LED is on based on the led code
163+
164+
Examples::
165+
166+
import usb_hid
167+
from adafruit_hid.keyboard import Keyboard
168+
from adafruit_hid.keycode import Keycode
169+
import time
170+
171+
# Press and release CapsLock.
172+
kbd.press(Keycode.CAPS_LOCK)
173+
time.sleep(.09)
174+
kbd.release(Keycode.CAPS_LOCK)
175+
176+
# Check status of the LED_CAPS_LOCK
177+
print(kbd.led_on(Keyboard.LED_CAPS_LOCK))
178+
179+
"""
180+
return bool(self.led_status[0] & led_code)

0 commit comments

Comments
 (0)