Keyboard.press only printing one character

I have some simple code based on this tutorial:

I have three switches I need to act as a pressed-and-held keyboard key.

My code is very simple:

#include <Keyboard.h>

void setup() {
  
Keyboard.begin();
 
//Button connections
pinMode(2, INPUT_PULLUP); //keyboard char A
pinMode(3, INPUT_PULLUP); //keyboard char B
pinMode(4, INPUT_PULLUP); //keyboard char C
}
 
void loop() {
  
// Check the switches:
int buttonState2 = digitalRead(2);
int buttonState3 = digitalRead(3);
int buttonState4 = digitalRead(4);

 
// press 'A' Key
if (buttonState2 == LOW) {
  Keyboard.press('A');
  }
else {
  Keyboard.release('A');
}

 
// press 'B' Key
if (buttonState3 == LOW) {
  Keyboard.press('B');
}
else{
  Keyboard.release('B');
}
  
 
// press 'C' Key
if(buttonState4 == LOW) {
  Keyboard.press('C');
}
else{
  Keyboard.release('C');
}

}

However, when I upload the code I only get one letter then the a beep and then the "sticky keys" pop-up comes up ("Do you want to turn on Sticky Keys?")..

Should these be while statements instead of if statements?

Not having played with it, I'd say yes, they should be whiles.

If's will only call .press() once.

if(buttonState4 == LOW) 
{
  while(buttonSate4==LOW)
    {
     Keyboard.press('C');
    }
  Keyboard.release('C');
}
while (digitalRead(4) == LOW)
    Keyboard.write ('C');

Be warned - that will write a lot of Cs! Maybe throw in a delay.

Marmotjr:

if(buttonState4 == LOW) 

{
  while(buttonState4==LOW)
    {
    Keyboard.press('C');
    }
  Keyboard.release('C');
}

buttonState4 won't change there, so that will be an infinite loop.

buttonState4 won't change there, so that will be an infinite loop.

I tested the code, and you are correct.

When I press a button it acts as though a keyboard key is being pressed and held down (yay!), but it does not keyboard.release when I release the button. I have to unplug it to get it to stop.

This seems like it should be an easy task!

What code will keyboard.release when I release the button?

Calling write as I did in reply #2 does a press and release. Why not use that?

Nick:

I have written a game/app and this will be used as a control for that.
I need to keep a key pressed until it is released, not just press and immediately release.
Like a game controller, but with keyboard keys.
I need all buttons active all the time, I can't get stuck in a loop and other buttons are never triggered.

EDIT: I checked your code for

while (digitalRead(4) == LOW)
    Keyboard.write ('C');

and while it does spit out a lot of keyboard keys, it makes my character start/stop/start/stop because each one is a keyboard press and keyboard release. :frowning:

I need a computer-interpreted 'keyboard key pressed' when I press the button, then 'keyboard key released' when I let go of the button..

I also need ALL buttons active at once.. Possible?

Oh I see. So more like this:

// press 'A' Key
if (digitalRead(2) == LOW) 
  {
  Keyboard.press('A');
  delay (10);  // debounce
  while (digitalRead(2) == LOW)
    { } // wait for switch to be released
  Keyboard.release('A');  // release key
  }  // end of "A" switch pressed

I also need ALL buttons active at once.. Possible?

Yes but not the way I showed there. :slight_smile:

I also need ALL buttons active at once.. Possible?

In that case you need transition detection. See Gammon Forum : Electronics : Microprocessors : Switches tutorial

So the logic would be:

  • If switch 2 transitions from HIGH to LOW, send keypress "A"
  • If switch 2 transitions from LOW to HIGH, send keyrelease "A"

Otherwise do nothing.

Thanks Nick, I will explore that..

:wink:

EDIT:

THANK YOU! :slight_smile:
Your link was VERY informative, and now I have a working 3 button input! Yay!
:slight_smile:

Update:

For future reference, the max number of actual keypressed characters you can send via a USB keyboard is 6. According to this website:

USB protocol limitation - A max of 10 simultaneous key presses are recognized, 6 non-modifier keys ('w', 'a', 's', 'd', etc) + 4 modifier keys (Shift, Caps, Ctrl, etc). Although you are limited to 6 regular keys you are still guaranteed that any combination of keys will be recognized properly if you have an n-key rollover keyboard. I would guess that most people would not need support for more keys than this. I would also guess that the 6 key limit may have had something to do with braille input requirements rather than someone choosing an arbitrary limit (although that doesn't explain why the limit exists in the first place).

I have a working 3 button input! Yay!

3DPiper, can you post a copy of your keyboard code for this?
I am working on a similar project and have the same problem that you described.

I posted my code here: http://forum.arduino.cc/index.php?topic=423799.0