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

I am trying to program a Teensy 2.0 with the Arduino IDE but the message in the title pops up on compile. The microprocessor is compatible with Keyboard.h (32u4) and the library is installed correctly so I don't know how to fix this.
This is my code:

#include <Keyboard.h>

const int rows[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int cols[] = {25, 26, 27, 28, 15, 16, 14, 9, 13, 24, 12, 10, 11};
const int keyMap[][8] = {
  {0x3A, 0x43, 0x4C, '4', 'Q', 'S', 'C', 0x59},
  {0x3B, 0x44, 0x4D, '5', 'E', 'F', 'X', 0x5A},
  {0x3C, 0x45, 0x50, '6', 'W', 'D', 'V', 0x5B},
  {0x3D, 0x46, 0x4F, '7', 'R', 'G', 'N', 0x5C},
  {0x3E, 0x47, 0x51, '8', 'P', 'H', 'M', 0x5D},
  {0x3F, 0x48, 0x52, '9', 'T', 'J', 'B', 0x5E},
  {0x40, 0x49, 0x2B, '0', 'U', 'K', 0x37, 0x5F},
  {0x29, 0x4C, 0x47, 0x1B, 'Y', 0x56, 0x33, 0x56},
  {0x1E, 0x3B, 0x36, '3', 0x0F, 'A', 0x35, 0x62},
  {0x33, 0x1D, 0x66, 0x09, 'T', 0x06, 0x0D, 0x60},
  {0x23, 0x36, 0x62, 0x0E, 0x12, 'I', 'L', 0x36},
  {0x29, 0x3E, 0x31, 0x28, 0x14, 'Z', ' ', 0x5D},
  {0x35, 0x37, 0x1D, 0x0A, 'P', 0x1C, 0x59, 0x58},
};

  
bool keyStates[sizeof(cols) / sizeof(cols[0])][sizeof(rows) / sizeof(rows[0])] = {false};

void setup() {
  Keyboard.begin();
  for (int i = 0; i < sizeof(cols) / sizeof(cols[0]); i++) {
    pinMode(cols[i], INPUT_PULLUP);
  }
}

void loop() {
  for (int col = 0; col < sizeof(cols) / sizeof(cols[0]); col++) {
    pinMode(cols[col], OUTPUT);
    digitalWrite(cols[col], LOW);

    for (int row = 0; row < sizeof(rows) / sizeof(rows[0]); row++) {
      bool keyPressed = (digitalRead(rows[row]) == LOW);

      if (keyPressed && !keyStates[col][row]) {
        simulateKeyPress(row, col);
        keyStates[col][row] = true;
      } else if (!keyPressed && keyStates[col][row]) {
        keyStates[col][row] = false;
      }
    }

    digitalWrite(cols[col], HIGH);
    pinMode(cols[col], INPUT_PULLUP);
  }
}

void simulateKeyPress(int row, int col) {
  // Get the key code based on the row and column
  int keyCode = keyMap[col][row];

  // Check if a valid key code is present
  if (keyCode != KEY_NONE) {
    Keyboard.press(keyCode);
  }
}

Thank you for your help

Correct board selected?

If you have the correct Teensy 2.0 board selected, what do you have selected under Tools > USB Type?

I'm getting an error that KEY_NONE was not declared.

David,
by selecting USB type --> any with keyboard I fixed the issue, thank you!
Yes I noticed the KEY_NONE error, I am debugging the code now.
Have a nice day :slight_smile:

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