I'm using an Arduino Leonardo as a keyboard (BadUSB) and I would like to get the Keyboard LEDs status (e.g. CAPS_LOCK).
Using https://github.com/NicoHood/HID/blob/master/examples/Keyboard/KeyboardLed/KeyboardLed.ino I have managed to get it working using the following code:
(The Keyboard library isn't included because it's somehow already included by HID-Project)
#include <HID-Project.h>
#define LED_NUM_LOCK (1 << 0) // B00000001
#define LED_CAPS_LOCK (1 << 1) // B00000010
#define LED_SCROLL_LOCK (1 << 2) // B00000100
#define LED_COMPOSE (1 << 3) // B00001000
#define LED_KANA (1 << 4) // B00010000
#define LED_POWER (1 << 5) // B00100000
#define LED_SHIFT (1 << 6) // B01000000
#define LED_DO_NOT_DISTURB (1 << 7) // B10000000
void setup() {
Keyboard.begin();
delay(500);
if (BootKeyboard.getLeds() & LED_CAPS_LOCK) {
// caps lock is on
Keyboard.write("a"); // C
} else {
// caps lock is off
Keyboard.write("b"); // e
};
Keyboard.end();
}
void loop() {}
The thing is I would like to add this particular functionnality to my own library (and avoid loading the large HID-Project library).
I belive BootKeyboard.getLeds() returns an 8-bit long number with each character representing the state of every key like defined in the code above. However I don't understand how it's obtain because the code doesn't seems clear to me https://github.com/NicoHood/HID/blob/master/src/SingleReport/BootKeyboard.cpp (lines 190-192):
uint8_t BootKeyboard_::getLeds(void) { return leds; }
as it only returns the variable leds which I guess is assigned somewhere else but I can't manage to find where and how ...