Hi I'm trying to make a program that lets me press keys with a joystick, but the code says that I haven't imported the keyboard library even though I already have.
Here's my code
#include <Keyboard.h>
int Xpin=A0;
int Ypin=A1;
int Spin=2;
int Xval;
int Yval;
int Sval;
int dt=200;
int sens=10;
int X;
int Y;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Xpin,INPUT);
pinMode(Ypin,INPUT);
pinMode(Spin,INPUT);
digitalWrite(Spin,HIGH);
Keyboard.begin()
}
void loop() {
// put your main code here, to run repeatedly:
Xval=analogRead(Xpin);
Yval=analogRead(Ypin);
Sval= digitalRead(Spin);
X=(Xval-511.5)/sens
Y=(Yval-511.5)/sens
//X
if (X < 0) {
Keyboard.press("a")
}
else if (X > 0) {
Keyboard.press("d")
}
//Y
if (Y < 0) {
Keyboard.press("s")
}
else if (Y > 0) {
Keyboard.press("w")
}
Keyboard.releaseAll();
delay(dt);
}
This is the error and I've tried everything to fix it. Please help
C:\Users\ \OneDrive\Documents\Arduino\Mouse_Joystick\Mouse_Joystick.ino: In function 'void setup()':
Mouse_Joystick:21:3: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
Keyboard.begin()
^~~~~~~~
Mouse_Joystick:37:5: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
Keyboard.press("d")
^~~~~~~~
Mouse_Joystick:42:5: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
Keyboard.press("s")
^~~~~~~~
Mouse_Joystick:45:5: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
Keyboard.press("w")
^~~~~~~~
Mouse_Joystick:47:3: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
Keyboard.releaseAll();
^~~~~~~~
exit status 1
'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
Thanks
Close and re-start the IDE.
Hi @dragooooooooooooooooooooooooooon . Which board do you have selected from the Tools > Board in the Arduino IDE?
I selected the Arduino Uno
OK, this is the cause of the error.
The "Keyboard" library can only be used with certain Arduino boards that have the necessary native USB capabilities. The Uno does not have native USB capabilities, so it cannot be used with the "Keyboard" library.
The Arduino IDE attempts to be helpful by adding this advice for fixing the more cryptic errors generated by the compilation process:
Unfortunately, there are multiple distinct possible causes of the error and that advice is only relevant to only of those causes. It is completely inappropriate in this case. The developers are tracking this poor user experience here:
opened 10:28AM - 29 Jun 22 UTC
type: enhancement
topic: code
### Describe the request
Evaluate the often irrelevant supplemental interpret… ations the Arduino IDE provides on Keyboard and Mouse library-related compilation errors.
- Can false interpretations be reduced?
- Can the interpretation message be adjusted to avoid confusion in the event of false positives?
- Should the interpretations be removed altogether?
### Describe the current behavior
The Arduino IDE attempts to provide some additional guidance to users in response to some specific compilation error messages:
https://github.com/arduino/arduino-ide/blob/34ef25c4e42173d18f64d2f42ff9f24265f16151/arduino-ide-extension/src/node/cli-error-parser.ts#L134-L164
This is accomplished by matching a subset of the error message produced by the compilation toolchain and then printing an additional message in a notification and to the "Output" panel. That message is intended to make the cause and resolution for the error easier for less advanced users to understand.
Two such interpretations were put in place at the time of a breaking change 7 years ago resulting from moving the popular keyboard and mouse emulation APIs out of the core to standalone libraries: https://github.com/arduino/Arduino/pull/3304
Prior to the change, the APIs could be used without adding an extra `#include` directive to the sketch:
```cpp
void setup() {
Keyboard.begin();
Keyboard.print("hello");
}
void loop() {}
```
After the change, it was necessary to add an `#include` directive for the appropriate header file:
```cpp
#include <Keyboard.h>
void setup() {
Keyboard.begin();
Keyboard.print("hello");
}
void loop() {}
```
Attempting to compile the previously valid code results in an error:
```text
C:\Users\per\AppData\Local\Temp\.arduinoIDE-unsaved2022528-3144-18az9y0.lofa\sketch_jun28a\sketch_jun28a.ino: In function 'void setup()':
C:\Users\per\AppData\Local\Temp\.arduinoIDE-unsaved2022528-3144-18az9y0.lofa\sketch_jun28a\sketch_jun28a.ino:2:3: error: 'Keyboard' was not declared in this scope
Keyboard.begin();
^~~~~~~~
exit status 1
```
These supplemental interpretations are also displayed by the Arduino IDE:
> 'Mouse' not found. Does your sketch include the line '#include <Mouse.h>'?
> 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
Arduino and the Arduino community have done a good job of adapting to the change and it is now very rare to find any legacy code without these `#include` directives. There is always the chance a user will forget to add an `#include` directive in a sketch they write from scratch, but that is just as likely to occur with any arbitrary header file, so does not justify giving these two special treatment.
Meanwhile, another mistake related to keyboard and mouse emulation code is just as prevalent as ever: attempting to use it with a board which does not have native USB support (e.g., [Uno](https://store.arduino.cc/products/arduino-uno-rev3), [Mega](https://store.arduino.cc/products/arduino-mega-2560-rev3)). Unfortunately, doing this produces the same compilation error as omitting the `#include` directive, which also triggers the interpretations. In this case the interpretation message is completely irrelevant because the user already has that `#include` directive in their sketch and the error is caused by something completely different.
🙁 The interpretation message increases confusion in the cases where it is irrelevant. The cases where it is relevant are increasingly rare as time goes on.
- https://forum.arduino.cc/t/what-is-wrong-here/1080550
- https://forum.arduino.cc/t/issue-on-keyboard-library/1065385
- https://forum.arduino.cc/t/sketch-funktioniert-nicht-keyboard/1099364
- https://forum.arduino.cc/t/missing-keyboard-h/1059440
- https://forum.arduino.cc/t/keyboard-not-found-does-your-sketch-include-the-line-include-keyboard-h/679287
- https://forum.arduino.cc/t/keyboard-not-found-does-your-sketch-include-the-line-include-keyboard-h/647842
- https://forum.arduino.cc/t/need-help-in-joystick-wasd-keyboard-keys/877912
- https://forum.arduino.cc/t/trying-to-build-alternative-computer-mouse-but-getting-nothing-but-errors/888471
- https://forum.arduino.cc/t/keyboard-h-header-is-not-working/656172
- https://forum.arduino.cc/t/i-have-the-library-on-the-code-but-it-keeps-asking-me-to-add-it/680602
- https://forum.arduino.cc/t/keyboard-h-not-working/934817
- https://forum.arduino.cc/t/old-lady-needs-a-hand-uno-clone/981702
- https://forum.arduino.cc/t/library-included-but-not-found/690756
- https://forum.arduino.cc/t/fehler-bei-keyboard-h/876342
- https://forum.arduino.cc/t/mouse-begin-not-working/676720/9
- https://forum.arduino.cc/t/another-newb-issue/674351
- https://forum.arduino.cc/t/emulation-dun-clavier-pour-une-arduino-mega-keyboard-emulation-for-an-mega/645375
- https://forum.arduino.cc/t/problem-with-nextion/622119
- https://forum.arduino.cc/t/keyboard-key-codes/287528/4
- https://forum.arduino.cc/t/outputting-keys-to-the-computer/427496
- https://forum.arduino.cc/t/makey-makey-source-code-troubles/422137/2
- https://forum.arduino.cc/t/keyboard-not-found-trying-to-clarify/536713
- https://forum.arduino.cc/t/issues-with-uploading-sketch/528862
- https://forum.arduino.cc/t/file-examples-usb-keyboard-keyboardmessage-not-work-this-example/422238/3
- https://forum.arduino.cc/t/trying-to-make-an-arduino-2-keys-keyboard-but-error-pops-up/499126
- https://forum.arduino.cc/t/library-not-found/503525
- https://forum.arduino.cc/t/library-not-working/491054/3
- https://forum.arduino.cc/t/library-error-keyboard-not-found-when-include-keyboard-h-is-included/629788
- https://forum.arduino.cc/t/problems-including-mouse-h/349502
- https://forum.arduino.cc/t/help-with-keyboard-h/551912
- https://forum.arduino.cc/t/arduino-uno-6-1-exscercise/571440
- https://forum.arduino.cc/t/controlling-a-computer-mouse-with-an-arduino-uno-not-working-mouse-h/588081
- https://forum.arduino.cc/t/key-mapping/402444
- https://forum.arduino.cc/t/arduino-nano-keyboard-h-problem/422820
- https://forum.arduino.cc/t/include-library-problem/469783
- https://forum.arduino.cc/t/cant-seem-to-be-able-use-keyboard-h-library/333667/5
- https://forum.arduino.cc/t/error-with-keyboard-library/421403
- https://forum.arduino.cc/t/compiling-example-programs/557209
- https://forum.arduino.cc/t/need-some-help-sending-commands-through-serial-without-a-keyboard/613920/8
- https://forum.arduino.cc/t/keyboard-library-not-working/362325
- https://forum.arduino.cc/t/new-to-this-using-copied-code-getting-errors/488587/27
- https://forum.arduino.cc/t/help-with-mouse-library/574057
- https://forum.arduino.cc/t/keyboard-library-not-working-correctly/583389
- https://forum.arduino.cc/t/arduino-keyboard-library-problem/546147
- https://forum.arduino.cc/t/keyboard-not-found-using-due/545296
- https://forum.arduino.cc/t/unknown-error-for-compiling-my-code-onto-my-arduino-genuino-uno/509186/2
- https://forum.arduino.cc/t/why-is-keyboard-h-library-not-working-on-arduino-uno/472447
- https://forum.arduino.cc/t/keyboard-not-found-on-usb-hid-capable-arduino-uno/450644
- https://forum.arduino.cc/t/keyboard-h-not-supported/427642
- https://forum.arduino.cc/t/compiling-error-doesnt-think-im-including-libraries-that-i-am/378743
- https://forum.arduino.cc/t/arduino-doesnt-seem-to-notice-that-i-have-already-imported-the-library/1008502
- https://forum.arduino.cc/t/keyboard-library-not-being-included/1043892
- https://forum.arduino.cc/t/keyboard-not-found-have-you-included-keyboard-h-in-your-sketch-arduino-uno/1096544
- https://forum.arduino.cc/t/i-have-the-library-installed-but-it-asks-me-to-use-include/1144083
- https://forum.arduino.cc/t/arduino-nano-is-not-working-with-programing/1153085
- https://forum.arduino.cc/t/no-me-reconoce-la-libreria-keyboar-el-arduino-ide/1168346
- https://forum.arduino.cc/t/mouse-not-found-does-your-sketch-include-the-line-include-mouse-h/1184592
- https://forum.arduino.cc/t/keyboard-h-not-found/1250139
- https://forum.arduino.cc/t/does-your-sketch-include-the-line-include-libname/1264530
- https://forum.arduino.cc/t/problem-whit-using-keyboard-library/1278549
- https://github.com/arduino-libraries/Keyboard/issues/39
- https://github.com/arduino/reference-en/issues/802
### Arduino IDE version
2.0.0-rc8-snapshot-34ef25c
### Operating system
Windows
### Operating system version
10
### Additional context
Related: https://github.com/arduino/arduino-ide/pull/1113
### Issue checklist
- [X] I searched for previous requests in [the issue tracker](https://github.com/arduino/arduino-ide/issues?q=)
- [X] I verified the feature was still missing when using the latest [nightly build](https://github.com/arduino/arduino-ide#nightly-builds)
- [X] My request contains all necessary details
If you do want to use the "Keyboard" or "Mouse" libraries, you will need to use a different Arduino board. I highly recommend the Micro as a good choice for development in combination with a breadboard or for integration into finished projects. If you want to use Arduino shields, the Leonardo will be a good option. If you need more resources than are available with those AVR microcontroller-based boards, then you might take a look at the Nano 33 IoT and MKR boards
system
Closed
December 28, 2022, 9:42pm
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.