Leonardo keyboard leds emulation?

I am trying to create an USB keyboard controller with an Arduino Leonardo, but I can't find the way to emulate the keyboard leds. The PC sends the leds status to the keyboard but the Keyboard object on Arduino seems not to have any read function or any way to get the leds status.

The PC sends the leds status to the keyboard

What LED and what status are you talking about? Shift lock, num lock? If so, didn't YOU send that information to the PC?

PaulS: What LED and what status are you talking about? Shift lock, num lock? If so, didn't YOU send that information to the PC?

In the very very old XT keyboards, the num lock/caps lock/scroll lock status where sent only from keyboard to PC. Xt keyboards didn't receive info from the PC. But since many years with the AT keyboards, the communication is bidirectional, then you can connect two keyboards to the PC and hitting num lock in one affects the led status in all keyboards. Then, you can change the caps/scrool/num lock statys by software and this status is shown in the leds. Remember that keyboards does not send "letters" to the PC; it sends row/column of the key. Caps lock key is only another key with it's own row/column scan code and hitting it not necessarily will cause the caps lock led to get on or off. It is controlled by software in the PC.

Your original posting made no mention of a second keyboard, only the one being emulated by the Leonardo, hence the comment that as you were setting the Shift lock/Num lock status using the Arduino you had sent the status to the PC and did not need to receive it back.

UKHeliBob: Your original posting made no mention of a second keyboard, only the one being emulated by the Leonardo, hence the comment that as you were setting the Shift lock/Num lock status using the Arduino you had sent the status to the PC and did not need to receive it back.

It does not matter how-many keyboards have you connected to the PC. The system is made in a way that allows many keyboards, then, the lock status is managed by the operating system and NOT by the keyboard. Then, the leds are controlled by the PC software and NOT by the keyboard itself. When you press caps lock, it does not turns on the caps lock led. The heyboard send the scan code (row/column) to the PC. The PC software changes the status of a flag and sends back to the keyboard the command for tuning on or off the led. The led status is not decided by the keyboard chip: it is completely controlled by the PC's operating system. Then, the keyboad chip must be able to receive controls from the PC... what seems to be lacking in Arduino HID implementation...

Then, the keyboad chip must be able to receive controls from the PC... what seems to be lacking in Arduino HID implementation...

It's open source. Quit whining and add it, if you need it.

PaulS:

Then, the keyboad chip must be able to receive controls from the PC… what seems to be lacking in Arduino HID implementation…

It’s open source. Quit whining and add it, if you need it.

Of corse, just now I am checking the code. But first I wanted to ask if there where some way of doing it without modifying the libraries.

Did you succed in this? I added support for USB keyboard LED reports myself over the weekend, maybe this is still of interest to you …

usb_key_leds.diff (3.01 KB)

The LED patch by hartmut_holzgraefe worked great for my project!

I've got a 20 year old Maxiswitch keyboard whose controller board died and I thought it would be a good project to replace the original Intel 8085 chip with an Arduino. This is a 124 key keyboard with extra left side function keys etc.

I used a Sparkfun Pro Micro arduino and my program does all the keyboard row and column scanning mapping into the correct HID keyboard codes. Thanks to the patch to the USB library, the shift/num/scroll lock LEDs work too. Plus thanks to the Arduino, it is now a USB keyboard instead of the ancient AT style.

Hi guys,

Could anyone explain how to install the "The LED patch by hartmut_holzgraefe"

I am not familiar with .diff patchs , but I guess I would need the original to see the difference to alter the files

or could someone post their patched USAPI.h and HID.cpp ?

I have been looking for an example all night :~, so I could modify it to show a "MUTE" LED

http://www.usb.org/developers/devclass_docs/Hut1_12v2.pdf


Page 61

section 11 LEDPAGE(0x08)

09 Mute LED

Thanks in advance

Rupert

A patch file is a file that indicates how to modify a given source code (or any text file) to obtain a new one with some changes.

Essentialy, a patch file contains data indicating something like “add this line, remove that one, etc.”. It is generated by the unix command “diff” that compares two files and responds with this data. Having the patch file and the original file, one can use the command “patch” to obtain the modified version.

[edit] (found the patch file). I am applying it:

cd /usr/share/arduino/hardware

patch -p2 </home/anv/Descargas/usb_key_leds.diff

patching file arduino/cores/arduino/HID.cpp
patching file arduino/cores/arduino/USBAPI.h
Hunk #1 succeeded at 111 (offset -1 lines).
Hunk #2 succeeded at 131 (offset -1 lines).
Hunk #3 succeeded at 140 (offset -1 lines).

I’m attaching the result files

I’m attaching the patched files resulting of applying usb_key_leds.diff to arduino version 1.0.5

Not tested it yet.

HID.cpp (14.3 KB)

USBAPI.h (5.41 KB)

Once these files are replaced, how what code do we use in a sketch to listen for a num/scroll/caps lock led from the PC?

hartmut_holzgraefe's code works like a charm. GREATE! Thank you very much for sharing the code!

Hi all,

I'm new to the Arduino world. I just bought an Arduino Micro and I was looking at a way to detect whether capslock has been set to ON on the host or not. I don't understand how to use function getLedStatus in the patch provided by hartmut_holzgraefe to check the status of KEY_CAPS_LOCK, can you provide an example?

Many thanks in advance, Valerio

OK I found a good explanation on how to _ledStatus variable is structured at
http://forum.pjrc.com/threads/25368-How-do-I-receive-a-numlock-capslock-LED-signal-from-the-PC

In what follows a very simple sketch that turns on the LED on Arduino Micro if CAPS_LOCK is ON.

uint8_t keyboard_leds;

void setup() {
  Keyboard.begin();
}

void loop() {
  delay(1000);
  keyboard_leds = Keyboard.getLedStatus();
  if (keyboard_leds & (1<<USB_LED_CAPS_LOCK))
  {
     digitalWrite(13, HIGH);
  }
  while(true);
}

Hello, I need to detect CapsLock, NumLock on the laptop. On the computer modified files work, but function on the laptop Keyboard.getLedStatus() back zero.

OK, I've found the file(!), patched it (have to be root), but now how to compile it??? I can't find a makefile anywhere, or even a .configure -???

Regarding the suggestion above to see https://forum.pjrc.com/threads/25368-How-do-I-receive-a-numlock-capslock-LED-signal-from-the-PC I can't find any "usb_keyboard.h" file -that wouldn't be because I'm using Leonardo instead of Teensy, would it?

Hi guys,

I am trying to implement a mute led on my leonardo volume knob, anyone have any ideas?
I found other indicator references

#define LED_SHIFT 0x07
#define LED_MUTE 0x09 // OOC - Indicates that the amplifier audio is shut off, 11.3 Consumer Indicators
#define LED_MICROPHONE 0x21
#define LED_PAUSE 0x37
#define LED_PLAY 0x36
#define LED_STOP 0x33
#define LED_FAST_FORWARD 0x35
#define LED_REWIND 0x34
#define LED_REPEAT 0x10

I hobbled together a working example for caps lock as valeriog’s example didn’t work for me.

/* 
 USB HID Keyboard LED Indicator example for the Leonardo, IDE 1.0.5
 Hobbled together from the references below,
 
 Illuminates the LED on pin 13. In sync with keyboard CAPS LOCK on a host PC keyboard
 
 https://forum.pjrc.com/threads/25368-How-do-I-receive-a-numlock-capslock-LED-signal-from-the-PC
 http://forum.arduino.cc/index.php?topic=173583.15
 http://forum.arduino.cc/index.php?topic=166638.0
 http://www.usb.org/developers/hidpage/Hut1_12v2.pdf
 https://github.com/coopermaa/USBKeyboard
 http://coopermaa2nd.blogspot.co.uk/2011/11/blog-post.html
 */

unsigned char keyboard_leds;


#define LED_CAPS_LOCK 1



int led = 13;                          // Set Indicator LED pin

void setup() {
  Serial.begin(9600);

  Keyboard.begin();
  pinMode(led, OUTPUT);                // Set LED indicator Pin as output
}

void loop() {

  delay(500);                          // Slow the serial monitor output

  keyboard_leds = Keyboard.getLedStatus();

  Serial.println("Listening...");      // Let us know somethin is happening on the serial monitor
  Serial.println (keyboard_leds);      // Output value to the  serial monitor

  if (keyboard_leds & (1<<LED_CAPS_LOCK))
  {
    digitalWrite(led, HIGH);           // Turn indicator LED on
  }
  else 
  {
    digitalWrite(led, LOW);            // Turn indicator LED off
  }

}

Cheers

koogar

I searched for LED status and found this thread. I tested out hartmut_holzgraefe's patch and found that after I repeatedly pressed Caps Lock multiple times, my board froze. I modified the code as follows (one line change), and it no longer hangs :

    if (HID_SET_REPORT == r)
    {
      if (setup.wLength == 2) 
      {
        uint8_t data[2];
        if (2 == USB_RecvControl(data, 2)) 
        {
          Keyboard.setLedStatus(data[1]);
          return true; // added this line
        }
      }
    }

To answer an earlier question, modify HID.cpp and USBAPI.h, rebuild your project, and it will pick up your changes.