Is it possible to emulate a 'KEY_UNKNOWN' press and release event with the Leonardo?
I am building a prototype unit and the client originally asked for a 'CTRL' key press and release event upon receiving an input on the Arduino, which is working fine through keyboard emulation. They have now asked if it is possible to give this key_unknown event instead?
This is what they sent me from the Linux input-event-codes.h
Any help/guidance would be extremely appreciated.
Thank you
That's not a key_unknown_event but a key press with that KEY_UNKNOWN scan code. The Keyboard.h library of the IDE doesn't allow to send such codes, so you have to modify it to send such scan codes.
You can extend the library to allow sending direct scan codes. Add these lines to Keyboard.h in the public section of the Keyboard_ class:
size_t Keyboard_::pressSC(uint8_t k)
{
uint8_t i;
// Add sc 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;
}
size_t Keyboard_::releaseSC(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;
}
Hi and thanks for your help so far.
I have tried the above but am getting no output from the Arduino. I have connected to my Pi and run evtest and when programmed with a 'normal' keypress, evtest recognises it but not if I change back to the 'pressSC' etc. Is there a specific place I need to add the code into keyboard.cpp? This is where I've put it.
Thanks again.
Never post pictures of code! I won't ruin my eyes by looking at too tiny characters just because someone isn't able to post text as text.
But if you inserted the code into Keyboard.cpp and Keyboard.h the location of the insert inside Keyboard.cpp doesn't matter (in contrary to Keyboard.h).
Or, if the code compiles, how do you know that it doesn't work? Are you sure the people on Linux know what they're doing? Have you tried with a standard scan code instead of 240?
Hi, I have tried other scan codes and they work fine and show in evtest running on a standalone Pi. When I change it to 240, evtest doesn't acknowledge any input. I am beginning to think that this just isn't an option. I think I am out of options.
I really do appreciate your time on this though.
Without having looked at the corresponding kernel code I guess from the naming of the define that the 240 scan code isn't actually a scan code but is used to flag a special state or error.
As scan codes that high aren't used in the USB keyboards that won't disturb normal usage.
You might have to ask your Linux colleagues what they actually need and why. The reference into the header files is definitely not enough information.