How to determing 'Caps Lock' status

Using a Leonardo, I know I can send a 'Caps Lock' key to the host computer with:

Keyboard.write( KEY_CAPS_LOCK );

But do a get a response? I would like the onboard LED (pin 13) to reflect the 'Caps Lock' status.

I could track the toggle of the lock in my code but that would not help if the lock is changed by a different keyboard/HID device. (is that redundant, Human Interface Device device?)

Put a light sensor over the Caps Lock LED on the keyboard & read it?

CrossRoads:
Put a light sensor over the Caps Lock LED on the keyboard & read it?

Ha Ha

is this what you were looking for?

/* Arduino USB Keyboard HID demo */
/* Read the keyboard leds status */

#define LED_NUMLOCK    (1 << 0)
#define LED_CAPSLOCK   (1 << 1)
#define LED_SCROLLLOCK (1 << 2)

const int ledPin =  13;
uint8_t keyNone[8] = { 0, 0, 0, 0, 0, 0, 0 };

void setup() 
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
  uint8_t ledStatus;
  
  delay(100);
  Serial.write(keyNone, 8);        // Release key

  // keyboard firmware will send back 1 byte every time it receives a key
  // this byte represents the LED status for the keyboard:
  // Bit 0 - NUMLOCK, Bit 1 - CAPSLOCK, Bit 2 - SCROLLLOCK
  ledStatus = Serial.read();
  if (ledStatus & LED_CAPSLOCK) {
    digitalWrite(ledPin, HIGH);      
  } else {
    digitalWrite(ledPin, LOW);
  }    
}

This is close but it does not appear to be for the Leonardo using the designated 'Keyboard' class.

Is this for the UNO or Mega with the Serial interface chip flashed with the HID firmware?

CrossRoads:
Put a light sensor over the Caps Lock LED on the keyboard & read it?

I fixed my Caps Lock key status by putting a faucet washer under it so it can't be pressed.

I fixed my Caps Lock key status by putting a faucet washer under it so it can't be pressed.

Part number and schematic please. That damned key is the stupidest one on my keyboard. It is always pressing itself. Can't be my fat fingers doing it.

@PaulS: eBay is selling a book titled "Diet for Fat FIngers" as an eBook...

I appreciate everyone's concern about my Caps Lock being turned on by accident. I happen to like using my Caps Lock.

My interest is knowing the status of the Caps Lock, as well as the Num Lock, the Scroll Lock and any other status information that a keyboard (or mouse) can receive.

The communications between the keyboard and compute has always be two way, however, it don't see it in the core Arduino code. Is it there and I am missing it?

My interest is knowing the status of the Caps Lock, as well as the Num Lock, the Scroll Lock and any other status information that a keyboard (or mouse) can receive.

The keyboard itself is maintaining that status. I have an external keyboard on my notebook. If I press the caps lock key on it, the corresponding LED turns on and off. If I press the caps lock on the notebook the keyboard doesn't get any information about thats. If I hit it on both keyboards, both LEDs turn on but the computer didn't activate the functionality for any of them.
So if your Leonardo is the keyboard it has to keep track of the caps lock key usage and display the status according to the tracked usage of that key.

pylon:
The keyboard itself is maintaining that status.

That's not my understanding. The LEDs in the keyboard are under the control of the host computer. It might appear that the LED control is something that happens spontaneously within the keyboard, but it isn't; the host computer is what controls whether capslock is engaged and it will be updating this in response to the capslock keypress events and sending LED commands to the keyboard via USB. It is up to the implementation of the USB slave software whether it makes those commands available to your sketch - I'm not familiar with the USB HID implementation used on the Leonardo so I don't know whether it does.

pylon:
The keyboard itself is maintaining that status. I have an external keyboard on my notebook. If I press the caps lock key on it, the corresponding LED turns on and off. If I press the caps lock on the notebook the keyboard doesn't get any information about thats. If I hit it on both keyboards, both LEDs turn on but the computer didn't activate the functionality for any of them.
So if your Leonardo is the keyboard it has to keep track of the caps lock key usage and display the status according to the tracked usage of that key.

I do not doubt what you are seeing. However, it is my understanding that if the status of the Caps Lock, Num Lock, ... changes, then the OS should inform all attached keyboards. That is true if the keyboard is a PS/2 attached keyboard or a USB HID.
This may be one of those things that is not done as well as it should be.

Has this issue been solved?

jumpjack:
Has this issue been solved?

It looks like message #7 from this thread solves this issue... although I don't know how to implement it in my system...

jumpjack:
It looks like message #7 from this thread solves this issue... although I don't know how to implement it in my system...

That contains a 'diff' file used to patch the source files. Reply #11 in the thread has the pre-patched version of the two files. They are from 2014 so they may not match the latest sources. I'd compare them to the latest and make sure all of the latest bug fixes and features are included.

Hi, to my knowledge there is no way of having the LED information using the Keyboard class.

If you are using a Teensy with Teensyduino libraries however, you can access it using the C interface:

// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
volatile uint8_t keyboard_leds=0;

This var is accessible from the sketch.