Hi, I am building a shortcut panel for photoshop. I have 5 buttons hooked up and they are all working, but the tasks that are not just single letters are repeating 7 times rather than just once.
To be more specific, I can get a single press of 'e' and 'b', but when I press my undo shortcut, it undos 7 times rather than once. All of my if statements are formatted the same way, and I am wondering if HEX codes have to be formatted differently than a simple letter press.
Here is my code:
#include <Keyboard.h> //keyboard library
int blueButton = 2;
int whiteButton = 3;
int redButton = 4;
int greenButton = 5;
int yellowButton = 6;
int blueStroke = 0;
int whiteStroke = 0;
int redStroke = 0;
int greenStroke = 0;
int yellowStroke = 0;
int prevBlueStroke = 0;
int prevWhiteStroke = 0;
int prevRedStroke = 0;
int prevGreenStroke = 0;
int prevYellowStroke = 0;
void setup() {
// put your setup code here, to run once:
pinMode (blueButton, INPUT);
digitalWrite (blueButton, HIGH);
pinMode (whiteButton, INPUT);
digitalWrite (whiteButton, HIGH);
pinMode (redButton, INPUT);
digitalWrite (redButton, HIGH);
pinMode (greenButton, INPUT);
digitalWrite (greenButton, HIGH);
pinMode (yellowButton, INPUT);
digitalWrite (yellowButton, HIGH);
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
// put your main code here, to run repeatedly:
blueStroke = digitalRead(blueButton);
whiteStroke = digitalRead(whiteButton);
redStroke = digitalRead(redButton);
greenStroke = digitalRead(greenButton);
yellowStroke = digitalRead(yellowButton);
if ((blueStroke != prevBlueStroke) && (blueStroke == HIGH)) {
Keyboard.press(91);
delay(1000);
Keyboard.release(91);
}
if ((whiteStroke != prevWhiteStroke) && (whiteStroke == HIGH)) {
Keyboard.press('e');
delay(1000);
Keyboard.releaseAll();
}
if ((redStroke != prevRedStroke) && (redStroke == HIGH)) {
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('z');
delay(1000);
Keyboard.releaseAll();
}
if ((greenStroke != prevGreenStroke) && (greenStroke == HIGH)) {
Keyboard.press('b');
delay(1000);
Keyboard.releaseAll();
}
if ((yellowStroke != prevYellowStroke) && (yellowStroke == HIGH)) {
Keyboard.press(93);
delay(1000);
Keyboard.release(93);
}
prevBlueStroke = blueStroke;
prevWhiteStroke = whiteStroke;
prevRedStroke = redStroke;
prevGreenStroke = greenStroke;
prevYellowStroke = yellowStroke;
}
J-M-L:
do you expect 91 to be '[' and 93 to be ']' ??
pressing for 1 second is a long time. do you need to wait that long?
seems you activate on HIGH. how are your buttons really wired? seems you use INPUT_PULLUP so being pressed would become LOW?
Please correct your post above and add code tags around your code: [code]`` [color=blue]// your code is here[/color] ``[/code].
It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)
I'm not quite sure how to answer the question of how they are "really" wired, but I just have 5V to the voltage prong with a 1k resistor and ground to ground. I activated them on high because initially the keyboard press would happen and pressing the button would stop it rather than initiate it, so I reversed the process.
I just corrected the format, and thanks for the shortcut hint, that just saved me a lot of time! I've never had formal programming training so I just sort of format it by eye.
mattlogue:
Wow you can use it to keyboard? This idea is cool! I would love shortcuts IDE, Photoshop, and excel
Yeah! I am making this as a foot pedalboard (like I use with my guitar effects board) so I can draw on photoshop with a tablet and not take my hands off to do shortcuts
runaway_pancake: @matt
keyboard.h is part of the Leonardo / Esplora environment
Actually you can use keyboard methods with any 32u4 or SAMD micro based boards and send keystrokes to an attached computer through their micro’s native USB port.
I activated them on high because initially the keyboard press would happen and pressing the button would stop it rather than initiate it, so I reversed the process.
When you declare a pin as an INPUT, setting it HIGH activates the PULLUP function. Basically this code
is the same as doing pinMode (blueButton, INPUT_PULLUP);
See pinMode() and this example for more details and how to wire a button with INPUT PULLUP and no need for a resistor.
Also to improve a bit your code, you could declare your pins as const byte instead of Integers
Macros are sequences of events (such as keystrokes, mouse clicks, and delays) that can be played back to help with repetitive tasks. They can also be used to replay sequences that are long or difficult to run. You can assign a macro recorded in Microsoft Mouse and Keyboard Centre to a key or a mouse button.