NodeMcu Keyboard mapping

Hy im new to this forum (and arduino). Im not realy good at it but i want to learn. So I just made a Bluetooth keyboard to control android apps for navigation with buttons. I used a NodeMcu esp32-wroom 32. Now my question is: is it possible to change the output of keyes for different aps via button?
Here`s my code:

#define USE_NIMBLE
#include <BleKeyboard.h>

BleKeyboard bleKeyboard;

#define MN_UP 15
#define MN_DOWN 12
#define MN_LEFT 19
#define MN_RIGHT 21
#define MN_CENTER 22
#define MN_ZOOMIN 14
#define MN_ZOOMOUT 13
#define MN_ROTATE 23


bool keyStates[8] = {false, false, false, false, false, false, false, false};
int keyPins[8] = {MN_UP, MN_DOWN, MN_LEFT, MN_RIGHT, MN_CENTER, MN_ZOOMIN, MN_ZOOMOUT, MN_ROTATE};
uint8_t keyCodes[8] = {KEY_UP_ARROW, KEY_DOWN_ARROW, KEY_LEFT_ARROW, KEY_RIGHT_ARROW, 'c', '+', '-', 'r'};

void setup() {
  Serial.begin(115200);
  Serial.println("Code running...");
  setInputs();
  bleKeyboard.begin();
}

bool connectNotificationSent = false;

void loop() {
  int counter;
  if (bleKeyboard.isConnected()) {
    if (!connectNotificationSent) {
      Serial.println("Code connected...");
      connectNotificationSent = true;
    }
    for (counter = 0; counter < 8; counter ++) {
      handleButton(counter);
    }
  }
}

void setInputs() {
  pinMode(MN_UP, INPUT_PULLUP);
  pinMode(MN_DOWN, INPUT_PULLUP);
  pinMode(MN_LEFT, INPUT_PULLUP);
  pinMode(MN_RIGHT, INPUT_PULLUP);
  pinMode(MN_CENTER, INPUT_PULLUP);
  pinMode(MN_ZOOMIN, INPUT_PULLUP);
  pinMode(MN_ZOOMOUT, INPUT_PULLUP);
  pinMode(MN_ROTATE, INPUT_PULLUP);
}
void handleButton(int keyIndex) {
  // handle the button press
  if (!digitalRead(keyPins[keyIndex])) {
    // button pressed
    if (!keyStates[keyIndex]) {
      // key not currently pressed
      keyStates[keyIndex] = true;
      bleKeyboard.press(keyCodes[keyIndex]);
    }
  }
  else {
    // button not pressed
    if (keyStates[keyIndex]) {
      // key currently pressed
      keyStates[keyIndex] = false;
      bleKeyboard.release(keyCodes[keyIndex]);
    }
  }
}

Theoretically it is, but if you use the same buttons you use to enter the key presses to change the output you might get a interpretation problem. Is a button press meant as an output change or a simple keypress?

I could add a button slide or toggleswitch which changes the "modes" if its easier. Or maybe a long press on a button to change the "mapping".
Is this possible?
Its meant that i have a button press which digitalWrites 'c' and in another "mode" it should digitalWrite 'x' for example

digitalWrite?

Of course you can adapt the code to send different thing to the host depending on the state of a switch or anything else. I would avoid using long presses for special cases in a keyboard setup.

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