sterretje:
I could not get your code working (in a short time) as intended so here is something based on the keypad library.
Before you use below, start simple with an adjusted version of the custom keypad code of the Keypad library and serial prints so you know which key is which.
Next study below and adjust to you needs; I've tried to demonstrate the different options (normal print, macros, special keys).
Code tested on a Leonardo with 3x4 keypad.
/*
Demo code of Keyboard.press and Keyboard.print including use with macros.
Based on the custom keypad code, uses Keyboard.press and Keyboard.print
Macro keys are 1, 2 and 3; tailored to Notepad++
Two special keys, and left ; all supported keys can be found in Keyboard.h
The other keys of the keypad just send the key
Safety:
A0 needs to be connected to ground else keypress / print will not be send
If not, all hanging keys will be released and no keypress / print will be send to the PC
*/
#include <Keypad.h>
#include <Keyboard.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{(char)KEY_ESC, 'a', (char)KEY_LEFT_CTRL}
};
byte rowPins[ROWS] = {4, 5, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
const byte safetyPin = A0;
const byte ledPin = LED_BUILTIN;
void setup()
{
pinMode(safetyPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
// safety net
if (digitalRead(safetyPin) == HIGH)
{
// indicate safety activated
digitalWrite(ledPin, HIGH);
// release all keys that might be hanging
Keyboard.releaseAll();
return;
}
digitalWrite(ledPin, LOW);
byte key = customKeypad.getKey();
// if a key is pressed
if (key != NO_KEY)
{
// test for normal keys
if (key < 0x80)
{
switch (key)
{
// 'delete all' macro
case '1':
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press('a');
Keyboard.release(KEY_RIGHT_CTRL);
Keyboard.press(KEY_DELETE);
break;
// 'undo' macro
case '2':
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press('z');
Keyboard.releaseAll(); // or Keyboard.release(KEY_RIGHT_CTRL)
break;
// "hello world" macro
case '3':
Keyboard.print("Hello world");
break;
default:
Keyboard.press(key);
break;
}
}
else
{
// keys defined in Keyboard.h will directly be handled
Keyboard.press(key);
}
}
}
Keypad used:
![](https://hobbytronics.co.za/content/images/thumbs/0005509_sealed-membrane-4x3-button-key-pad-with-sticker_300.jpeg)
Ok thank ypu I´ll try that out