0
votes

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 ...

1

1 Answers

1
votes

Look at

  • line53/54 (_hidReportDescriptorKeyboard definition in progmem)
  • line168-172 Setting the value of the leds (0/1)
  • line190-192 Getting the values

of the BootKeyboard.cpp and the definition is in the header file BootKeyboard.h

  • line 78 as uint8_t

as some of the functions use the _hidReportDescriptorKeyboard you should try touse the files as base and then get rid of the functions you do not need. Ifyou have a minimalworking set you can start to refactor the code -for code analysis and dependencies you can use a static tool (snapshot) like doxygen or an IDE supporting code analysis
EDIT
Heres the wiki to the Hood Loader get some tea and time to read and learn
See those links for details and info and Alternative Keyboard Firmware These are all repos on github you could search for