ESP32-C3 BLEKeyboard/Keypad not outputting keypress over BT

I'm trying to make a Bluetooth keypad/macropad for use in games (not a controller). The issue is that my keypresses aren't sent over Bluetooth to my Windows 10 PC.
The keypad is 2 column X 6 rows. I'm using keypad.h (matrix connected to gpio pins) as this seemed the simplest soultion to hooking everything up.
Wiring is correct as the right keypresses are sent on the COM port when viewed in the Serial Monitor.
Windows recognised the "keyboard" via bluetooth as well, so it's not that it's not connecting properly either.
I'm sure the problem is very simple and I'm missing something obvious.

Any help would be greatly appreciated.

#include <BleKeyboard.h>
#define USE_NIMBLE

#include <Keypad.h>

const byte ROWS = 6;  //six rows
const byte COLS = 2;  //two columns

char keys[ROWS][COLS] = {
  { '7', '1' },
  { '8', '2' },
  { '9', '3' },
  { '0', '4' },
  { '-', '5' },
  { '=', '6' },
};


byte rowPins[ROWS] = { 10, 6, 7, 5, 4, 8 };  
byte colPins[COLS] = { 2, 3 };              

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

BleKeyboard bleKeyboard("ESP32-C3_Keypad", "Me", 100);

void setup() {
  Serial.begin(9600);
  bleKeyboard.begin();
}

// This will hold down all the following buttons.
void macroCommand(uint8_t key) {
  bleKeyboard.press(KEY_LEFT_SHIFT);
  bleKeyboard.press(key);
}

void loop() {
  char key = keypad.getKey();

  // Only do something with the keypress if we
  // are connected to something via bluetooth
      Serial.println(key);
  if (bleKeyboard.isConnected() && key) {
    switch (key) {
      case '1':
        bleKeyboard.print('!');
        break;
      case '2':
        bleKeyboard.press('2');
        break;
      case '3':
        macroCommand('3');
        break;
      case '4':
        bleKeyboard.write('$');
        break;
      case '5':
        macroCommand('5');
        break;
      case '6':
        macroCommand('6');
        break;
      case '7':
        macroCommand('7');
        break;
      case '8':
        macroCommand('8');
        break;
      case '9':
        macroCommand('9');
        break;
      case '0':
        macroCommand('0');
        break;
      case '-':
        macroCommand('-');
        break;
      case '=':
        macroCommand('=');
        break;
    }

    delay(100);
    bleKeyboard.releaseAll(); 
  }
}

I tried various different bits of code down near the bottom to see if anything would work. So far no luck.

Edit. I've used the BLEKeyboard example code before. This DOES output text over bluetooth. I need an output to happen when a button is pressed though.

try removing the debug trace as this is like a "machine-gun" printing out tons of 0 and saturating the Serial connection. Might create an issue

1 Like

Omg :man_facepalming:t3:
Two and a half days it's taken me to get this thing working. Thank you very much!

glad it solved it :slight_smile:

It did. Until I went back and fixed up all the different output codes (they all worked before). I think I need to walk away for a while :sweat_smile: :skull_and_crossbones:

yeah - sometimes taking a walk is a good option

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