Arduino When keep pressing button a continuous Letter is show

First of all sorry for my weak English

I have Arduino Leonardo and I have a push-button ok?

When I click the button the letter 'W' is printed to the notepad ok?

I Want when I keep holding the button the 'w' Letter is printed why? like in games when I keep holding on 'W' letter the player will move, then when I release my finger the player will stop. Please please please I need your help because I'm a beginner paymydoctor

#include "Keyboard.h"

const int buttonPin = 4;          // input pin for pushbutton
int previousButtonState = HIGH;   // for checking the state of a pushButton

void setup() {
  // make the pushButton pin an input:
  pinMode(buttonPin, INPUT);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonPin);
  // if the button state has changed,
  if ((buttonState != previousButtonState)
      // and it's currently pressed:
      && (buttonState == HIGH)) {
    // type out a message
    Keyboard.print("W");
  }
  // save the current button state for comparison next time:
  previousButtonState = buttonState;
}

You are using the checking the state method that is why.

#include "Keyboard.h"

const int buttonPin = 4;          // input pin for pushbutton


void setup() {
  // make the pushButton pin an input:
  pinMode(buttonPin, INPUT);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // read the pushbutton:
   if (digitalRead(buttonPin) == HIGH) {
    // type out a message
    Keyboard.print("W");
  }
 else if (digitalRead(buttonPin) == LOW) {
    // do something else
  }


}

but as i dont have the keyboard.h to test it here is an other sketch to test with serial :

const int buttonPin = 4;          // input pin for pushbutton


void setup() {
  Serial.begin(9600);
  // make the pushButton pin an input:
  //pinMode(buttonPin, INPUT);

  pinMode(buttonPin, INPUT_PULLUP); // use pull up method to avoid noise, pin must be connected to Grn to read
  //  pin state is HIGH when not pressed 
}

void loop() {
  // read the pushbutton:
  if (digitalRead(buttonPin) == LOW) {
    // type out a message
    Serial.println("W");
  }

  else if (digitalRead(buttonPin) == HIGH) {
    Serial.println("no button");
  }
}

You can try replacing your loop() with this (untested):

void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonPin);
  // if the button state has changed,
  if (buttonState == HIGH) {
    // type out a message
    Keyboard.print("W");
 delay(100) ; // adjust as required. replace with a millis() construct after testing.
  }
  // save the current button state for comparison next time:
  previousButtonState = buttonState;
}

edit: crossed with @caslor

consider

void loop() {
    static unsigned long timeout = 0;
    static unsigned long msecLst = 0;
           unsigned long msec    = millis ();

    int buttonState = digitalRead(buttonPin); // read the pushbutton:

    // if the button state has changed,
    if (buttonState != previousButtonState)  {
        if (buttonState == LOW) {       // pressed
#define ONE_SEC     1000
            Serial.println ("W");
            timeout = ONE_SEC;
            msecLst = msec;
        }
        else                            // released
            timeout = 0;
    }
    else if (timeout && (msec - msecLst > timeout))  {
        Serial.println ("e");
#define REPEAT     200
        timeout = REPEAT;
        msecLst = msec;
    }

    // save the current button state for comparison next time:
    previousButtonState = buttonState;
}

Instead of printing letters, maybe you want to make key presses?

The commands I use in my keyboard encoder are:

#include <Keyboard.h>
Keyboard.press(keyvalue);
Keyboard.release(keyvalue);

The program being used will decide what to do with a held down key.