Leonardo Simple keyboard code problem

hello! im new at arduino i just wanna make two button macro key with arduino leonardo, one "CTRL+K" another is "Q" i can make it work separately but can not combine two codes. thank you in advance.

code for (CTRL+K)

#include <Keyboard.h>

char ctrlKey = KEY_LEFT_GUI;

void setup() {

pinMode(2, INPUT_PULLUP);

Keyboard.begin();
}

void loop() {
while (digitalRead(2) == HIGH) {

delay(500);

}
delay(1000);

Keyboard.press(ctrlKey);
Keyboard.press('k');
delay(100);
Keyboard.releaseAll();

delay(1000);
}


Code for (Q)

#include <Keyboard.h>

void setup() {
pinMode(3,INPUT_PULLUP);

Serial.begin(9600);

}

void loop() {

Keyboard.begin();
if (digitalRead(3) == 0)
{
Keyboard.press('q');
delay(200);
}
else { Keyboard.releaseAll(); }
Keyboard.end();
}

How are they supposed to work together?

Why would you do that in loop()?

Your code for 'Ctrl-K' is going to wait until Pin 2 goes HIGH. While it is waiting, Pin 3 is never checked.

You should look at the StateChangeDetection example to see how to perform actions when a button goes from Unpressed to Pressed or from Pressed to Unpressed. That will get rid of all of the delays and wait loops.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.