Hi everyone,
I have a fairly basic level in Arduino programming.
I'm trying to build a device to send hotkeys to my pc with an Arduino Pro Micro, in particular the commands that I often use with some softwares.
For some commands like "undo" (ctrl + z) I was able to easily execute them through simple buttons and the switch...case function.
Here's my question:
in the software I use, to access the 'cut' function faster, I hold down the ctrl key and then use the mouse to choose the point where to cut.
In the code attached here, I can't tell arduino to send and hold ctrl function while the button is pressed and to release the "key" when the button is released.
In the code I wrote, the ctrl key is pressed and never released. Maybe the switch...case is the wrong way?
#include <Keyboard.h>
int keys[] = {2, 3, 4, 5, 6,};
void setup() {
Keyboard.begin(); // setup keyboard
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
}
void loop() {
for (int i = 2; i < 7; ++i) {
// check buttons
if(readButton(i)) {
doAction(i);
}
}
}
boolean readButton(int pin) {
// check and debounce buttons
if (digitalRead(pin) == LOW) {
delay(10);
if (digitalRead(pin) == LOW) {
return true;
}
}
return false;
}
void doAction(int pin) {
switch (pin) {
//Redo
case 2:
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('z');
delay(100);
Keyboard.releaseAll();
break;
//Undo
case 3:
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('z');
delay(100);
Keyboard.releaseAll();
break;
//zoom out
case 4:
Keyboard.press('t');
delay(200);
Keyboard.releaseAll();
break;
//zoom in
case 5:
Keyboard.press('r');
delay(200);
Keyboard.releaseAll();
break;
// here's where I need the "Hold cmd" function.
//In this case the hold function is performed but the key is never released.
case 6:
Keyboard.press(KEY_LEFT_GUI);
delay(10);
break;
}
}
Surely there is a very simple way of solving the problem, but at the moment I don't see it.
Thanks in advance to anyone who will help me.
Ettore.