How to type only ONE letter with an analog switch? PLEASE HELP!

Hi. My first post on this forum. I'm very new to Arduino and I have already stuck in the programming part of my first project. I guess this is very simple stuff for most of you.

So here's what I want my project to be:

I've built a small panel with four toggle switches. I want them to be able to type one letter each when I switch them to ON. They are connected as inputs to 2, 3, 4 and 5.
For example, I want to write the letter "A" when I turn the input 2-switch to on. My problem is that my computer is typing hundreds of "A" until I turn the switch to OFF.

I want my computer to write only one "A" and then stop, even if my switch is still turned ON.

How do i do this? Hope you can help!

Here's my code:

#include <Keyboard.h>

int pin1 = 2;
int pin2 = 3;
int pin3 = 4;
int pin4 = 5;

void setup() {
  pinMode(pin1, INPUT_PULLUP);
  pinMode(pin2, INPUT_PULLUP);
  pinMode(pin3, INPUT_PULLUP);
  pinMode(pin4, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  
  if (digitalRead(pin1) == LOW)
  { 
    Keyboard.press('A'); 
  } else {
    Keyboard.release ('A');
  } 


}

sketch_sep11d.ino (371 Bytes)

You need to type the letter when the switch becomes ON, not while the switch is ON. The state change detection method will sense when the switch becomes ON (the OFF to ON transition).

you will need to change the logic to match your switch wiring. The example is for a switch wired to 5V instead of ground.