0

I want to make a simple program to put on my Arduino/Genuino (Sunfounder) Uno board that when plugged in, it invokes the Keyboard.print() function. When I compile my program to do that, it says that I'm not including Keyboard.h even though I actually am at the beginning of my program.

My code:

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
  Keyboard.print("Hello, world!");
  Keyboard.end();
}


void loop() {

}

When I compile the code, I get this error:

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

I checked my libraries file in the Arduino sketch editor file and Keyboard.h is there.

Any help is appreciated.

9
  • "Keyboard.h not found " - that's not what the error message says.
    – user2100815
    Sep 22, 2018 at 19:32
  • 1
    Which version of Arduino and Keyboard do you use? Sep 22, 2018 at 19:49
  • @ThomasSablik 1.8.7, 2.1 Sep 22, 2018 at 19:50
  • The newest version of keyboard I can find is 1.0.2. Where does your version come from? Sep 22, 2018 at 19:58
  • @ThomasSablik wait, it's not 2.1, that's the version of the license; I can't find a version anywhere. It's the version that comes with the IDE. Sep 22, 2018 at 20:02

2 Answers 2

6

Arduino/Genuino Uno does not support Keyboard. You can see it in hardware/ardunino/avr/libraries/HID/HID.h. This file is included from Keyboard.h and contains #if defined(USBCON). But USBCON is not defined for Arduino/Genuino Uno. You need to use another board for Keyboard.h

Following controllers support it:

  • ATmega32U6
  • ATmega8U2
  • ATmega16U2
  • ATmega16U4
  • ATmega32U2
  • ATmega32U4
  • and some ATxxUSBxx

Arduino/Genuino Uno uses ATmega32U8.

-2

You know what you don't require the 32u4 now, I got a solution to use keyboard and mouse through any Arduino.

All you have to do is something like this:

Serial.println("pressA")

On the Arduino

Head to a python script and do this:

import serial
import pyautogui
Arduino_Serial = serial.Serial('COM5', 9600)

while 1:
    incoming_data = str(Arduino_Serial.readline())
    print(incoming_data)
    if 'pressA' in incoming_data:
        pyautogui.press('a')
    incoming_data = ""
1
  • So you gave to have a python script on the PC to send the serial input to the keyboard buffer. Not very compatible. Dec 25, 2020 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.