Keyboard.h library not working

Hello!
I recently encountered a problem with the Keyboard.h library. My previous versions of the code work, as do the examples (tested just now, after my new code), so I know the problem isn't my arduino, library compatability or connections. I also know that it can send the characters, as my previous code worked. I also know the code reaches the point attached as it prints to the serial monitor. My board of choice (seeeduino XIAO) also has a transmitting light or something, which lights up when it is sending data to the serial monitor or the computer. This light turns on but nothing appears on field i'm typing to. I have also tried multiple computers so i don't think that's the problem. The code snippet: (full code below as i can't attach it as an .ino file)

  for (int i = 0; i < 15; i++) {
    if (pressed[i] == true) {
      //Keyboard.press(128);
      //Keyboard.press(129);
      if (lastPressed[i] == false) {
        Keyboard.press(i + 33 + mode * 30);
        Serial.println(i + 33 + mode * 30);
      }
      lastPressed[i] = true;
    }
    else if (lastPressed[i] == true) {
      Keyboard.release(i + 33 + mode * 30);
      lastPressed[i] = false;
    }
  }

FULL CODE:

#include <Mouse.h>
#include <Keyboard.h>

#define CLK 8
#define DT 9
#define ModeSwitchPin A10

int DBTime = 50; //SWITCH DEBOUNCE DELAY
unsigned long LPT[15]; //THIS IS AN ARRAY OF PRESSED TIMES
bool lastPressed[15]; //ARRAY OF SWITCHES LAST LOOP PRESSED (THERE ARE 15 TOTAL)
int BNR = 0; //THIS IS THE CURRENT BUTTON NUMBER IN THE LOOP
bool pressed[15]; //CURRENT PRESSED SWITCHES
int mode = 0; //MACROPAD WORKING MODE
int currentSwitch; //MODE SWITCH STATE
int counter = 0; //ROTARY ENCODER STUFF
int currentStateCLK;
int lastStateCLK;
int loopStart;
int loopEnd;

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  Serial.begin(9600);
  Keyboard.begin();
  Mouse.begin();
  lastStateCLK = digitalRead(CLK);

}

void loop() {
  loopStart = millis();
  //-----Mode determining-----
  currentSwitch = analogRead(ModeSwitchPin);
  if (currentSwitch > 500 && currentSwitch < 600) mode = 2; //normal
  else if (currentSwitch > 300 && currentSwitch < 400) mode = 1; //obs
  else if (currentSwitch < 100) mode = 0; //koolitöö
  //-----Reading rotary encoder-----
  currentStateCLK = digitalRead(CLK);

  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1) {
    if (digitalRead(DT) != currentStateCLK) {
      counter --;
      if (mode == 0) {
        Keyboard.write(0xD7);
      }
      else if (mode == 1) {
        Mouse.move(0, 0, -1);
      }
      else if (mode == 2) {
        Mouse.click(MOUSE_RIGHT);
      }
    } else {
      // Encoder is rotating CW so increment
      counter ++;
      if (mode == 0) {
        Keyboard.write(0xD8);
      }
      else if (mode == 1) {
        Mouse.move(0, 0, 1);
      }
      else if (mode == 2) {
        Mouse.click(MOUSE_LEFT);
      }
    }
  }
  lastStateCLK = currentStateCLK;
  //-----Reading buttons-----
  for (int column = 5; column < 8; column++) {
    digitalWrite(column, 1);
    for (int row = 0; row < 5; row++) {
      BNR = (column - 5) * 5 + row;
      if (analogRead(row) >= 900) {
        LPT[BNR] = millis();
        pressed[BNR] = true;
      }
      else if (millis() - LPT[BNR] < DBTime && lastPressed[BNR] == true) {
        pressed[BNR] = true;
      }
    }
    digitalWrite(column, 0);
  }
  for (int i = 0; i < 15; i++) {
    if (pressed[i] == true) {
      //Keyboard.press(128);
      //Keyboard.press(129);
      if (lastPressed[i] == false) {
        Keyboard.press(i + 33 + mode * 30);
        Serial.println(i + 33 + mode * 30);
      }
      lastPressed[i] = true;
    }
    else if (lastPressed[i] == true) {
      Keyboard.release(i + 33 + mode * 30);
      lastPressed[i] = false;
    }
  }
  for (int i = 0; i < 33; i++) {
    pressed[i] = 0;
  }
  loopEnd = millis();
  // Serial.println(loopEnd - loopStart);


}

Thanks in advance!

So what does this print?

as I'm using multiplexing in 3x5 configuration and haven't connected the switches yet, just using a test lead, it prints
93
98
103
or the old code in a typing field
h&c
I wanted to update the code as it didn't support holding down keys
(edit: this is when I touch the first row of 5 with a positive aka high voltage, on mode 2 and the +33 is to avoid weird characters like newline and stuff. Also yes, i plan to change the analogread to digitalread once I connect the switches)

So you are trying to send multi key presses?
Do you have a QWERTY system keyboard?

I'm building a macropad right now, so it doesn't nessecarily N E E D multiple keypresses, but it would be nice to have for the future when i maybe want to use it as a mini gaming keyboard or something. Also I think I have a QWERTY system keyboard if its referring to the layout but it isn't important anyways as the sent keys will be mapped to macros or shortcuts.(if the system keyboard affects that) I guess I will try to rewrite my code tomorrow as I can't find the problem.

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