Skip to content

RP2040 Raspberry Pico W issues with KeyboardBLE #2547

@nkoske

Description

@nkoske

Im having Trouble to get KeyboardBLE Running and staying Connected.

Compiling my Code and running it is no issue, but when i connect to my Win11 Laptop it connects and disconnects after few seconds.
Windows 11 tells me its an driver issue, i tryed to install new drivers (other BT devices work just fine).

My gut tells me its either my code or the libs fault.
What im doing wrong here?

#include <KeyboardBLE.h>

// Define keyboard inputs and outputs
byte inputs[] = {6, 7, 8}; // Minimal 3 inputs
byte outputs[] = {0, 1};   // Minimal 2 outputs

// Simple keyboard layout with a 2x3 matrix
char layout[2][3] = {
    {'a', 'b', 'c'},
    {'d', 'e', 'f'}
};

int keyDown[2][3] = {0};   // Key status

void setup() {
  // Initialize outputs
  for (int i = 0; i < 2; i++) {
    pinMode(outputs[i], OUTPUT);
    digitalWrite(outputs[i], HIGH);
  }
  
  // Initialize inputs
  for (int i = 0; i < 3; i++) {
    pinMode(inputs[i], INPUT_PULLUP);
  }

  // Start Bluetooth HID
  KeyboardBLE.begin();
  delay(5000);  // Allow time for Bluetooth to initialize
}

void loop() {
  for (int i = 0; i < 2; i++) {    
    digitalWrite(outputs[i], LOW);  // Set one row low
    
    for (int j = 0; j < 3; j++) {
      if (digitalRead(inputs[j]) == LOW) {
        keyPressed(i, j);  // Call function when key is pressed
      } else if (keyDown[i][j] != 0) {
        resetKey(i, j);    // Reset key when released
      }
    }
    
    digitalWrite(outputs[i], HIGH);  // Set the row back high
  }
}

void keyPressed(int row, int col) {
  if (keyDown[row][col] == 0) {
    KeyboardBLE.write(layout[row][col]);  // Send keypress
  }
  keyDown[row][col]++;
}

void resetKey(int row, int col) {
  keyDown[row][col] = 0;
  KeyboardBLE.release(layout[row][col]);  // Release the key
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions