Keyboard Unable to determine keyboard Caps

ARduino Leonardo

Master atmega32u4

Libraries used:

#Include "keyboard. H"

When I use the following code to input a text into Notepad

Keyboard. println("hello");

But it became a capital HELLO

Although I can let him press the case key, I don't think it's smart because I can't judge the case of the keyboard

Is there any way to maintain lower case output
:slight_smile:

This is Google translation. It can be bad
This is a well expressed post, but there is no solution here

It's best to start here and add the necessary information to your post: How to get the best out of this forum

1 Like

I think you mean "Keyboard.h".

The Capslock flag is kept by the operating system so all keyboards control the same Capslock. The USB standard includes regular and locking Caps Lock keys. I think that, possibly, if you 'press' and 'release' the locking Caps Lock key that will force the Caps Lock off, where the regular Caps Lock key would toggle.

USB CODE    Keyboard()    Key Location on US-en Keyboard
========    ==========
   ==============================
 57 0x39    193 0xC1    Caps Lock
 71 0x47    207 0xCF    Scroll Lock
 83 0x53    219 0xDB    Num Lock
130 0x82                Locking Caps Lock
131 0x83                Locking Num Lock
132 0x84                Locking Scroll Lock

Unfortunately, the Keyboard() function can't send keycodes higher than 119 and you need 130. You can add these functions to the Kayboard library to get support for all keycodes:

// pressRaw() adds the specified USB raw keycode to the persistent key 
// report and sends the report.
size_t Keyboard_::pressRaw(uint8_t k) 
{
	uint8_t i;
	
	// Add k to the key report only if it's not already present
	// and if there is an empty slot.
	if (_keyReport.keys[0] != k && _keyReport.keys[1] != k && 
		_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
		_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
		
		for (i=0; i<6; i++) {
			if (_keyReport.keys[i] == 0x00) {
				_keyReport.keys[i] = k;
				break;
			}
		}
		if (i == 6) {
			setWriteError();
			return 0;
		}	
	}
	sendReport(&_keyReport);
	return 1;
}


// releaseRaw() takes the specified key out of the persistent key report and
// sends the report.  This tells the OS the key is no longer pressed and that
// it shouldn't be repeated any more.
size_t Keyboard_::releaseRaw(uint8_t k) 
{
	uint8_t i;
	
	// Test the key report to see if k is present.  Clear it if it exists.
	// Check all positions in case the key is present more than once (which it shouldn't be)
	for (i=0; i<6; i++) {
		if (0 != k && _keyReport.keys[i] == k) {
			_keyReport.keys[i] = 0x00;
		}
	}


	sendReport(&_keyReport);
	return 1;
}


size_t Keyboard_::writeRaw(uint8_t c)
{
	uint8_t p = pressRaw(c);  // Keydown
	releaseRaw(c);            // Keyup
	return p;              // just return the result of press() since releaseRaw() almost always returns 1
}

You would add these functions to Keyboard.cpp and modify the Keyboard class in Keyboard.h to add:

  size_t writeRaw(uint8_t k);
  size_t pressRaw(uint8_t k);
  size_t releaseRaw(uint8_t k);
2 Likes

That's very helpful. thank you

There is also a complete library HID-Project by Nico Hood in library manager and on github: GitHub - NicoHood/HID: Bring enhanced HID functions to your Arduino!

And an example from that library to detect the caps lock key: HID/examples/Keyboard/KeyboardLed/KeyboardLed.ino at master · NicoHood/HID · GitHub

Further you could have done some research as the subject has come a long a few times :wink:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.