key press in unity code example

Example 1: unity get key down

// KeyCode.Space : The key you press
// GetKeyDown : Is true once, when the key is pressed 
// If you want it to be true WHILE the key is pressed, try GetKey() instead
if(Input.GetKeyDown(KeyCode.Space))
{
	// Do something
}

Example 2: unity when key pressed

void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            print("space key was pressed");
        }
    }

Example 3: unity getkey keycode

Input.GetKey(KeyCode.Space))

Example 4: unity key pressed

using UnityEngine;

public class Egsample_script : MonoBehaviour
{
    [SerializeField] KeyCode your_key;
    //select a key under inspektor
    void Update()
    {
        if (Input.GetKey(your_key))
        {
            print("your selekted key was pressed");
        }
    }
}