Skip to content

Commit 97d9ce9

Browse files
committed
fixed logic error in Keyboard.release() - now removes every occurrence of a key if it's present more than once
1 parent b86ec27 commit 97d9ce9

File tree

1 file changed

+6
-8
lines changed
  • hardware/arduino/cores/arduino

1 file changed

+6
-8
lines changed

hardware/arduino/cores/arduino/HID.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ size_t Keyboard_::press(uint8_t k)
435435
setWriteError();
436436
return 0;
437437
}
438-
if (k & 0x80) {
438+
if (k & 0x80) { // it's a capital letter or other character reached with shift
439439
_keyReport.modifiers |= 0x02; // the left shift modifier
440440
k &= 0x7F;
441441
}
@@ -478,22 +478,20 @@ size_t Keyboard_::release(uint8_t k)
478478
if (!k) {
479479
return 0;
480480
}
481-
if (k & 0x80) {
481+
if (k & 0x80) { // it's a capital letter or other character reached with shift
482482
_keyReport.modifiers &= ~(0x02); // the left shift modifier
483483
k &= 0x7F;
484484
}
485485
}
486486

487487
// Test the key report to see if k is present. Clear it if it exists.
488+
// Check all positions in case the key is present more than once (which it shouldn't be)
488489
for (i=0; i<6; i++) {
489-
if (_keyReport.keys[i] == k) {
490+
if (0 != k && _keyReport.keys[i] == k) {
490491
_keyReport.keys[i] = 0x00;
491-
break;
492492
}
493493
}
494-
if (i == 6) {
495-
return 0;
496-
}
494+
497495
sendReport(&_keyReport);
498496
return 1;
499497
}
@@ -514,7 +512,7 @@ size_t Keyboard_::write(uint8_t c)
514512
{
515513
uint8_t p = press(c); // Keydown
516514
uint8_t r = release(c); // Keyup
517-
return (p&r);
515+
return (p); // just return the result of press() since release() almost always returns 1
518516
}
519517

520518
#endif

0 commit comments

Comments
 (0)