Hi, all first time posting I think I attached the sketch so anyone can see it but I'm not certain I did.
looking for help with two problems I'm having I'm new to programming.
Problem 1. is I'm trying to emulate the Right shift modifier key on the keyboard but I'm getting sticky key alerts and I'm not sure why I'm assuming its the delay but sense its a modifier key i don't get why it is giving a sticky key alert at all I can turn stick keys off to bypass the problem but id like to know why I'm getting the alert to begin with.
Problem 2., while the program works in that it recognizes that the shift key, is being pressed its function is wonky in that if I hold the emulated
shift key and press any key that can be printed on the keyboard I'll use the ('a') key for the example It will print around 60 ish uppercase A and one lowercase a and then it will loop as long as I hold the keys. I tried to remove the delay but it prints one uppercase A and one lowercase a on loop as long as keys are pressed. I feel like the time delay (); function, is not the right function to get the program to act the way I want it to but I don't know what to use anyone got a suggestion.
#define KEY_RIGHT_SHIFT 0x85
#include <Keyboard.h>
void setup() {
// put your setup code here, to run once:
pinMode (3,INPUT_PULLUP);
Serial.begin (9600);
}
void loop() {
Keyboard.begin ();
if (digitalRead(3) == 0)
{
Keyboard.press(0x85);
delay (100);
Keyboard.release(0x85);
}
Keyboard.end ();
}
sketch_shift.ino (335 Bytes)
Move keyboard.begin() to you setup() function. Don't call keyboard.end(). The Arduino will be a keyboard for the entirety of its run
Look with at the examples in the documentation for keyboard.h, which I found readily and look at some of the examples.
Or look at the example sketch in your IDE:
Examples/02 Digital/StateChangeDetection.
Any of that code will show you how to make something happen once per button press, not continuously as long as the button is pressed, which is what you observe now (and as the code says, of course).
I do not know why you get any lower case or other than the key named.
I use a different library, but the delay can be much shorter. The example I copied was 7 ms, again a different library so check.
HTH
a7
To activate Sticky Keys – Press the Shift Key 5 times in short succession. A tone sounds and the Sticky Keys dialog appears. By default, the cursor is on the Yes button. ... To turn off Sticky Keys once enabled, press 3 or more of the modifier keys (Shift, Ctrl, Alt, Function, Windows Key) at the same time
Since you are repeating the shift key about ten times per second, if the button is down for more than half a second you will pop up the Sticky Keys dialog. Did you want the button to act like a shift key (press to turn on Shift and release to turn off Shift), or did you really want to repeat the shift key on and off ten times per seconds as long as the button was down?
I want it to act like shift press (press to turn on Shift and release to turn off Shift)
macr43:
I want it to act like shift press (press to turn on Shift and release to turn off Shift)
OK. To do that we start with State Change Detection. For a mechanical button it is good to include contact debounce. This is how I like to do it:
const byte ButtonPin = 2;
const unsigned long DebounceTime = 30;
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode (ButtonPin, INPUT_PULLUP); // Button between Pin and Ground
}
void loop()
{
unsigned long currentTime = millis();
boolean buttonIsPressed = digitalRead(ButtonPin) == LOW; // Active LOW
static boolean buttonWasPressed = false;
static unsigned long buttonStateChangeTime = 0;
// Check for button state change and do debounce
if (buttonIsPressed != buttonWasPressed &&
currentTime - buttonStateChangeTime > DebounceTime)
{
// Button state has changed
buttonStateChangeTime = currentTime;
buttonWasPressed = buttonIsPressed;
if (buttonIsPressed)
{
// Button was just pressed
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
// Button was just released
digitalWrite(LED_BUILTIN, LOW);
}
}
}
Now you just have to put Keyboard.press(0x85); where the button is pressed and Keyboard.release(0x85) where the button is released.
thank you so much for your help I have been looking for a tutorial/Reference to use but since I'm so new to coding I have a hard time reading the code itself it's helpful to have a Reference that I know corresponds to the problem I'm working on