)
Im trying to use my Arduino nano esp32 as an HID device. I'm new to using Arduino and C++ but im having this problem where the Keyboard.h library wont work. I've also tried to use the premade KeyboardMessage example and get the exact same error. im realt stuck and I cant find any documentation on the problem I'm having. Any help would be apreciated.
The library is probably not suited for your board. Which one did you pick ?
side notes
please, please never ever again post pictures of text... Not only they are not readable nor usable directly for copy&paste but they use up lots of storage and internet bandwidth which contributes to polluting the planet.
➜ do your part and do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).
also please choose the right category, this does not seem like an IDE 2.x issue. I moved it into the Nano ESP32 category.
Welcome to the forum
Keyboard.h may be there but the error refers to HID.h not being there, possibly because the compiler is using the wrong library. There may be many libraries of the same name and the compiler decides which one to use based on the board type selected as the target in the IDE. Which board type have you got selected ?
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
Please post your full sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
It is also helpful to post full error messages in code tags as it makes it easier to scroll through them and copy them for examination
To post the error message, click the "COPY ERROR MESSAGES" button in the IDE, paste the copied messages in a reply here, select all of it and click the < CODE > icon above the reply editor to add the code tags and then save the reply
Why do you have a Keyboard directory in Arduino/libraries? The Keyboard directory is pre-installed in the Arduino15 directory. HID.h comes installed with the AVR platform (e.g. Uno, Leonardo) and maybe others and that is your problem, it's not intended for use with the Nano ESP32.
Try the keyboard example on https://docs.arduino.cc/tutorials/nano-esp32/cheat-sheet/#usb-hid
Consider also the difference between using for example:
#include "HID.h"
and
#include <HID.h>
The former looks for files in the same folder as the sketch and therefore requires the .h file and any corresponding matching .c or .cpp to be present there. The latter looks in the Arduino file structure and references libraries already installed and available in the IDE.
Not quite correct. If the compiler can't find it it will also look in the latter.
Where is "there?"
Interesting to note. Always thought that #include with quotes strictly limited the compiler to searching the local directory or the specified path only.
indeed - not the case, it just adds the local directory at the top of the search list.
This way if you have an updated library and want an old one, you just stick it into your local directory and it will be found before the other one and so you get to use the one you wanted.
I've taken the default HID library for AVR (located in Arduino15\packages\arduino\hardware\avr\1.8.7\libraries) and copied it to
- Arduino15\libraries; in this directory you will also find e.g. the Servo library.
- sketchbook/libraries
I've copied HID.cpp and HID.h from the library to the sketch directory.
I've include "HID.h" in a dummy sketch
#include "HID.h"
void setup()
{
}
void loop()
{
}
With verbose output for compilation you can see some lines indicating that the local HID library (in the sketch directory) is used; e.g.
C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -IC:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.7\cores\arduino -IC:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.7\variants\eightanaloginputs C:\Users\bugge\AppData\Local\arduino\sketches\8796DF82CE130C2CDB7A5C948AD832CC\sketch\HID.cpp -o nul
There is further no mention in the line of libraries used
Next I deleted the two HID files from the sketch directory and compiled again.
At the end of the verbose compilation output you will see
Multiple libraries were found for "HID.h"
Used: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\HID
Not used: C:\Users\bugge\AppData\Local\Arduino15\libraries\HID
Not used: C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.7\libraries\HID
Using library HID at version 1.0 in folder: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\HID
The above shows the sequence in which the compiler searches.
First the 3rd party library directory (sketchbook/libraries), next the libraries in the relevant platform directory (Arduino15\packages\arduino\hardware\avr\1.8.7\libraries) and lastly the "generic" libraries (Arduino15\libraries).
