Hold pressed button keyboard emulation

Hello, I’m new to this forum and to arduino. I’m trying to setup a keyboard emulator with a 32u4 chip arduino.

I can get it to give me a character by pressing a button but I can’t seem to get it to give me more when I hold the button down. I’ve tried a few different things but with no luck…

I would love some help,

Here’s the code that’s so far working for me

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

const byte ROWS = 7; //four rows
const byte COLS = 6; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{‘w’,‘i’,‘1’,‘7’,‘c’,‘v’},
{‘a’,‘j’,‘2’,‘8’,‘m’,‘u’},
{‘s’,‘k’,‘3’,‘9’,’,’,’.’},
{‘d’,‘l’,‘4’,‘0’,‘z’,‘x’},
{‘e’,‘h’,‘5’,‘t’,‘b’,‘g’},
{‘q’,‘n’,‘6’,‘r’,‘y’,‘o’},
{’ ‘,’ ‘,’[’,’]’}
};
byte rowPins[ROWS] = {6, 5, 4, 3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {16, 15, 14, 9, 8, 7}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){

Serial.begin(9600);
}

void loop(){
char customKey = customKeypad.getKey(); //figure out which key

if(customKey == ‘y’) {
Keyboard.press(8); //8 is ASCII code for backspace
delay(100);
Keyboard.releaseAll();
} else if(customKey == ‘o’){
Keyboard.press(’ ');
delay(100);
Keyboard.releaseAll();
}else if(customKey) {
Keyboard.press(customKey);
delay(100);
Keyboard.releaseAll();
delay(10);

}

}

keyboard.txt (1.38 KB)

Are you trying to make the Arduino appear as a keyboard attached to a PC?

If you want to cause the PC to repeat keys then you need to send separate key-down and key-up signals (I may have the names slightly wrong but you should get the idea.

...R

I've tried a few different things but with no luck..

The only luck involved in programming is finding the job you love.

The code you posted does something. You need to tell us what it actually does.
You expect it to do something. You need to tell us what you expect it to do.

Hey, sorry about the miss information. What I’m trying to do is have a Arduino run as a keyboard emulator on my computer so I can build a controller for a game.

I’m using a keypad matrix to increase the number of buttons I need and when I call for a character(ie.press a button) I get that character. So thats good, but when I press and hold that button I only get one character instead of a whole line…

I get this “h”
instead of this “hhhhhhhhhhhhhhhhhhh”

Replace your Keyboard.press() and Keyboard.releaseAll() calls with Serial.print() statements. Do you detect the key being held down?

The Keypad library has other methods besides getKey(). One of them, getState(), looks interesting.

No, I guess not as with the getkey it only detects what two pins are reading. Not sure if the keypad is looking for HIGH vs LOW.

I think it's something to do with a Hold key

Heres a link, but I can seem to get the code to work with mine.

I reckon there are two separate issues that need to be attended to. First your program needs to know when the user pressed a key on the keypad and when he releases that key. If you were using digitalRead() to detect the keys that would be very straightforward.

And secondly, when a key is pressed or released the Arduino needs to send an appropriate message to the PC.

...R

So using a keypad matrix with keyboard emulation won't work for how I'm calling a button press. ie. just I wire to from pin 16 to pin 6 I get a 'w'. and changing my code to not have Keyboard.releaseAll() then gives me 'wwwwwwww that goes on for ever, I have to unplug the arduino to stop it.

So how would I use digitalRead(). would I have to call each pin and grn them?

or read output vs input?

So how would I use digitalRead(). would I have to call each pin and grn them?

I don't know how to intend to grin a pin. Or was that supposed to be green?

The Keypad library already manages the reading of the pins that the keypad is connected to, so it knows what row and column is pressed, AND whether that same row and column was pressed last time (so the key is being held). You just need to ask it if the key being pressed is being held.

Sorry, I meant "Ground"

You just need to ask it if the key being pressed is being held.

So how should I go about telling it that its a press or a press and hold. I just want it work like a normal keyboard, so the user can input a press function or a press and hold function.

would it work to use a digitalRead() or getState() I've tried so many things and nothing is working. :confused:

would it work to use a digitalRead()

No.

or getState()

Yes.

I've tried so many things and nothing is working.

I've typed up a bunch of responses, but I'm not going to show any of them to you.

Got it! Thanks for the help.

#include <Keypad.h>

#include <Keyboard.h>

// – Keypad related begin

char previousPressedKey;

boolean hasReleasedKey = false;

const byte ROWS = 7; //four rows

const byte COLS = 6; //four columns

//define the cymbols on the buttons of the keypads

char keys[ROWS][COLS] = {

{‘w’,‘i’,‘1’,‘7’,‘c’,‘v’},

{‘a’,‘j’,‘2’,‘8’,‘m’,‘u’},

{‘s’,‘k’,‘3’,‘9’,’,’,’.’},

{‘d’,‘l’,‘4’,‘0’,‘z’,‘x’},

{‘e’,‘h’,‘5’,‘t’,‘b’,‘g’},

{‘q’,‘n’,‘6’,‘r’,‘y’,‘o’},

{’ ‘,’ ‘,’[’,’]’}

};

byte rowPins[ROWS] = {6, 5, 4, 3, 2, 1, 0}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {16, 15, 14, 9, 8, 7}; //connect to the column pinouts of the keypad

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

// – Keypad related ends

void setup(){

Serial.begin(9600);

delay(1000);

}

void loop(){

delay(50);

keypadLoop();

}

void keypadLoop() {

// Keypad related

char key = keypad.getKey();

KeyState state = keypad.getState();

if (state == PRESSED && key != NO_KEY) {

previousPressedKey = key;

hasReleasedKey = false;

Keyboard.print(key);

}

else if (state == RELEASED && !hasReleasedKey) {

// Multiple RELEASED events occur when there had not been HOLD

Keyboard.print(key);

hasReleasedKey = true;

}

else if (state == HOLD) {

Keyboard.print(key);

Keyboard.println(previousPressedKey);

}

}