Nano ESP32 Keyboard and Mouse library

In the Keyboard and Mouse library pages it says the Nano ESP32 supports these libraries.

When I go to compile a sketch with these libraries it gives me:
"...\Documents\Arduino\libraries\Mouse\src/Mouse.h:25:10: fatal error: HID.h: No such file or directory
#include "HID.h"
^~~~~~~
compilation terminated.
exit status 1

Compilation error: exit status 1"

Is there anything else I should install or include so I can use these libraries?

Thanks for the help in advance!

Edit: I have put the answer below but, under the Examples tab be careful to select the USB specific for the Arduino Nano ESP32.

Hi @joao_ff_duarte. I apologize for the confusion. The information in the references is wrong.

There are different libraries for the Nano ESP32 which provide the same functionality. You can learn how to use them by studying the examples that are listed under the following menus in Arduino IDE:

  • File > Examples > USB > Keyboard
  • File > Examples > USB > Mouse

Examples helped... Thank you!

1 Like

I was able to use the HID libraries with the following libraries:

#include "USBHIDMouse.h"
#include "USBHIDKeyboard.h"

I believe you have to install the USB library on Arduino IDE to make it work but I'm not sure.

When I wrote this post I was trying to write a code for my Nano ESP32 that would make it so I could use it like a controller. I'll leave the code that was working for me below (some of it is commented because I was just testing it then and we ended up using bluetooth for it):

#include <Arduino.h>
#include "USB.h"
#include "USBHIDMouse.h"
#include "USBHIDKeyboard.h"

#define XBUTTONKEY 'e'
#define YBUTTONKEY MOUSE_LEFT
#define ABUTTONKEY ' '
#define BBUTTONKEY MOUSE_RIGHT

#define ESCAPEKEY  KEY_ESC

#define L3KEY      ' '
#define R3KEY      ' '


USBHIDMouse Mouse;
USBHIDKeyboard Keyboard;

// set pin numbers for switch, joystick axes, and LED:
//const int switchPin = 2;    // switch to turn on and off mouse control
//const int mouseButton = 3;  // input pin for the mouse pushButton
const int xAxis1 = A0;       // joystick1 X axis
const int yAxis1 = A1;       // joystick1 Y axis
const int xAxis2 = A4;       // joystick2 X axis
const int yAxis2 = A5;       // joystick2 Y axis


const int joystickL3 = D2;

const int upButton = D4;
const int leftButton = D5;
const int downButton = D6;
const int rightButton = D7;


const int escapeButton = D3;


const int xButton = D8;
const int yButton = D9;
const int aButton = D10;
const int bButton = D11;

const int joystickR3 = D13;


// parameters for reading the joystick:
int range = 12;             // output range of X or Y movement
int responseDelay = 5;      // response delay of the mouse, in ms
int threshold = range/6;    // resting threshold
int center = range/2;       // resting position value

bool mouseIsActive = true;  // whether or not to control the mouse
int lastSwitchState = LOW;   // previous switch state

void setup() {
  //Configure pins as an input and enable the internal pull-up resistor,
  pinMode(joystickL3, INPUT_PULLUP);

  pinMode(xButton, INPUT_PULLUP);
  pinMode(yButton, INPUT_PULLUP);
  pinMode(aButton, INPUT_PULLUP);
  pinMode(bButton, INPUT_PULLUP);


  pinMode(upButton, INPUT_PULLUP);
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);

  pinMode(joystickR3, INPUT_PULLUP);
  

  pinMode(escapeButton, INPUT_PULLUP);

  //Start Mouse and Keyboard control as well as USB
  Mouse.begin();
  Keyboard.begin();
  USB.begin();
}

void loop() {
  // joystick control
  bool xButRead = !digitalRead(xButton);
  bool yButRead = !digitalRead(yButton);
  bool aButRead = !digitalRead(aButton);
  bool bButRead = !digitalRead(bButton);

  bool joyL3Read = !digitalRead(joystickL3);

  bool upButRead = !digitalRead(upButton);
  bool leftButRead = !digitalRead(leftButton);
  bool downButRead = !digitalRead(downButton);
  bool rightButRead = !digitalRead(rightButton);

  bool joyR3Read = !digitalRead(joystickR3);

  bool escButRead = !digitalRead(escapeButton);

  // read and scale the two axes:
  int xReading1 = readAxis(xAxis1);
  int yReading1 = readAxis(yAxis1);
  int xReading2 = readAxis(xAxis2);
  int yReading2 = readAxis(yAxis2);


  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(yReading2, -xReading2, 0);
  }

  if(xReading1 == 0){
    Keyboard.release('s');
    Keyboard.release('w');
  } else if(xReading1 > 0){
    Keyboard.press('w');
  } else{
    Keyboard.press('s');
  }
  if(yReading1 == 0){
    Keyboard.release('d');
    Keyboard.release('a');
  } else if(yReading1 > 0){
    Keyboard.press('d');
  } else{
    Keyboard.press('a');
  }

  // button control
  //If corresponding button is pressed
  if (xButRead) {
    //Press 'x',
    Keyboard.press(XBUTTONKEY);
  } else{
    Keyboard.release(XBUTTONKEY);
  }
  if (yButRead) {
    //Press 'y',
    Mouse.press(YBUTTONKEY);
  } else{
    Mouse.release(YBUTTONKEY);
  }
  if (aButRead) {
    //Press 'a',
    Keyboard.press(ABUTTONKEY);
  } else{
    Keyboard.release(ABUTTONKEY);
  }
  if (bButRead) {
    //Press 'b',
    Mouse.press(BBUTTONKEY);
  } else{
    Mouse.release(BBUTTONKEY);
  }

  /*
  if (joyL3Read){
    //Press 'b',
    Keyboard.press('b');
  } else{
    Keyboard.release('b');
  }
  */
  
  //COMPATIBILITY ISSUES WITH THE JOYSTICK
  /*
  if (upButRead) {
    //Press 'w',
    Keyboard.press('w');
  } else{
    Keyboard.release('w');
  }
  if (leftButRead) {
    //Press LEFT_ARROW,
    Keyboard.press('a');
    //Serial.println("LEFT was pushed");
  } else{
    Keyboard.release('a');
  }
  if (downButRead) {
    //Press DOWN_ARROW,
    Keyboard.press('s');
    //Serial.println("DOWN was pushed");
  } else{
    Keyboard.release('s');
  }
  if (rightButRead) {
    //Press RIGHT_ARROW,
    Keyboard.press('d');
    //Serial.println("RIGHT was pushed");
  } else{
    Keyboard.release('d');
  }
  */

  /*
  if (joyR3Read){
    //Press RIGHT_ARROW,
    Keyboard.press(KEY_RIGHT_ARROW);
  } else{
    Keyboard.release('b');
  }
  */

  if (escButRead){
    //Press ESCAPE,
    Keyboard.press(ESCAPEKEY);
  } else{
    Keyboard.release(ESCAPEKEY);
  }

  delay(1);  
}

/*
  reads an axis (0 or 1 for x or y) and scales the analog input range to a range
  from 0 to <range>
*/
int readAxis(int thisAxis) {
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 4095, 0, range);

  // if the output reading is outside from the rest position threshold, use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  }

  // return the distance for this axis:
  return distance;
}

If you have any questions about it feel free to ask about it, I'll try my best to answer them.

Thanks for the response! Pointing me to the right libraries was the trick!

I am trying to use the code as a controller, similar to your code.

Thanks for the help!

Lynn P. Martin

This is what is known as a "platform bundled" library, which means it is installed as part of the "Arduino ESP32 Boards" platform, which you install via the Arduino IDE Boards Manager.

You don't install the library via Library Manager, as you would with a standalone library.