Holding a key press on numberpad

Hi everyone,
I've recently been working on a macropad for my desk and I am using a number pad to simulate key bindings on my keyboard ot press certain buttons. I was able to find a tutorial online to do this, and I used this code written by Brian Lough. In this code, when I press down on the number pad, it only sends one key press. I want to know how I should go about trying to implement a code so that whil ei have the key pressed on my number pad, it keeps sending that key bind until I let go of the key. The code written by Brian Lough is below and all of the key bindings in the cases are my own that I use. I am pretty new to programing and arduinos, so any help would be appreciated! Thank you!

/*******************************************************************
 *  A simple Macro keyboard built with Arduino Pro Micro and a        
 *  4*4 Button Matrix.
 *  
 *  Parts:
 *  Arduino Pro Micro* - http://s.click.aliexpress.com/e/FQmMd5uc 
 *  4*4 Keypad Button Matrix* - http://s.click.aliexpress.com/e/CqnjTgGg 
 *  
 *  * = Affilate
 *                                                                 
 *  Written by Brian Lough
 *  YouTube: https://www.youtube.com/brianlough
 *  Tindie: https://www.tindie.com/stores/brianlough/
 *  Twitter: https://twitter.com/witnessmenow                                 
 *******************************************************************/

// ----------------------------
// Standard Libraries
// ----------------------------

#include "Keyboard.h"
// Library with a lot of the HID definitions and methods
// Can be useful to take a look at it see whats available
// https://github.com/arduino-libraries/Keyboard/blob/master/src/Keyboard.h

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <Keypad.h>
// This library is for interfacing with the 4x4 Matrix
// 
// Can be installed from the library manager, search for "keypad"
// and install the one by Mark Stanley and Alexander Brevig
// https://playground.arduino.cc/Code/Keypad/

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

// the library will return the character inside this array
// when the appropriate button is pressed.
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9 }; //connect to the column pinouts of the keypad

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

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

// This will hold down all the following buttons.
void sendMacroCommand(uint8_t key) {
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(key);
}

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

  if (key) {
    Serial.println(key);
    switch (key) {
      case '1':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press(KEY_F9);
        break;
      case '2':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press(KEY_F10);
        break;
      case '3':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press(KEY_F11);
        break;
      case '4':
        sendMacroCommand(KEY_F4);
        break;
      case '5':
        sendMacroCommand(KEY_F5);
        break;
      case '6':
        sendMacroCommand(KEY_F6);
        break;
      case '7':
        sendMacroCommand(KEY_F7);
        break;
      case '8':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press(KEY_F1);
        break;
      case '9':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press(KEY_F2);
        break;
      case '0':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press(KEY_F8);
        break;
      case '*':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press(KEY_F7);
        break;
      case '#':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_SHIFT);
        Keyboard.press('m');
        break;
      case 'A':
        Keyboard.press(KEY_LEFT_CTRL);
        Keyboard.press('q');
        break;
      case 'B':
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press('t');
        break;
      case 'C':
        Keyboard.press(KEY_RIGHT_CTRL);
        Keyboard.press(KEY_RIGHT_ALT);
        Keyboard.press('c');
        break;
      case 'D':
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press('y');
        break;
    }

    delay(100);
    Keyboard.releaseAll(); // this releases the buttons
  }
}

Can you provide links to github where these versions of Keyboard.h and Keypad.h which you are using can be found ?
It is almost certain that the method keypad.getKey() returns a NULL unless there has been a new key press. There may be a similar method which returns the pressed key whenever it is called.

The links are in the comments in their code.

1 Like

I believe that is referring only to the emulated keyboard for the host, not the Arduino connected keypad which the OP is attempting to use to generate a repeating stream of key presses.

@nikoolpatel
You can read the keypad quite easily without a library. This example repeats keys while these are held in. You can play with it in a simulator: keypad_V0_01 - Wokwi ESP32, STM32, Arduino Simulator

Have a look at the example sketch MultiKey from the Keypad library, it shows how to read the keypad and see if a key is pressed / held / released.

This https://github.com/Chris--A/Keypad/blob/master/examples/MultiKey/MultiKey.ino does look like it could work for the OP. I've put in a simulator: https://github.com/Chris--A/Keypad/blob/master/examples/MultiKey/MultiKey.ino

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