I've just bought a new pro-micro and I want to simulate some keyboard inputs with it, the thing is the Arduino IDE (version 2:1.0.5) can't seem to find neither HID.h or Keyboard.h libraries. The steps to reproduce the problem are:
- plug the device using usb cable on the pc
- Open arduino ide
- try to compile my code
- it fails with the error
test_keyboard.ino:27:22: fatal error: Keyboard.h: No such file or directory compilation terminated.
The code is:
#include <Keyboard.h>
void typeKey(int key)
{
Keyboard.press(key);
delay(50);
Keyboard.release(key);
}
/* Init function */
void setup()
{
// Begining the Keyboard stream
Keyboard.begin();
delay(400);
//Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_CTRL);
Keyboard.press(KEY_ALT);
Keyboard.press(KEY_T);
Keyboard.releaseAll();
delay(100);
Keyboard.print("echo 'hello world'");
typeKey(KEY_RETURN);
delay(100);
// Ending stream
Keyboard.end();
}
/* Unused endless loop */
void loop() {}
By the way, I'm running this on linux, after pluging it and running lsusb
on terminal I got:
Bus 002 Device 071: ID 2341:8036 Arduino SA Leonardo (CDC ACM, HID)
2 Answers 2
The Keyboard functions were moved into their own library in Arduino IDE 1.6.6/Arduino AVR Boards 1.6.9. In previous Arduino AVR Boards versions, those functions were part of the core library and there was no Keyboard library or Keyboard.h. Since you're using a very outdated version of the Arduino IDE, you don't have a Keyboard library, thus the error. There are two options to solve this problem:
- Remove the line
#include <Keyboard.h>
from your sketch. - Update to a modern version of the Arduino IDE. Note that when you install the Arduino IDE via
sudo apt-get install arduino
, you end up with the very outdated IDE version you're using. I recommend always installing the official Arduino IDE downloaded from https://www.arduino.cc/en/Main/Software.
For anyone wanting to write code that's backwards compatible with old IDE versions, you can do this:
#if ARDUINO > 10605
#include <Keyboard.h>
#endif
Reference: https://github.com/arduino/Arduino/pull/3304
Don't hang around in the stone age, step forward to 2019 and join us with the newest Arduino IDE.
Do you use the newest linux version, for example Ubuntu 18.10?
Remove all arduino related packages from the repositories.
Be sure to remove the package librxtx-java.
You don't need to install Java, jdk, openjdk, jre or any of those.
If you use snap, remove the Java and Arduino from the snap as well.
When you have used such an old version, I suggest to delete the hidden .arduino15
folder from your HOME folder.
Perhaps you have downloaded libraries that are now included with the Arduino IDE. You could check the "libraries" folder (in the same folder as the folders of your projects) and delete unused libraries.
Download the Arduino IDE from the arduino.cc website.
The 32-bit version for a 32-bit linux system, and the 64-bit version for a 64-bit linux system.
Unpack it somewhere in under your HOME folder. You can make a shortcut to the file "arduino" and run the shortcut.
Don't make changes to the stardard Arduino system files (the Arduino IDE that you have downloaded and unpacked). Sometimes it is needed to make changes to the files in the hidden .arduino15
folder.