Use toggle switch to send one time keystoke

The following sketch is not exactly what you want but it should give you some ideas of what you can do.

Note, the Arduino pins are different than in your sketch.

//Version 1.0

//#include <HID-Project.h>
//#include <HID-Settings.h>

//const byte switch_pin               = A6;
//const byte ledR                     = 16;
//const byte ledG                     = 14;

const byte switch_pin               = 8;
const byte ledR                     = 9;
const byte ledG                     = 10;
const byte heartbeatLed             = 13;

//0 = waiting for red   switch push, 1 = red ON timing, 2 = red OFF timing,
//3 = waiting for green switch push, 4 = green ON timing, 5 = green OFF timing
byte myFlag                         = 0;

byte lastSwitchState;

//timing stuff
unsigned long heartbeatMillis;
unsigned long blinkMillis;

unsigned long blinkItervalHigh      = 300;
unsigned long blinkItervalLow       = 700;



//***************************************************************************
void setup()
{
  pinMode(switch_pin, INPUT_PULLUP);  //switch connected to GND, cloed = LOW = pushed
  lastSwitchState = digitalRead(switch_pin);

  pinMode(heartbeatLed, OUTPUT);
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);

} //END of setup()

//***************************************************************************
void loop()
{
  //********************************
  //time to toggle the heartbeat LED ?
  if(millis() - heartbeatMillis >= 500)
  {
    heartbeatMillis = millis();

    //toggle the heartbeat LED
    digitalWrite(heartbeatLed, !digitalRead(heartbeatLed));
  }
  
  //********************************
  //Change in switch state
  byte state = digitalRead(switch_pin);
  if (lastSwitchState != state)
  {
    //update to the new state
    lastSwitchState = state;

    //if we are not Timing, did the switch close ?
    if ((myFlag == 0 || myFlag == 3) && state == LOW)
    {
      //enable Timer
      myFlag++;

      //turn the RED LED ON ?
      if (myFlag == 1)
      {
        digitalWrite(ledR, HIGH);
      }

      //turn the GREEN LED ON ?
      else if (myFlag == 4)
      {
        digitalWrite(ledG, HIGH);
      }

      //restart the Timer
      blinkMillis = millis();
    }
  }

  //********************************
  //RED LED stuff
  if (myFlag == 1 && millis() - blinkMillis >= blinkItervalHigh)
  {
    //change to LED OFF timing
    myFlag = 2;

    //turn the red LED OFF
    digitalWrite(ledR, LOW);

    //restart the Timer
    blinkMillis = millis();
  }

  //**********
  if (myFlag == 2 && millis() - blinkMillis >= blinkItervalLow)
  {
    //change to green switch waiting
    myFlag = 3;

  }

  //********************************
  //GREEN LED stuff
  if (myFlag == 4 && millis() - blinkMillis >= blinkItervalHigh)
  {
    //change to LED OFF timing
    myFlag = 5;

    //turn the green LED OFF
    digitalWrite(ledG, LOW);

    //restart the Timer
    blinkMillis = millis();
  }

  //**********
  if (myFlag == 5 && millis() - blinkMillis >= blinkItervalLow)
  {
    //disable timing
    myFlag = 0;
  }

} //END of loop()

//***************************************************************************