# Trying to program multiple buttons for a keyboard

**URL:** <https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330>\
**Category:** Programming\
**Created:** [March 15, 2023, 3:41pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330 "2023-03-15T15:41:54Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 5:10pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/21 "2023-03-15T17:10:51Z")

</div>

I told you that there was a better way !

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 5:12pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/22 "2023-03-15T17:12:26Z")

</div>

And there definitely was now to learn some more and grow on this as time goes

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 15, 2023, 5:12pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/23 "2023-03-15T17:12:43Z")

</div>

> [@mbishopfof](#):
>
> that cleans it up so nicely

No problem. I hope you understand the changes I made well enough to use the same technique in the future. If not, please ask any questions.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [March 15, 2023, 6:42pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/24 "2023-03-15T18:42:00Z")

</div>

Hello [mbishopfof](https://forum.arduino.cc/u/mbishopfof)

One solution using OOP is missing in this thread.

You can rearrange the used button and related letter simply.

```cpp
constexpr int ButtonPin[] {A0, A1, A2, A3, A4, A5, A6, A7};
constexpr char Button2Char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};

```

The compiler is a clever and smart guy to generate a running sketch out of the edited settings for button and related letter.  
That´s cool.

Check, try, play and have fun:

```cpp
/* BLOCK COMMENT
  - https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330
  - Trying to program multiple buttons for a keyboard
  - This sketch may contain traces of C++.
  - In case of indisposition:
  - https://www.learncpp.com/
  - Hardware:
  - Thanks to LarryD
  - https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  - Tested @ Arduino: DUE[X] - UNO [] - Nano []
*/
#define ProjectName "Trying to program multiple buttons for a keyboard"

// I am too lazy to type
#define usl unsigned long
// include libaries
#include <Keyboard.h>
// make variables
constexpr int ButtonPin[] {A0, A1, A2, A3, A4, A5, A6, A7};
constexpr char Button2Char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
// make structures
struct TIMER
{
  usl now;
  usl span;
  int onOff;
};
struct BUTTON2CHAR
{
  int pin;
  char letter;
  int stateOld;
  TIMER debounce;
  TIMER release;
  void make (int pin_, char letter_)
  {
    pin = pin_;
    pinMode(pin, INPUT_PULLUP);
    letter = letter_;
    stateOld = LOW;
    debounce.now = millis();
    debounce.span = 20;
    release.now = millis();
    release.span = 50;
    release.onOff = LOW;
  }
};
BUTTON2CHAR button2Chars[sizeof(ButtonPin) / sizeof(ButtonPin[0])];
// tools
void heartBeat(int LedPin)
{
  static bool setUp = false;
  if (!setUp) pinMode (LedPin, OUTPUT), setUp = !setUp;
  digitalWrite(LedPin, (millis() / 500) % 2);
}
void setup()
{
  Serial.begin(115200);
  Serial.print(ProjectName), Serial.print(" in "), Serial.println( __FILE__ );
  int element = 0;
  for (auto &button2Char : button2Chars) button2Char.make(ButtonPin[element], Button2Char[element]), element++;
  Keyboard.begin();
}
void loop()
{
  usl currentMillis = millis();
  heartBeat(LED_BUILTIN);
  for (auto &button2Char : button2Chars)
  {
    if (currentMillis - button2Char.debounce.now >= button2Char.debounce.span)
    {
      button2Char.debounce.now = currentMillis;
      int stateNew = digitalRead(button2Char.pin) ? LOW : HIGH;
      if (button2Char.stateOld != stateNew)
      {
        button2Char.stateOld = stateNew;
        if (stateNew == HIGH)
        {
          Keyboard.press(button2Char.letter);
          button2Char.release.now = currentMillis;
          button2Char.release.onOff = HIGH;
        }
      }
    }
    if (currentMillis - button2Char.release.now >= button2Char.release.span && button2Char.release.onOff == HIGH)
    {
      button2Char.release.onOff = LOW;
      Keyboard.release(button2Char.letter);
    }
  }
}

```

Have a nice day and enjoy coding in C++.

p.s. All buttons are connected to gnd.

---

<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:** [September 11, 2023, 6:42pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/25 "2023-09-11T18:42:16Z")

</div>

Closed on September 11, 2023, 6:42pm UTC

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

[Previous page](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330.md?page=1)
