Help needed with toggle switch and keyboard input

I am busy creating a prototype controller. the problem I am having is with a toggle switch that have two on positions. each on position on the toggle must send a single keyboard input. but with my current sketch when the switch is flipped in either on position it just keeps on sending the keyboard input. How can I get either on position to send just a single character/Key?

/

/ Keyboard - Version: Latest 
#include <Keyboard.h>

/*


*/
int toggle1 = 6;
int toggle1State = 0;

int toggle2 = 5;
int toggle2State = 0;

void setup() 
{  

  pinMode(toggle1, INPUT);
  pinMode(toggle2, INPUT);
  
  Keyboard.begin();

}

void loop() 
{

  toggle1State = digitalRead(toggle1);
  toggle2State = digitalRead(toggle2);
  
  if (toggle1State == HIGH)
  {
    Keyboard.press('r');
  }
  else{Keyboard.release('r');}
  
  if (toggle2State == HIGH)
  {
    Keyboard.press('t');
  }
  else{Keyboard.release('t');}
  
  
}

when the switch is flipped in either on position it just keeps on sending the keyboard input

You need to detect when the switch becomes on rather than when it is on.

Look at the StateChangeDetection example in the IDE

Okay so had a look at the suggested example and came up with the following. it works as I want but I would just like to know if the code is optimal and if there is a way I can improve it?

/ Keyboard - Version: Latest 
#include <Keyboard.h>

/*


*/
const int toggle1 = 6;
int toggle1State = 0;
int lastToggle1State = 0;

void setup() 
{  

  pinMode(toggle1, INPUT);
  pinMode(toggle2, INPUT);
  
  Keyboard.begin();

}

void loop() 
{

  toggle1State = digitalRead(toggle1);
  toggle2State = digitalRead(toggle2);
  
  
  if (toggle1State != lastToggle1State) 
  {

    if (toggle1State == HIGH) 

    {
 
      Keyboard.press('1');
    }
 
    else if (toggle1State == LOW) 
    {

      Keyboard.release('1');

    }
   
    delay(50);
  }

  lastToggle1State = toggle1State;

  
  if (toggle1State == lastToggle1State)
  {
    if (toggle1State == HIGH)
    {
      Keyboard.release('1');
    }
    else if (toggle1State == LOW)
    {
      Keyboard.release('1');
    }
    
    delay(50);
  }

  
  
}

if there is a way I can improve it?

As the code posted does not compile then the answer is yes, it can be improved

It does compile and upload the sketch to the chip. I probably should have mentioned that the end goal is to use 4-6 toggle switches all using 2 inputs. Here is the complete sketch I currently have with two joysticks each with a push button, one push button and then the toggle switch.

// Keyboard - Version: Latest 
#include <Keyboard.h>

/*


*/
#define joyLX A2 // Pin for the left joystick x-axis
#define joyLY A3 // Pin for the left joystick y-axis
int joyLB = 3;   // Pin for the left joystick button
int joyLBstate = 0;
int joyLBstate1 = 0;

#define joyRX A1 // Pin for the right joystick x-axis
#define joyRY A0 // Pin for the right joystick y-axis
int joyRB = 2;   // Pin for the right joystick button
int joyRBstate = 0;
int joyRBstate1 = 0;

int pushButton = 4; //// Pin for the push button
int pushButtonState = 0;

const int toggle1 = 6; // Pin for toggle switch first High position
int toggle1Counter = 0;
int toggle1State = 0;
int lastToggle1State = 0;

const int toggle2 = 5; // Pin for toggle switch second High position
int toggle2Counter = 0;
int toggle2State = 0;
int lastToggle2State = 0;


void setup() 
{
  pinMode(joyLB,INPUT);
  digitalWrite(joyLB, HIGH);

  pinMode(joyRB, INPUT);
  digitalWrite(joyRB, HIGH);
  
  pinMode(pushButton,INPUT);
  
  pinMode(toggle1, INPUT);
  pinMode(toggle2, INPUT);
  
  Serial.begin(9600);
  
  Keyboard.begin();
}

void loop() 
{
  int lxValue=analogRead(joyLX);
  int lyValue=analogRead(joyLY);
  Serial.print(lxValue);
  Serial.print("\t");
  Serial.println(lyValue);
  
  joyLBstate = digitalRead(joyLB);
  Serial.println(joyLBstate);
  
  //joystick move in -y axis direction
  if (lxValue>=0 && lyValue<=10)
  {
    Keyboard.press('a');
  }
  else{Keyboard.release('a');}
  
  //joystick move in -x axis direction
  if (lxValue<=0 && lyValue>=10)
  {
    Keyboard.press('s');
  }
  else{Keyboard.release('s');}
  
  //joystick move in +x axis direction
  if (lxValue>=1000 && lyValue>=100)
  {
    Keyboard.press('w');
  }
  else{Keyboard.release('w');}
  
  //joystick move in +y axis direction
  if (lxValue>=100 && lyValue>=1000)
  {
    Keyboard.press('d');
  }
  else{Keyboard.release('d');}
  
  // if the left joy stick button is pushed
  if (joyLBstate == LOW)
  {
    Serial.println("Switch = High");
    Keyboard.press('e');
  }
  else{Keyboard.release('e');}
  
  pushButtonState = digitalRead(pushButton);
  
  if (pushButtonState == HIGH)
  {
    Keyboard.press('f');
  }
  else{Keyboard.release('f');}
  
  toggle1State = digitalRead(toggle1);
  
  // compare the buttonState to its previous state
  if (toggle1State != lastToggle1State) 
  {
    // if the state has changed, increment the counter
    if (toggle1State == HIGH) 
    {
      // if the current state is HIGH then the button went from off to on:
      Keyboard.press('1');
    } 
    else if (toggle1State == LOW) 
    {
      Keyboard.release('1');
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastToggle1State = toggle1State;
  
  if (toggle1State == lastToggle1State)
  {
    if (toggle1State == HIGH)
    {
      Keyboard.release('1');
    }
    else if (toggle1State == LOW)
    {
      Keyboard.release('1');
    }
    
    delay(50);
  }
  
  toggle2State = digitalRead(toggle2);
  
  if (toggle2State != lastToggle2State) 
  {
    // if the state has changed, increment the counter
    if (toggle2State == HIGH) 
    {
      // if the current state is HIGH then the button went from off to on:
      Keyboard.press('2');
    } 
    else if (toggle2State == LOW) 
    {
      Keyboard.release('2');
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastToggle2State = toggle2State;
  
  if (toggle2State == lastToggle2State)
  {
    if (toggle2State == HIGH)
    {
      Keyboard.release('2');
    }
    else if (toggle2State == LOW)
    {
      Keyboard.release('2');
    }
    
    delay(50);
  }


  
  
  
}

The code in reply #2 does not compile because toggle is not declared