Search code examples
arduinokeyboard

'Keyboard' was not declared in this scope - Arduino coding


I'm writing a simple program for an Arduino that will take the input of 2 buttons and output simulated keys for 2 different functions to use in Clone Hero.

Arduino editor (both online and local versions) spit out

'Keyboard' was not declared in this scope

The offline editor asks if Keyboard.h is included... Which it obviously is.

Any ideas why?

// Keyboard - Version: Latest
#include <Keyboard.h>

//btnWhammy is the button to replace whammy bar function
//btnSP is the button to replace star power activation
//Set Clone Hero to register j for whammy and k for star power

//declaring constant integers for the pins on the Arduino
const int btnWhammy = 2;
const int btnSP = 13;

//Declaring integers for the state of the button press
int btnWhammyState = 0;
int btnSPState = 0;

void setup() {
  //Initialisation of the pins as inputs
  pinMode(btnWhammy, INPUT);
  pinMode(btnSP, INPUT);
}

void loop() {
  //Setting the button states to the read of the digital pin (LOW is off, HIGH is on)
  btnWhammyState = digitalRead(btnWhammy);
  btnSPState = digitalRead(btnSP);
  //If the whammy button is pressed, send 'j' to the keyboard, wait 100ms then release all keys
  if (btnWhammyState == HIGH) {
    Keyboard.press('j');
    delay(100);
    Keyboard.releaseAll();
  }
  //If the Star Power button is pressed, send 'k' to the keyboard, wait 100ms then release all keys
  if (btnSPState == HIGH) {
    Keyboard.press('k');
    delay(100);
    Keyboard.releaseAll();
  }
}

Solution

  • This is a classic mistake -- you are probably compiling for a non-Leonardo board, like a Uno. The Keyboard.h library is not included because it is not present for the board you are compiling with.

    I took your code and compiled it for Leonardo -- no issues. For Uno, I get the same error as you...