I want to use Arduino Leonardo as USB keyboard. In previous version of my project I wanted to use old keyboard controller, but because I need Arduino for other purposes, I'm trying to merge both parts.
But - I need to use multimedia keys (play, pause, volume up/down etc) and I don't see them on the list here
Check out the HID-Project library.
It is available in the IDE under Tools, Manage Libraries, and after installation se the examples under File, Examples, HID-Project.
It replaces the Keyboard- and Mouse-libraries, and supports both LED-status and an extended list of HID devices.
I have not used it, but tested it by just replacing Mouse- and Keyboard-library on my USB keyboard, and everything seemed to work.
The Keyboard library can only go up to keycode 119. Unfortunately for you, the multimedia keys are just beyond that limit:
(END OF CODES REACHABLE FROM Keyboard.write/press/release)
120 0x78 Keyboard Stop
121 0x79 Keyboard Again
122 0x7A Keyboard Undo
123 0x7B Keyboard Cut
124 0x7C Keyboard Copy
125 0x7D Keyboard Paste
126 0x7E Keyboard Find
127 0x7F Keyboard Mute
128 0x80 Keyboard Volume Up
129 0x81 Keyboard Volume Down
To use those keycodes you have to add some functions to the Keyboard library to allow you to send raw 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 release() almost always returns 1
}
You would add these functions to Keyboard.cpp and modify the Keyboard class in Keyboard.h to add:
I recommend using Nico Hood's HID library. It has all the keys built in including multimedia, normal keyboard, mouse, joystick, gamepad and system (power sleep, etc). And as a bonus it's easy to use.