Skip to content

Commit 9367ca2

Browse files
committed
Enable Keyboard library to send key scancodes.
1 parent 804112a commit 9367ca2

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

hardware/arduino/avr/cores/arduino/HID.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,61 @@ size_t Keyboard_::release(uint8_t k)
494494
return 1;
495495
}
496496

497+
// press() adds the specified key scancode
498+
// to the persistent key report and sends the report. Because of the way
499+
// USB HID works, the host acts like the key remains pressed until we
500+
// call release(), releaseAll(), or otherwise clear the report and resend.
501+
size_t Keyboard_::press_sc(uint8_t k)
502+
{
503+
uint8_t i;
504+
505+
if (k >= 224 && k <= 231) {
506+
_keyReport.modifiers |= (1 << (k - 224));
507+
}
508+
// Add k to the key report only if it's not already present
509+
// and if there is an empty slot.
510+
else if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
511+
_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
512+
_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
513+
514+
for (i=0; i<6; i++) {
515+
if (_keyReport.keys[i] == 0x00) {
516+
_keyReport.keys[i] = k;
517+
break;
518+
}
519+
}
520+
if (i == 6) {
521+
setWriteError();
522+
return 0;
523+
}
524+
}
525+
sendReport(&_keyReport);
526+
return 1;
527+
}
528+
529+
// release() takes the specified key out of the persistent key report and
530+
// sends the report. This tells the OS the key is no longer pressed and that
531+
// it shouldn't be repeated any more.
532+
size_t Keyboard_::release_sc(uint8_t k)
533+
{
534+
uint8_t i;
535+
536+
if (k >= 224 && k <= 231) {
537+
_keyReport.modifiers &= ~(1 << (k - 224));
538+
}
539+
// Test the key report to see if k is present. Clear it if it exists.
540+
// Check all positions in case the key is present more than once (which it shouldn't be)
541+
else {
542+
for (i=0; i<6; i++) {
543+
if (0 != k && _keyReport.keys[i] == k) {
544+
_keyReport.keys[i] = 0x00;
545+
}
546+
}
547+
}
548+
sendReport(&_keyReport);
549+
return 1;
550+
}
551+
497552
void Keyboard_::releaseAll(void)
498553
{
499554
_keyReport.keys[0] = 0;

hardware/arduino/avr/cores/arduino/USBAPI.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ class Keyboard_ : public Print
179179
virtual size_t write(uint8_t k);
180180
virtual size_t press(uint8_t k);
181181
virtual size_t release(uint8_t k);
182+
virtual size_t press_sc(uint8_t k);
183+
virtual size_t release_sc(uint8_t k);
182184
virtual void releaseAll(void);
183185
};
184186
extern Keyboard_ Keyboard;

0 commit comments

Comments
 (0)