Keyboard matrix with multiple key

image

Do you have the Keypad library installed and, if so, what version ?

If not, then install it

Ok i found it

so the code becomes like this

void loop(){
  char key = keypad.getKey();
  
  if (key==1){
     Keyboard.press(ctrlKey);
  Keyboard.press('n');
  delay(100);
  Keyboard.releaseAll();
  }

If you have '1' in the keypad definition then you must test for '1' rather than 1

A comment
It won't make any difference to functionality but in my opinion it would be better to format the code differently, ie

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

    if (key == '1')
    {
        Keyboard.press(ctrlKey);
        Keyboard.press('n');
        delay(100);
        Keyboard.releaseAll();
    }
}

To my mind that makes the code more readable because it is easier to see the dependant code block

Another thought
If you are going to detect and act on several keypad inputs then using if/else soon gets messy and you may be better using switch/case which makes the code easier to read and maintain

void loop()
{
    char key = keypad.getKey();
    switch (key)
    {
        case '1':
            Keyboard.press(ctrlKey);
            Keyboard.press('n');
            delay(100);
            Keyboard.releaseAll();
            break;
        case '2':
            //some other action
            break;
        case '3':
            //yet another action
            break;
    }
}

the afternoon when I will be at home. I will try to make a code with 3x3 matrix with your good advice. and I will send you the codes again to point out my mistakes.

Thank you again for your valuable time.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'}
};
byte rowPins[ROWS] = {5, 4, 3,}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

void setup(){
  Serial.begin(9600);
}
  
void loop()
{
    char key = keypad.getKey();
    switch (key)
    {
        case '1':
            Keyboard.press(KEY_RIGHT_CTRL);
            Keyboard.press(KEY_INSERT);
            delay(100);
            Keyboard.releaseAll();
            break;
        case '2':
           Keyboard.press(KEY_DOWN_ARROW);
            break;
        case '3':
            Keyboard.press(KEY_UP_ARROW);
            break;
        case '4':
            Keyboard.press(KEY_RIGHT_ARROW);
            break;
        case '5':
            Keyboard.press(KEY_LEFT_ARROW);
            break;
        case '6':
            Keyboard.press(KEY_F1);
            break;
        case '7':
            Keyboard.press(KEY_F2);
            break;
        case '8':
            Keyboard.press(KEY_F3);
            break;
        case '9':
            Keyboard.press(KEY_F4);
            break;
        
    }
}

Good Evening.

do you see this program correctly?

why when I type #include < in the library options that opens, there is no keyboard.h while I have it installed?

I think that the Keyboard library is built in for the Leonardo and does not show up in the available libraries like that

Either use the Sketch/Include Library menu or simply type #include <Keyboard.h> in the sketch

did you have time to see the program I have uploaded?

It compiles OK once the Keyboard library is #included

Do you need to release the keys at some point other than case '1' ?

any case neded keyboard.release();
I did all this, but when I compiled, I got the following message

fatal error: keyboard.h: No such file or directory
 #include <keyboard.h>
          ^~~~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: keyboard.h: No such file or directory

Do a global search on your computer for file keyboard.h

If you can't find it
unzip this zip-file in folder "libraries"
Keyboard.zip (21.6 KB)

That's because the name of the library is Keyboard.h which is why I suggested

at the beginning I have also #include <keypad.h> and
#include <keyboard.h>

The name of the library is NOT keyboard.h

ok i'm stupid... now understen what did you mean.

finaly the code compiled

and the code is.

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

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'}
};
byte rowPins[ROWS] = {5, 4, 3,}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

void setup(){
  Serial.begin(9600);
  Keyboard.begin();
  
}
  
void loop()
{
    char key = keypad.getKey();
    switch (key)
    {
        case '1':
            Keyboard.press(KEY_RIGHT_CTRL);
            Keyboard.press(KEY_INSERT);
            delay(100);
            Keyboard.releaseAll();
            break;
        case '2':
           Keyboard.press(KEY_DOWN_ARROW);
           delay(100);
          Keyboard.releaseAll();
            break;
        case '3':
            Keyboard.press(KEY_UP_ARROW);
            delay(100);
            Keyboard.releaseAll();
            break;
        case '4':
            Keyboard.press(KEY_RIGHT_ARROW);
            delay(100);
           Keyboard.releaseAll();
            break;
        case '5':
            Keyboard.press(KEY_LEFT_ARROW);
            delay(100);
           Keyboard.releaseAll();
            break;
        case '6':
            Keyboard.press(KEY_F1);
            delay(100);
           Keyboard.releaseAll();
            break;
        case '7':
            Keyboard.press(KEY_F2);
            delay(100);
           Keyboard.releaseAll();
            break;
        case '8':
            Keyboard.press(KEY_F3);
            delay(100);
           Keyboard.releaseAll();
            break;
        case '9':
            Keyboard.press(KEY_F4);
            delay(100);
         Keyboard.releaseAll();
            break;
        
    }
}

Thank you very very much the keyboard workings...:grin::grin:

1 Like

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