Prevent user to hold key button down

Hi

I’m looking for a solution that a key or the function behind it is only executed once and then when the player has the opportunity to do so. This means that the key must not be held down, but may only be executed if you clicking.

Here my code:

Blockquote

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
       
        PlayerMovement.moveSpeed = PlayerMovement.moveSpeed + 20f; 
        
    }

    if (Input.GetKeyUp(KeyCode.LeftShift))
    {

        PlayerMovement.moveSpeed = PlayerMovement.moveSpeed - 20f;
        
        
    }
}

Here try this:

bool pressed = false;
bool reset = false;

void Update()
 {
     if(pressed && !reset)
     {
         PlayerMovement.moveSpeed = PlayerMovement.moveSpeed - 20f;
         reset = true;
     }

     if (Input.GetKeyDown(KeyCode.LeftShift) &  !pressed)
     {
        
         PlayerMovement.moveSpeed = PlayerMovement.moveSpeed + 20f;

         pressed = true;

     }
     if (Input.GetKeyUp(KeyCode.LeftShift))
     {
         pressed = false;
         reset = false;
     }
 }