# Leonardo Simple keyboard code problem

**URL:** <https://forum.arduino.cc/t/leonardo-simple-keyboard-code-problem/871346>\
**Category:** Programming\
**Created:** [June 6, 2021, 6:01pm UTC](https://forum.arduino.cc/t/leonardo-simple-keyboard-code-problem/871346 "2021-06-06T18:01:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tnory](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@tnory](https://forum.arduino.cc/u/tnory)\
**Post date:** [June 6, 2021, 6:01pm UTC](https://forum.arduino.cc/t/leonardo-simple-keyboard-code-problem/871346/1 "2021-06-06T18:01:06Z")

</div>

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();  
}

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [June 6, 2021, 7:46pm UTC](https://forum.arduino.cc/t/leonardo-simple-keyboard-code-problem/871346/2 "2021-06-06T19:46:37Z")

</div>

How are they supposed to work together?

> **[Flowchart](https://en.wikipedia.org/wiki/Flowchart)**
>
> Pages for logged out editors learn more
> 
> 		 
> A flowchart is a type of diagram that represents a workflow or process. A flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task.
> The flowchart shows the steps as boxes of various kinds, and their order by connecting the boxes with arrows. This diagrammatic representation illustrates a solution model to a given problem. Flowcharts are used in analyzing, designing, documenting or m...

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [June 6, 2021, 8:39pm UTC](https://forum.arduino.cc/t/leonardo-simple-keyboard-code-problem/871346/3 "2021-06-06T20:39:22Z")

</div>

> [@tnory](#):
>
> Keyboard.begin();  
> ...  
> Keyboard.end();

Why would you do that in _loop()_?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [June 7, 2021, 1:56pm UTC](https://forum.arduino.cc/t/leonardo-simple-keyboard-code-problem/871346/4 "2021-06-07T13:56:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 5, 2021, 1:57pm UTC](https://forum.arduino.cc/t/leonardo-simple-keyboard-code-problem/871346/5 "2021-10-05T13:57:55Z")

</div>

Closed on October 5, 2021, 1:57pm UTC

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