Keyboard, acting as a real keyboard ? :)

Hi,
I'm trying to use a arduino keyboard for shortcuts in a lighting program.
I've made a "standard" arduino keyboard, using a Leonardo board, and it works... BUT :slight_smile:
in the lighting program, i have 2 types of buttons : some are on/off, for that, no problem, but some buttons require the user to maintain the key (flash buttons) .
So if i put a very short delay between Keyboard.press and Keyboard.release, flash buttons are ok, but on/off ones no ( according to the pressed time, it's on, or off, on release).
If i put a "normal delay" then, flash buttons aren't working.
So, is there a way to make a arduino keyboiard behave like a real keyboard ?
Thank you :slight_smile:

You can use the MultiKey example from the Keypad library to see how to separately act on keypad PRESSED events and keypad RELEASED events. If you use Keyboard.press(kpd.key[i].kchar) on PRESSED and Keyboard.release(kpd.key[i].kchar) on RELEASED then it will act just like a real keyboard. The OS will auto-repeat the key as long as it is being held.

You know what buttons need to do what. If I understand the question correctly, in some situations you want to detect a state change and react on it (e.g when the button becomes pressed, send a 'short' keypress), and in other situations you want to detect the level and react on it (keypress when button is pressed, keyrelease when button is released).

If that is what you want, write the code that detects which button is pressed and react accordingly.

Many thank's :slight_smile: I'll go this way :slight_smile:

sterretje it's more simple as Johnwasser sait, it's the ability to send a "Keypress" or "key hold" associations between program and hardware are subject to change "on the fly" , i need the hardware to be able to handle both possibilities.
Thank you for your answer :slight_smile:

The Keypad Solution worked for me :

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

char previousKey;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
// Define Keymap
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','a'},
  {'4','5','6','b'},
  {'7','8','9','c'},
  {'*','0','#','d'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connected to the row keypad pins
byte colPins[COLS] = {5, 4, 3, 2}; //connected to the Columns keypad pins

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
  Keyboard.begin(KeyboardLayout_fr_FR);
  pinMode(13,OUTPUT); //a LED is added on pin 13
}
  
void loop(){
  char customKey = customKeypad.getKey();
  KeyState state = customKeypad.getState();
   
   if (state == PRESSED) {
        previousKey = customKey;
        Keyboard.press(customKey);
        digitalWrite(13,HIGH); // light LED
        delay(100);
        }

  else if (state == RELEASED) {
        Keyboard.releaseAll();
        digitalWrite(13,LOW); // switch off LED
       }
    else if (state == HOLD) {
       Keyboard.press(previousKey);
       digitalWrite(13,HIGH); // light LED
       delay(100);
       }
 }

Thank you johnwasser for your help, and sterretje for your answer :slight_smile:

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