When keep holding button a continuous Letter is printed

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

This is my code

#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;
}

Also posted at:

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

This is my code

#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;
}

Because I Need solution ASAP:(
The stackoverflow one didnt work for me

whenever i click the button
the 'W' letter enters in a loop and keeps printing not stopping

Does this work?

#include <Keyboard.h>

#define KEY_REPEAT_TIME 50ul        //mS    time between keypresses

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

byte 
    swLast;                        // for checking the state of a pushButton
bool 
    bPress;
unsigned long 
    tNow,    
    tRepeat;
    
void setup() 
{
    // make the pushButton pin an input:
    pinMode(buttonPin, INPUT);              //make sure things are wired properly for "HIGH" switch closures
    swLast = digitalRead( buttonPin );
    
    bPress = false;
    
    // initialize control over the keyboard:
    Keyboard.begin();
}

void loop() 
{
    ReadButton();
    if( bPress )
    {
        tNow = millis();
        if( (tNow - tRepeat) >= KEY_REPEAT_TIME )
        {
            tRepeat = tNow;
            Keyboard.print("W");
                       
        }//if
        
    }//if
    
}//loop

void ReadButton( void )
{
    byte
        swNow;
        
    swNow = digitalRead( buttonPin );
    if( swNow != swLast )
    {
        swLast = swNow;
        if( swNow == HIGH )
        {
            bPress = true;
            tRepeat = 0;
            
        }//if
        else
        {
            bPress = false;
            
        }//else

    }//if
    
}//ReadButton

Your previous button state is being updated to HIGH when you push the button, then it is still HIGH when you hold the button - your 'if' statement is then not fulfilled and you don't print W. Just remove that portion of your if statement (and the previousButtonState as it's no longer used).

if(buttonState != previousButtonState && buttonState == HIGH)
{
   Keyboard.print("W");
}

change it to

if(buttonState == HIGH)
{
   Keyboard.print("W");
}

I've merged your other cross-post @SirMajed.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.