Keypress when a button is held

Alright to start off I just want to say this is my first project ever with a Arduino board. I really don't have much experience in coding but this board is a dire part to a project of mine. Also I really couldn't put my problem into the right words for a title, that is the best I could come up with.

I am doing something odd, I am turning a CB Microphone into a computer mic, cause why not. On the CB mic, there is a port which outputs a signal when the microphone button is pushed in. I want to transform this signal into a keypress for push to talk. I already have some code worked out that I copied from a youtube video and did some small edits.

#include <Keyboard.h>    // This is a "built-in" library no need to install

//---------------------------------------------------------
//                           Setup
//---------------------------------------------------------



void setup() {
pinMode(3,INPUT_PULLUP);  // sets pin 3 to input & pulls it high w/ internal resistor

Serial.begin(9600);       // begin serial comms for debugging

}

//---------------------------------------------------------
//                           Loop
//-----------------------------------------------You pressed the button 214 times.----------

void loop() {
  
  
 Keyboard.begin();         //begin keyboard 
 if (digitalRead(3) == 0)  // if buton 3 is pushed
  {
    Keyboard.press('x');  // send single character "A"
    delay(200);           // delay so you don't get 20 A's
  }
  Keyboard.end();                 //stops keybord
}

I thought this would work, but what ends up happening is when I press the button it decides to type "X" forever. So I need a fix where it will only press that key while the button is pressed.

Thanks

EDIT - I am using the Leonardo W/ Headers

What Arduino board are you using?

.

larryd:
What Arduino board are you using?

.

Arduino Leonardo

You press a the key but never release it.

The state change detection example bears investigation, too.

You should attach (begin()) the Keyboard once. There is no reason to ever detach (end()) it.

Press the key. Pause. Release the key. Do that ONCE when the signal becomes HIGH, not over and over when the signal is HIGH.

PaulS:
The state change detection example bears investigation, too.

You should attach (begin()) the Keyboard once. There is no reason to ever detach (end()) it.

Press the key. Pause. Release the key. Do that ONCE when the signal becomes HIGH, not over and over when the signal is HIGH.

Being completely honest, I have no idea what your saying. Like I said this is my first experience with an Arduino board and I really have no background in coding. I think I might need this laid out in basic language.

I think I might need this laid out in basic language.

If you tap a key on your computer keyboard, you get one letter, right? If you hold the key down, you get lots of letters, right?

I do, anyway...............................................

If you want one letter when the sensor BECOMES HIGH, rather than a letter every time you discover that the sensor IS HIGH, you need different code. There is an example that comes with the IDE, called State Change Detection.

So, the first question is how many letters do you want? One when the sensor becomes HIGH or one every time you discover that it is HIGH (which could be thousands of times per second)?

While confused and waiting for a response that I understood better (lol) I fixed the issue I had.

It works exactly how I wanted it to now, When I hold the mic button it will keep pressing the x key until I release the button.

Here is the code I used:

#include <Keyboard.h>    // This is a "built-in" library no need to install

//---------------------------------------------------------
//                           Setup
//---------------------------------------------------------



void setup() {
pinMode(3,INPUT_PULLUP);  // sets pin 3 to input & pulls it high w/ internal resistor

Serial.begin(9600);       // begin serial comms for debugging

}

//---------------------------------------------------------
//                           Loop
//-----------------------------------------------You pressed the button 214 times.----------

void loop() {
  
  
 Keyboard.begin();         //begin keyboard 
 if (digitalRead(3) == 0)  // if buton 3 is pushed
  {
    Keyboard.press('x');  // send single character "A"
    delay(200);           // delay so you don't get 20 A's
  }
  else { Keyboard.releaseAll(); }
  Keyboard.end();                 //stops keybord
}

I simply added the else to the if statement, now it works.

Thank y'all for your help.