0

I've got a USB keyboard connected to my Arduino Uno with a USB Host Shield. I'm reading keystrokes using this example from the USB Host Shield library.

I need to toggle the LEDs in the keyboard (that indicate the state of Caps Lock, Num Lock etc.)

This thread mentions using Usb.setReport(), but this method doesn't seem to exist any more: there's no such method in Usb.cpp in the library. There does seem to be a method called SetReport in the USBHID class, which looks like it takes the same arguments, which I'm trying to use.

What I'm currently doing:

void keyboard_setup(){
    static HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
    delay(200);
    HidKeyboard.SetReportParser(0, &Prs);

    static USBHID usbhid(&Usb);
    uint8_t leds = 1;
#define KBD_ADDR 1
#define KBD_IF 0
    usbhid.SetReport( KBD_ADDR, 0, 1, KBD_IF, 0, &leds );
    delay(100);
    leds = 255;
    usbhid.SetReport( KBD_ADDR, 0, 1, KBD_IF, 0, &leds );
    delay(100);

Neither call to SetReport seems to work, and I'm not certain the values of KBD_ADDR or KBD_IF are correct (I'm using ones that I've seen in examples), or event that calling SetReport is right .

What am I doing wrong?

  • 1
    I posted an answer but deleted it. My 1st thought was that you were creating your own instance of the SetReport method (which you are) that didn't work w/any USB device. But looking at your code again - I'm not so sure. – st2000 Apr 27 at 13:34
  • @st2000 I'm creating my own instance of USBHID, which I don't otherwise use, because I seem to need it to use SetReport(). I'm not sure if that's the right approach though. – Joe Malt Apr 27 at 13:38
  • This is going to take a bit of thinking for me. But I think the best way to go about controlling the keyboard LEDs is to expose SetReport to your Sketch by adding a wrapper method to the USBHost library. Otherwise, I'm thinking you need to setup all the USB stuff in your Sketch - which the driver already is capable of doing. – st2000 Apr 27 at 14:00
  • I thought NicoHood/HID github page was the answer, but I think it works with custom keyboard and does not necessary work w/standard USB keyboard LEDs. – st2000 Apr 27 at 14:38
  • 1
    the NicoHood/HID uses an Arduino to emulate a keyboard .... there is no actual keyboard involved – jsotola Apr 27 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.