Arduino doesn't seem to notice that I have already imported the library

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:

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.