Skip to content

Enable Keyboard library to send key scancodes. #2356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions hardware/arduino/avr/cores/arduino/HID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,61 @@ size_t Keyboard_::release(uint8_t k)
return 1;
}

// press() adds the specified key scancode
// to the persistent key report and sends the report. Because of the way
// USB HID works, the host acts like the key remains pressed until we
// call release(), releaseAll(), or otherwise clear the report and resend.
size_t Keyboard_::press_sc(uint8_t k)
{
uint8_t i;

if (k >= 224 && k <= 231) {
_keyReport.modifiers |= (1 << (k - 224));
}
// Add k to the key report only if it's not already present
// and if there is an empty slot.
else 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;
}

// release() 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_::release_sc(uint8_t k)
{
uint8_t i;

if (k >= 224 && k <= 231) {
_keyReport.modifiers &= ~(1 << (k - 224));
}
// 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)
else {
for (i=0; i<6; i++) {
if (0 != k && _keyReport.keys[i] == k) {
_keyReport.keys[i] = 0x00;
}
}
}
sendReport(&_keyReport);
return 1;
}

void Keyboard_::releaseAll(void)
{
_keyReport.keys[0] = 0;
Expand Down
2 changes: 2 additions & 0 deletions hardware/arduino/avr/cores/arduino/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class Keyboard_ : public Print
virtual size_t write(uint8_t k);
virtual size_t press(uint8_t k);
virtual size_t release(uint8_t k);
virtual size_t press_sc(uint8_t k);
virtual size_t release_sc(uint8_t k);
virtual void releaseAll(void);
};
extern Keyboard_ Keyboard;
Expand Down