@@ -494,6 +494,61 @@ size_t Keyboard_::release(uint8_t k)
494
494
return 1 ;
495
495
}
496
496
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
+
497
552
void Keyboard_::releaseAll (void )
498
553
{
499
554
_keyReport.keys [0 ] = 0 ;
0 commit comments