2 layer HID Keyboard

I write this code to have two layer keyborad.
but the issue is i am not getting the 2nd time pressed key. just “customKey1” is empty

// mutantC keyboard v1.4

#include <Keypad.h>
#include <Keyboard.h> 

const byte ROWS = 4; 
const byte COLS = 11; 

char hexaKeys1[ROWS][COLS] = {
  {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '},
  {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', KEY_BACKSPACE},
  { KEY_CAPS_LOCK , 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',  KEY_RETURN  },
  {KEY_LEFT_CTRL, KEY_LEFT_GUI,  'z', 'x', 'c', 'v', 'b', 'n', 'm', ' ', '@' }
};

char hexaKeys2[ROWS][COLS] = {
  {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '},
  {'q', 'w', 'e', 'r', 't', 'w', 'w', 'w', 'w', 'w', KEY_BACKSPACE},
  { KEY_CAPS_LOCK , 'a', 's', 'd', 'd', 'g', 'h', 'j', 'k', 'l',  KEY_RETURN  },
  {KEY_LEFT_CTRL, KEY_LEFT_GUI,  'z', 'x', 'c', 'v', 'b', 'n', 'm', ' ', KEY_LEFT_SHIFT }
};

byte rowPins[ROWS] = {A0, A1 ,A3 , A2}; 
byte colPins[COLS] = {9, 16, 14, 15, 8, 7, 6, 5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys1), rowPins, colPins, ROWS, COLS); 
Keypad customKeypad2 = Keypad(makeKeymap(hexaKeys2), rowPins, colPins, ROWS, COLS);

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

void loop(){
  char customKey = customKeypad.getKey();
    char customKey1 = customKeypad2.getKey();
//  if (customKey){
//    Serial.println(customKey);
//  }
  Keyboard.begin();  //begin keyboard 

  if (customKey == '@')     // if buton is pushed
  {
    Serial.println('@');
    
//    Serial.println(customKey1);
    if (customKey1)     // if buton is pushed
    {
      Serial.println('=');
       Keyboard.write(customKey1);  // send single character "A"
       delay(200);                 // delay so you don't get 20 A's
    }
  } else if (customKey)  {
    Serial.println('-');
    Keyboard.write(customKey);  // send single character "A"
    delay(200);                 // delay so you don't get 20 A's
  }
  
  Keyboard.end();    //stops keybord
}

Explain what equipment is in use, the wiring you have done and what you try to really achieve

I am using sparkfun pro micro.
I printed a pcb. keyboard is a matrix format. A0,A1,A2,A3 are the row pins and 2,3,4,5,6,7,8,9,14,15,16 are col pins.

The keyboard is working fine with one layer. the alphabet layer. but i want another layer which will Contains all the sign like ,.?/ etc.
So the idea is if the "@" button is clicked it will use 2nd layer or customkeypad2.
But issue is it dictate the if "@" is clicked but get an empty chr in "customkey1" variable.

I think there is a issue with declaring the "customkeypad2" .

why do you instantiate two Keypads? you really have only one.

The keypad library knows how to handle multiple keypress at the same time. have a look at the MultiKey.ino example.

What you want to do is monitor what keys are being pressed and if ‘@’ is being held down and another key is pressed, then you go get the alternate symbol from you second keyMap and that’s what you send

For that you’ll need to build a small state machine checking states of the key. You write a loop for (int index =0; index <LIST_MAX; index++) { ...to scan the kpd.key[index].kstate and have a flag set when you recognize in kpd.key[index].kchar the ‘@’ key is in the PRESSED state

This flag will not change as long as the ‘@’ key is the HOLD state and is cleared when the ‘@’ key goes ‘RELEASED’.

Then for each other key in the kpd.key[index].kstate that in the PRESSED state, if the flag is set you send the corresponding alternate key, if flag is not set you send the normal key (which will be in kpd.key[index].kchar)

the question is how can you print the ‘@’ symbol though if this is used as a SHIFT key. May be you want a better state machine and you trigger the shifted mode only if ‘@’ has been pressed twice very quickly (double clicked) or held down for a while. Ideally though you really needed a shift key in your keyboard.

I really want also a proper keyboard.
Shift key is bit tricky to put in a if function.
Any example will be really helfull.

look at the MultiKey.ino example, just change the matrix to match yours and see what happens when you press the keys

Adding a if (kpd.key[i].kchar == ‘@‘) .... in the PRESSED and RELEASED sections of the case should not be too difficult to set or clear a boolean

I will try.
As a beginner it is a difficult project for me.

We all started somewhere - give it a try, work from the existing example

// mutantC keyboard v1.4

#include <Keypad.h>
#include <Keyboard.h>

const byte ROWS = 4;
const byte COLS = 11;
bool state = 0 ;

char hexaKeys1[ROWS][COLS] = {
  {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '},
  {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', KEY_BACKSPACE},
  { KEY_CAPS_LOCK , 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',  KEY_RETURN  },
  {KEY_LEFT_CTRL, KEY_LEFT_GUI,  'z', 'x', 'c', 'v', 'b', 'n', 'm', ' ', '@' }
};

char hexaKeys2[ROWS][COLS] = {
  {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '},
  {'q', 'w', 'e', 'r', 't', 'w', 'w', 'w', 'w', 'w', KEY_BACKSPACE},
  { KEY_CAPS_LOCK , 'a', 's', 'd', 'd', 'g', 'h', 'j', 'k', 'l',  KEY_RETURN  },
  {KEY_LEFT_CTRL, KEY_LEFT_GUI,  'z', 'x', 'c', 'v', 'b', 'n', 'm', ' ', KEY_LEFT_SHIFT }
};

byte rowPins[ROWS] = {A0, A1 ,A3 , A2};
byte colPins[COLS] = {9, 16, 14, 15, 8, 7, 6, 5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys1), rowPins, colPins, ROWS, COLS);
Keypad customKeypad2 = Keypad(makeKeymap(hexaKeys2), rowPins, colPins, ROWS, COLS);

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

void loop(){
//  if (customKeypad.getKeys()) {
//    for (int i = 0; i < LIST_MAX; i++) {
//      if (customKeypad.key[i].kchar == '@') {
//        Serial.print(customKeypad.key[i].kchar);
//        Serial.print("=");
//        if (customKeypad.getKeys()) {
//          Serial.print(customKeypad.getKey());
//        }
//        delay(200);
//      } else {
//        Serial.print(customKeypad.key[i].kchar);
//        delay(200);
//      }
//    }
//  }

  char customKey = customKeypad.getKey();
  if (customKey) {
  if (customKey == '@') {
      Serial.print('dd');
    if (state == 0) {
      state = 1;
    } else  {
      state = 0;
    }
  } else {
      Serial.print('jj');
    if (state == 0) {
      char key = customKeypad.getKey();
      Serial.print(key);
    delay(200);
    } else {
      char key = customKeypad2.getKey();
      Serial.print(key);
    delay(200);
    }
  }
  }
}

i write the code
if the presed key is “@” then switch layout to another. if presed again then change again.

but issue i am having is is get “27242” instead of character.

ok. finally got it fixed.

// mutantC keyboard v1.4

#include <Keypad.h>
#include <Keyboard.h>

const byte ROWS = 4;
const byte COLS = 11;
bool state = 0;

char hexaKeys1[ROWS][COLS] = {
  {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '},
  {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', KEY_BACKSPACE},
  {KEY_CAPS_LOCK, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',  KEY_RETURN  },
  {KEY_LEFT_CTRL, KEY_LEFT_GUI,  'z', 'x', 'c', 'v', 'b', 'n', 'm', ' ', '\\' }
};

char hexaKeys2[ROWS][COLS] = {
  {'!', '@', '#', '

, ‘%’, ‘^’, ‘&’, ‘’, ‘(’, ‘)’, ’ ‘},
  {’`’, ‘~’, ‘-’, ‘_’, ‘=’, ‘|’, ‘:’, ‘;’, ‘[’, ‘]’, KEY_BACKSPACE},
  {KEY_CAPS_LOCK, ‘A’, ‘M’, ‘|’, ‘:’, ‘;’, ‘"’, ‘’’, ‘{’, ‘}’,  KEY_RETURN  },
  {KEY_LEFT_CTRL, KEY_LEFT_GUI , ‘/’, '
’, ‘-’, ‘+’, ‘<’, ‘>’, ‘,’, ‘.’, ‘\’ }
};

byte rowPins[ROWS] = {A0, A1 ,A3 , A2};
byte colPins[COLS] = {9, 16, 14, 15, 8, 7, 6, 5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys1), rowPins, colPins, ROWS, COLS);
Keypad customKeypad2 = Keypad(makeKeymap(hexaKeys2), rowPins, colPins, ROWS, COLS);

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

void loop() {
  char key;
  if (state == 0) {
    key = customKeypad.getKey();
  } else {
    key = customKeypad2.getKey();
  }

Keyboard.begin();
  if (key) {
    if (key != ‘\’) {
      Keyboard.write(key);
      delay(200);
    }
   
    if (key == ‘\’ && state) {
      state = 0;
    } else if (key == ‘\’ && !state) {
      state = 1;
    }
  }
  Keyboard.end();
}
[/code]