1

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

julesmcrt
  • 11
  • 2

1 Answers1

2

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

Codebreaker007
  • 2,911
  • 1
  • 10
  • 22
  • I've managed to get rid of a bunch of files ... . Now here is my current understanding of the leds variable assignement : . . - line74 : BootKeyboard_ constructor sets it to null (B00000000) . - line180 : It's reassigned using the USB_RecvControl function . - USB_RecvControl is defined to be USBDevice.recvControl in HID-settings.h line116 . . . But I can't manage to find the code of the USBDevice.recvControl function. I think it's related to "USB/PluggableUSB.h" (see line107 in HID-Settings.h) however I don't find anything that seems helpful online ... – julesmcrt Apr 30 '20 at 12:30
  • You have probably to look into the HID sources (firmware chip) of your Leonardo or in thoseof the HIDdriver for uNO/MEGA in the Nico Lib. About HID drivers you have to search howto write an HID driver Microsoft to get the concept – Codebreaker007 Apr 30 '20 at 12:40
  • Do you know how I could access the source code of the chip ? (atmega32u4) Do you have any idea where the BootKeyboard_::setup function is called and what are the variable setup and the variabletype USBSetup& ? (line107) – julesmcrt Apr 30 '20 at 14:57