"Keyboard" undefined despite the fact it's defined twice

Hello! I've been trying to make a basic Arduino keyboard, but it doesn't detect the variable
Here's the code
Edit: Whoops, messed up the formatting

#include <Keyboard.h>
#include <KeyboardLayout.h>

void setup() {
  Serial.begin(9600);
  Keyboard.begin()
}

This is the error:

...\sketch_dec8a.ino:14:3: error: 'Keyboard' was not declared in this scope
Keyboard.begin()
^~~~~~~~
Multiple libraries were found for "Keyboard.h"
Used: ...\Arduino\libraries\Keyboard
Not used: ...\Arduino15\libraries\Keyboard
exit status 1

Compilation error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?

What do I do? I'm using a Arduino Mega 2560, so could that be an issue?
Thanks!

Where did you get that library? You should always provide a link to any libraries that you use that are not included with the Arduino IDE.

Have you tried any of the example code that comes with that library?

Why do you include <KeyboardLayout.h>?

Hint: whenever you get some new hardware, the very best place to start is to make sure that one or more of the library examples compiles and runs as expected.

The library is included in the library tab of the IDE, but the example code still failed to compile

1 Like

I'm assuming you're trying to use the "Keyboard" library in the default Arduino libraries.

I'm using a Arduino Mega 2560, so could that be an issue?

Alas, the ATmega2560 chip on that board does not support USB.
That may be why the
the Keyboard library documentation
says

The keyboard functions enable ...
the 32u4 and SAMD based boards (Leonardo, Esplora, Zero, Due and MKR Family) to appear as a native Mouse and/or Keyboard to a connected computer.

which implies that Keyboard library doesn't work with Arduino Mega 2560 board or the Arduino Uno boards.

What do I do?

Perhaps you could try using a 32u4 or SAMD board.
You would still get that same error message, but then it would be fixable like so:

According to the Keyboard library documentation,
programs that use that library should begin with

#include "Keyboard.h"

(not "#include <KeyboardLayout.h>").

That's exactly what the error message about '#include <Keyboard.h>' was trying to tell you.

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