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?
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?
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.
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?
// 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?
THANK YOU!
Your link was VERY informative, and now I have a working 3 button input! Yay!
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).