# Leonardo: custom keyboard - simulate holding key and simultaneous presses

**URL:** <https://forum.arduino.cc/t/leonardo-custom-keyboard-simulate-holding-key-and-simultaneous-presses/1025915>\
**Category:** Programming\
**Created:** [August 26, 2022, 11:45am UTC](https://forum.arduino.cc/t/leonardo-custom-keyboard-simulate-holding-key-and-simultaneous-presses/1025915 "2022-08-26T11:45:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![madvicer](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@madvicer](https://forum.arduino.cc/u/madvicer)\
**Post date:** [August 26, 2022, 11:45am UTC](https://forum.arduino.cc/t/leonardo-custom-keyboard-simulate-holding-key-and-simultaneous-presses/1025915/1 "2022-08-26T11:45:11Z")

</div>

Hello, I am new to arduino and I am in a process of making a simple custom keyboard, which consists of four keys -WAD and V. I intend to use it to control a game I am making in Unity. This is a tutorial which I have followed:  
[https://www.arduino.cc/education/diy-game-controllers](https://www.arduino.cc/education/diy-game-controllers)

there are however two problems which I have partly solved:

1. simulate keyboards hold key function

- out of the box from this tutorial the keys after press were bursting about 10-20 characters at one press, making unity overhelmed. Also when I pressed it for longer periods, the characters were being printed even after I released the key. So I introduced to my code While function, which prints keys in delays only after first character is printed immediately. This works fine for walking the Unity character, but when I need the button to be on hold, there is some problem and it does not work

1. simultaneous presses  
I dont understand why this code does not allow to have two keys pressed at same time - in my unity game I want to go forward while turning left or right.

code here:

```cpp
#include <Keyboard.h> 
// Emulating keyboard or mouse functions only work on 32u4 or SAMD micro-based boards.
void setup() {
  //Start serial connection
  Serial.begin(9600);
  //Configure pins as an input and enable the internal pull-up resistor,
  pinMode(2, INPUT_PULLUP); //a
  pinMode(3, INPUT_PULLUP); //w
  pinMode(4, INPUT_PULLUP); //v
  pinMode(5, INPUT_PULLUP); //d
  pinMode(6, INPUT_PULLUP); //killswitch
  Keyboard.begin();
  
}

boolean pressed = false;

int delaytime = 10;
int initialdelaytime = 150;

int d = 100;
boolean pressedD = false;
boolean holdD = false;

int w = 119;
boolean pressedW = false;
boolean holdW = false;

int a = 97;
boolean pressedA = false;
boolean holdA = false;

int v = 118;
boolean pressedV = false;
boolean holdV = false;

void loop() {
//If corresponding button is pressed

  if (digitalRead(6) == LOW) {
    pressed = true;
   
  }

  if(pressed == true)
  {
         

//KEY a
if (digitalRead(2) == LOW && pressedA == false)
         {
            pressedA=true;
            //Send an ASCII 'A',
            
            Keyboard.write(a);
            delay(initialdelaytime);
            holdA = true;
          }
          while(digitalRead(2) == LOW && holdA == true)
          {
            Keyboard.write(a);
            delay(delaytime);
            }
            if(digitalRead(2) == HIGH)
            {
              holdA = false;
              pressedA=false;
              }

        
//KEY w
if (digitalRead(3) == LOW && pressedW == false)
         {
            pressedW=true;
            //Send an ASCII 'W',
            
            Keyboard.write(w);
            delay(initialdelaytime);
            holdW = true;
          }
          while(digitalRead(3) == LOW && holdW == true)
          {
            Keyboard.write(w);
            delay(delaytime);
            }
            if(digitalRead(3) == HIGH)
            {
              holdW = false;
              pressedW=false;
              }

          

//KEY v
         if (digitalRead(4) == LOW && pressedV == false)
         {
            pressedV=true;
            //Send an ASCII 'V',
            
            Keyboard.write(v);
            delay(initialdelaytime);
            holdV = true;
          }
          while(digitalRead(4) == LOW && holdV == true)
          {
            Keyboard.write(v);
            delay(0);
            }
            if(digitalRead(4) == HIGH)
            {
              holdV = false;
              pressedV=false;
              }

//KEY d
         if (digitalRead(5) == LOW && pressedD == false)
         {
            pressedD=true;
            //Send an ASCII 'D',
            
            Keyboard.write(d);
            delay(initialdelaytime);
            holdD = true;
          }
          while(digitalRead(5) == LOW && holdD == true)
          {
            Keyboard.write(d);
            delay(delaytime);
            }
            if(digitalRead(5) == HIGH)
            {
              holdD = false;
              pressedD=false;
              }

         
   }
 else {
    Keyboard.releaseAll();
    
}
}

```

Thanks in advance for reading and help

---

<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:** [August 26, 2022, 12:37pm UTC](https://forum.arduino.cc/t/leonardo-custom-keyboard-simulate-holding-key-and-simultaneous-presses/1025915/2 "2022-08-26T12:37:12Z")

</div>

> [@madvicer](#):
>
> simultaneous presses

You use ".write()" which releases the key immediately. You will not have simultaneous presses without using:

```arduino
  Keyboard.press('A');
  Keyboard.press('B');
  Keyboard.releaseAll();

```

or

```arduino
  Keyboard.press('A');
  Keyboard.press('B');
  Keyboard.release('A');
  Keyboard.release('B');

```

---

<div class="post-metadata">

**Author:** ![madvicer](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@madvicer](https://forum.arduino.cc/u/madvicer)\
**Post date:** [August 26, 2022, 1:20pm UTC](https://forum.arduino.cc/t/leonardo-custom-keyboard-simulate-holding-key-and-simultaneous-presses/1025915/3 "2022-08-26T13:20:05Z")

</div>

Thank you very much, I have just wanted to say that I have figured it out. Perhaps this should also be mentioned in the tutorial I went through...

So just i case anyone stumbles across this, this is the solution I am happy with:

```cpp
#include <Keyboard.h>

// Emulating keyboard or mouse functions only work on 32u4 or SAMD micro-based boards.
void setup() {
  //Start serial connection
  Serial.begin(9600);
  //Configure pins as an input and enable the internal pull-up resistor,
  pinMode(2, INPUT_PULLUP); //a
  pinMode(3, INPUT_PULLUP); //w
  pinMode(4, INPUT_PULLUP); //v
  pinMode(5, INPUT_PULLUP); //d
  pinMode(6, INPUT_PULLUP); //killswitch
  Keyboard.begin();

}

boolean pressed = false;

int d = 100;
boolean pressedD = false;
boolean holdD = false;

int w = 119;
boolean pressedW = false;
boolean holdW = false;

int a = 97;
boolean pressedA = false;
boolean holdA = false;

int v = 118;
boolean pressedV = false;
boolean holdV = false;

void loop() {
  //If corresponding button is pressed

  if (digitalRead(6) == LOW) {
    pressed = true;

  }

  if (pressed == true)
  {

    //KEY a
    if (digitalRead(2) == LOW)
    {

      Keyboard.press(a);

    }
    if(digitalRead(2) == HIGH)
    {
      Keyboard.release(a);
    }

//KEY w
    if (digitalRead(3) == LOW)
    {

      Keyboard.press(w);

    }
    if(digitalRead(3) == HIGH)
    {
      Keyboard.release(w);
    }

   //KEY v
    if (digitalRead(4) == LOW)
    {

      Keyboard.press(v);

    }
    if(digitalRead(4) == HIGH)
    {
      Keyboard.release(v);
    }

   //KEY d
    if (digitalRead(5) == LOW)
    {

      Keyboard.press(d);

    }
    if(digitalRead(5) == HIGH)
    {
      Keyboard.release(d);
    }

  }
  else {
    Keyboard.releaseAll();

  }
}

```

---

<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:** [February 22, 2023, 1:20pm UTC](https://forum.arduino.cc/t/leonardo-custom-keyboard-simulate-holding-key-and-simultaneous-presses/1025915/4 "2023-02-22T13:20:32Z")

</div>

Closed on February 22, 2023, 1:20pm UTC

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