espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.71k stars 7.43k forks source link

feat(usb_hid_keyboard): Adds Keyboard Layout and Sends reports just for Modifier Keys #10591

Closed SuGlider closed 1 week ago

SuGlider commented 1 week ago

Description of Change

This PR fixes a problem related to sending just modifier key pressing/releasing (SHIFT/CTRL/ALT). It also adds the upstream Keyboard Layouts to the library. Additionally it adds the Brazilian ABNT2 Keyboard Layout.

Tests scenarios

Tested with ESP32-S3 using a Keyboard Test website. Configuration: CDC on Boot: Disabled || USB Mode: OTG(TinyUSB)

#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;
const int buttonPin = 0;  // input pin for pushbutton

void setup() {
  // make pin 0 an input and turn on the pull-up resistor so it goes high unless
  // connected to ground:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(115200);
  while (!Serial) delay(100);

  Serial.println();
  Serial.println("Starting USB Keyboard Device.");
  Serial.println();

  Keyboard.begin();
  USB.begin();
}

void pressRelease(uint8_t k) {
  Keyboard.press(k);
  delay(1000);
  Keyboard.release(k);
  delay(1000);
}

void loop() {
  while (digitalRead(buttonPin) == HIGH) {
    // do nothing until button pin goes low
    delay(50);
  }
  delay(500);

  // test all 8 modifier keys - press & release
  pressRelease(KEY_RIGHT_SHIFT);
  pressRelease(KEY_LEFT_SHIFT);
  pressRelease(KEY_RIGHT_CTRL);
  pressRelease(KEY_LEFT_CTRL);
  pressRelease(KEY_RIGHT_ALT);
  pressRelease(KEY_LEFT_ALT);
  pressRelease(KEY_RIGHT_GUI);
  pressRelease(KEY_LEFT_GUI);

  Serial.println("Done.");
}

The new Keyboard Layout needs a compatible Keyboard of the selected location. Example:

#include "USB.h"
#include "USBHIDKeyboard.h"
#include "keyboardLayout/Keyboard_pt_BR.h" // using Portuguese - ABNT2 Keyboard Layout

USBHIDKeyboard Keyboard;
const int buttonPin = 0;  // input pin for pushbutton

void setup() {
  // make pin 0 an input and turn on the pull-up resistor so it goes high unless
  // connected to ground:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(115200);

  Keyboard.begin(KeyboardLayout_pt_BR);
  USB.begin();
}

void loop() {
  while (digitalRead(buttonPin) == HIGH) {
    // do nothing until pin 2 goes low
    delay(50);
  }

  // presses all ASCII table respective keys 
  for (char i = 32;  i < 128; i++) {
    Keyboard.write(i);
    if (i == '`' || i == '~') {
      Keyboard.write(' ');  // otherwise it may affect the next character
      delay(50);
    }
    delay(50); // minimum time between keystrokes for Windows OS
  }
  // sends enter
  Keyboard.println();
}

Related links

Fixes #10368

github-actions[bot] commented 1 week ago
Warnings
:warning: Please consider squashing your 6 commits (simplifying branch history).
Messages
:book: This PR seems to be quite large (total lines of code: 1983), you might consider splitting it into smaller PRs

πŸ‘‹ Hello SuGlider, we appreciate your contribution to this project!


Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- Resolve all warnings (⚠️ ) before requesting a review from human reviewers - they will appreciate it.
- Addressing info messages (πŸ“–) is strongly recommended; they're less critical but valuable.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by :no_entry_sign: dangerJS against b5b5ebd3e04cd4da9f1998aa36e2c51dacfbf165

github-actions[bot] commented 1 week ago

Test Results

 56 files   56 suites   4m 15s ⏱️  21 tests  21 βœ…β€ƒ0 πŸ’€β€ƒ0 ❌ 134 runsβ€Šβ€ƒ134 βœ…β€ƒ0 πŸ’€β€ƒ0 ❌

Results for commit b5b5ebd3.

:recycle: This comment has been updated with latest results.