Keyboard.press once?

How can I use this function once?

How to use this forum
Please read this:-
How to use this forum
Because your post is very silly.

In what aspect is this "very silly?"

jAMDup:
In what aspect is this "very silly?"

Can you make a guess (even if it is completely wrong) about the sort of reply that someone could make in reply to your original question.

It is a bit like the question "how can I win the race"

...R

On first keypress set a flag. Ignore further keypresses while the flag is set. When ready to accept new keypresses clear the flag.

Thank you.

This code doesn't work, any idea why?

int SwitchPin = 12;
boolean okToPrint = true;
//int Debounce = 0;


void setup() 
{
  pinMode(SwitchPin,INPUT);
  digitalWrite(SwitchPin,HIGH);
  Keyboard.begin();
}
////////////////////////////////////////////
void loop() 
{
  okToPrint = true; 
////////////////////////////////////////////
   if (digitalRead(SwitchPin)== HIGH && okToPrint)
   {
   Keyboard.press('n');
   Keyboard.releaseAll();
   okToPrint = false;
   }
////////////////////////////////////////////   
   else
   {
    Keyboard.press('O');
    Keyboard.releaseAll();  
    okToPrint = false; 
   }

}

Don't you need to include a keyboard library and declare an instance of keyboard or are you using a Leonardo?

Sorry, I should of made that clear, yes I'm using a Leonardo.

jAMDup:
In what aspect is this "very silly?"

No context.
If you would have read the link you would know how to ask a question.

The real answer is only to use it once.

If you use it once, you'll just have to use it again, and again, and again...

I want it to output the character once.

Oh! THE character!

I think we're going to need a pry-bar.

jAMDup:
I want it to output the character once.

The put it in the setup function.

Why doesn't this work then?
Nothing is being output

int SwitchPin = 12;

void setup() 
{
  pinMode(SwitchPin,INPUT);
  digitalWrite(SwitchPin,HIGH);
  Keyboard.begin();

  
  if (digitalRead(SwitchPin)== HIGH)
   {
    Keyboard.press('P');
   }

   else
   {
    
    Keyboard.press('W');
    }
  
}

void loop() 
{

}

Because you missed out the

// while the serial stream is not open, do nothing:
   while (!Serial) ;

As described here:-

It didn't help, I tried a moving the "Switch pin" to pin 13 and the LED reacts accordingly. The setup is I have a DTDP toggle switch and I'm trying to make a custom USB controller. So I want the same output each time the switch is flicked. Thanks

int SwitchPin = 13;

void setup() 
{
  pinMode(SwitchPin,INPUT);
  digitalWrite(SwitchPin,HIGH);
  Keyboard.begin();

   while (!Serial); 
   
   
   while(digitalRead(SwitchPin) > HIGH)
   {
    Keyboard.press('P');
    Keyboard.releaseAll();
   }
}

void loop() 
{

}

No the "while (!Serial)" must be before anything else in the setup function.

So I want the same output each time the switch is flicked.

But that is not only doing it once, it is doing it every time a switch is CHANGED. It is called a state change.

So have a variable that keeps track of the last switch state and then the if statement says

switchState = digitalRead(SwitchPin);
if ( switchState == HIGH && lastSwitchState == LOW){
// the code you want to do
}
lastSwitchState = switchState

And do that in the loop not the setup, the setup runs only once which is what you said you wanted to do but now you say you don't.

Still nothing. No printing/pressing. Thanks anyway.

I used most of the example code and applied it somewhat. But it seems I'm back to square one. Nothing is printing.

int SwitchPin = 12;
int switchState = 0;         
int lastswitchState = 0;  

void setup() 
{
  while (!Serial); 
  pinMode(SwitchPin,INPUT);
  digitalWrite(SwitchPin,HIGH);
  Keyboard.begin();


  if (digitalRead(SwitchPin)== HIGH)
  {
    Keyboard.press('P');
    }  
}


void loop(){    
   
    if (switchState != lastswitchState){
      {
        Keyboard.press('N');
      }
    if (switchState == HIGH) 
      {
        Keyboard.println("on");
      }
    else 
      {
        Keyboard.println("off");
      }
    delay(50);
    }
    lastswitchState = switchState;

}