keyboard.print help

Hey all,

I`m using a Leonardo to do some keyboard.prints when buttons are pressed. The command keyboard.print outputs a single character and then waits for the next press. Is there an easy way to simulate a press and hold as long as the button is pressed?

This is my code:

const byte switchPinA = 2;
byte oldSwitchStateA = HIGH;  // assume switch OFF because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds

void setup ()
  {
  Serial.begin (115200);
  pinMode (switchPinA, INPUT_PULLUP);
    Keyboard.begin();
  }  // end of setup

void loop ()
  {
  // see if switch A is OFF or ON
  byte switchStateA = digitalRead (switchPinA);
  
  // has it changed since last time?
  if (switchStateA != oldSwitchStateA)
    {
    oldSwitchStateA =  switchStateA;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchStateA == LOW)
       {
       Serial.println ("Switch A is ON.");
       Keyboard.print("a");
       }  // end if switchStateA is LOW
    else
       {
       Serial.println ("Switch A is OFF.");
       Keyboard.print("a");
       }  // end if switchStateA is HIGH
    }  // end of state change  
  }  // end of loop

please ignore missing end of this code, I deleted most so you guys can read it.

Have a look at the keyboard.press() and keyboard.release() functions.

Thanks, that helped me! Here is the code:

void setup() 
  {
  pinMode (2, INPUT_PULLUP);
  pinMode (3, INPUT_PULLUP);
  pinMode (4, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);
  pinMode (6, INPUT_PULLUP);
  pinMode (7, INPUT_PULLUP);
  pinMode (8, INPUT_PULLUP);
  pinMode (9, INPUT_PULLUP);
  pinMode (10, INPUT_PULLUP);
  Keyboard.begin(); 
  } // end of setup
  
void loop() 
  {
  //if the button ispressed
  if(digitalRead(2)==LOW)
    {
    //charrcter to be send
    Serial.print("a");
    Keyboard.print("a"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(3)==LOW)
    {
    //charrcter to be send
    Serial.print("b");
    Keyboard.print("b"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(4)==LOW)
    {
    //charrcter to be send
    Serial.print("c");
    Keyboard.print("c"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(5)==LOW)
    {
    //charrcter to be send
    Serial.print("d");
    Keyboard.print("d"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(6)==LOW)
    {
    //charrcter to be send
    Serial.print("e");
    Keyboard.print("e"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(7)==LOW)
    {
    //charrcter to be send
    Serial.print("f");
    Keyboard.print("f"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(8)==LOW)
    {
    //charrcter to be send
    Serial.print("g");
    Keyboard.print("g"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(9)==LOW)
    {
    //charrcter to be send
    Serial.print("h");
    Keyboard.print("h"); 
    } // end of state change

    //if the button ispressed
  if(digitalRead(10)==LOW)
    {
    //charrcter to be send
    Serial.print("i");
    Keyboard.print("i"); 
    } // end of state change
    
  } // end of loop