Arduino build error

fatal error: USBHIDKeyboard.h: No such file or directory

USBHIDKeyboard.h is the keyboard library of the ESP32-S2 and ESP32-S3, which can be a USB device.

  • Build error
  • 2 common causes
The message

What the Arduino IDE prints

Output window
sketch.ino:1:10: fatal error: USBHIDKeyboard.h: No such file or directory
    1 | #include <USBHIDKeyboard.h>
      |          ^~~~~~~~~~
compilation terminated.
exit status 1

An Uno cannot be a USB keyboard at all. A Leonardo, Micro or Pro Micro can, with Keyboard.h.

Mokxi's compiler, clang, words it as 'USBHIDKeyboard.h' file not found.

Causes

The 2 usual causes with the fix

1The board cannot be a USB keyboard

Choose a board that talks USB itself. The Leonardo, Micro and Pro Micro use #include <Keyboard.h>.

Broken
#include "USBHIDKeyboard.h"

USBHIDKeyboard keyboard;

void setup() {
  Serial.begin(9600);
  keyboard.begin();
}

void loop() {
}
Fixed
#include <Keyboard.h>

void setup() {
  Serial.begin(9600);
  Keyboard.begin();
  Serial.println("keyboard ready");
}

void loop() {
}

Mokxi’s build tab says: This project has no file named USBHIDKeyboard.h.

Try this fix in Mokxi

2An ESP32 board without USB device mode

An ESP32-S3 needs its USB mode set before USBHIDKeyboard.h is available.

  1. Choose an ESP32-S2 or ESP32-S3 board, not a classic ESP32. The classic ESP32 has no USB device port.
  2. In Tools, set USB Mode to USB-OTG (TinyUSB), and compile again.
  3. Add #include "USB.h" and call USB.begin() after keyboard.begin().
FAQ

Questions and answers

Which boards can be a USB keyboard?

Boards with a chip that has USB built in: the Leonardo, Micro, Pro Micro, ESP32-S2, ESP32-S3 and the Raspberry Pi Pico.

Why not the Uno?

The Uno has a separate chip for USB, so the main chip cannot act as a keyboard.

Does Keyboard work in Mokxi?

Yes. Mokxi runs Keyboard.h sketches on a Leonardo, Micro or Pro Micro.