TSW-4 I'm making an attempt to create my own controller with an arduino leonardo. Things are going quite well except those functions which require the ctrl or shift key. For example when the following code is used to toggle the cruise control button(ctrl-r) Keyboard.press(KEY_LEFT_CTRL); Delay(2); Keyboard.press('r'); delay(2); Keyboard.releaseAll(); When the arduino runs this code the button is pushed....and keeps being pushed although I release the keys. I also tried to release the keys apart from eachother. I wonder if someone who made his own controller has a solution for this and wants to share it.
I think your problem might be how you detect the change of state for a button. You should do Keyboard.press when the button changes state from IDLE/RELEASED to PRESSED/HOLD, and Keyboard.release when it changes state the other way round. How do you detect the button press/release? Can you share that code? Personally, I've used Keypad library to create a button box, and I haven't had the issue you mention.
I use a pcf8574 to handle some inputs, this one is called bank22 . void cruise_control_toggle(){ if (digitalRead(savePin)==1){ // prevent the keyboard keeps firing and the leonardo gets stuck byte bankval = bank22.read8() & 128; // pin 7 if (bankval == 128) { Keyboard.press(KEY_LEFT_CTRL); delay(1); Keyboard.print('r'); delay(10); Keyboard.releaseAll(); } } } Btw, I purchased an ipac 4 thinking that would solve all my problems but when i use that device the button also gets stuck.
I assume this function is called repeatedly from inside Arduino's loop() function. I don't see a way to detect transitions, just check the state, which means your code executes a lot of key presses and releases, instead of just one. Ideally, you want your button state to translate into key press/release actions, but once. When you press the button, Arduino should send one key press. When you release the button, Arduino should send the key release. Your code will send the key presses, then release command, then more key presses and release, and so on, for as long as you keep the button pressed. It is not how a keyboard works. I can recommend this video for inspiration on how to connect buttons to Arduino, and how to use the Keypad library to read the state and convert into key presses/releases:
I think I found the solution, releasing a character key before releasing the ctrl key is mandatory. If you don't do that TSW gets confused and holds the button somehow.