Input Handling (Keyboard, Mouse) In Unity

Last Updated : 4 May, 2026

Games are interactive because they respond to player actions. Whether pressing a key to jump, moving the mouse to aim, or touching the screen to shoot, handling input is what makes a game feel alive. Unity provides the Input class to detect player input across all devices.

Input Class

The Input class is Unity's gateway to all player input. It provides methods and properties to check the state of keyboards, mice, touch screens, controllers, and joysticks.

  • Available anywhere in your scripts (just type Input).
  • Works across all platforms automatically.
  • Provides both discrete (press/release) and continuous (axis) input.
Input-Handling-In-Unity
Inputs Handling In Unity

Handling Keyboard Input

Unity provides three main methods to detect keyboard input, each serving a different purpose.

GetKeyDown() – One-time press detection

C#
void Update()
{// Runs ONCE when key is pressed
    if (Input.GetKeyDown(KeyCode.Space))
    {
        Jump();
    }
    if (Input.GetKeyDown(KeyCode.E))
    {
        Interact();
    }
}
  • Returns true for exactly one frame when the key is pressed
  • Perfect for jumping, shooting, interacting, or any single-action event

GetKey() – Continuous hold detection

C#
void Update()
{// Runs EVERY frame while key is held
    if (Input.GetKey(KeyCode.LeftShift))
    {
        Run();
    }
    if (Input.GetKey(KeyCode.W))
    {
        MoveForward();
    }
}
  • Returns true every frame as long as the key remains pressed
  • Perfect for running, walking, or any continuous action

GetKeyUp() – Release detection

C#
void Update()
{// Runs ONCE when key is released
    if (Input.GetKeyUp(KeyCode.Space))
    {
        StopJumping();
    }
}
  • Returns true for exactly one frame when the key is released
  • Perfect for releasing a charged attack or stopping an action

Common KeyCode Values

Movement keys:

  • KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D
  • KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow

Action keys:

  • KeyCode.Space: Jump
  • KeyCode.LeftShift: Run
  • KeyCode.E: Interact
  • KeyCode.R: Reload
  • KeyCode.Tab: Inventory
  • KeyCode.Escape: Pause menu

Mouse keys:

  • KeyCode.Mouse0: Left click
  • KeyCode.Mouse1: Right click
  • KeyCode.Mouse2: Middle click

Using Input Axes for Smooth Movement

For smooth movement like walking or looking around, axes are better than individual key checks.

C#
void Update()
{
    // Returns value between -1 and 1
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");
    // Move player smoothly
    Vector3 movement = new Vector3(horizontal, 0, vertical);
    transform.Translate(movement * speed * Time.deltaTime);
}

Predefined axes in Unity:

  • Horizontal: A/D and Left/Right arrows (left = -1, right = +1)
  • Vertical: W/S and Up/Down arrows (down = -1, up = +1)
  • Mouse X: Mouse horizontal movement
  • Mouse Y: Mouse vertical movement
  • Mouse ScrollWheel: Scroll wheel input

GetAxis() vs GetAxisRaw():

  • GetAxis(): Smooth, gradual values (good for movement)
  • GetAxisRaw(): Instant -1, 0, or 1 (good for snapping)
C#
// Smooth acceleration
float smooth = Input.GetAxis("Horizontal");
// Instant response (no smoothing)
float raw = Input.GetAxisRaw("Horizontal");

Checking Any Key or Key Combination

Sometimes you need to check if any key was pressed or detect combinations.

C#
void Update()
{
    // Check if ANY key was pressed this frame
    if (Input.anyKeyDown)
    {
        Debug.Log("A key was pressed!");
    }
    // Check for key combinations (Ctrl + S)
    if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.S))
    {
        SaveGame();
    }
    // Check for modifier keys
    if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
    {
        // Either shift key is held
    }
}

Handling Mouse Cursor

Controlling the cursor is important for different game types.

C#
void Start()
{   // Lock cursor to center of screen
    Cursor.lockState = CursorLockMode.Locked;
    // Hide the cursor
    Cursor.visible = false;
}
void Update()
{
    // Press Escape to free the cursor
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }
    // Click to lock cursor again
    if (Input.GetMouseButtonDown(0))
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
}

Cursor lock states:

  • CursorLockMode.None: Cursor moves freely, can leave window
  • CursorLockMode.Locked: Cursor locked to center, hidden
  • CursorLockMode.Confined: Cursor confined to game window

Common Errors and Solutions

  • Input won’t work in Update() if the script is disabled or the game isn’t running.
  • Using GetKey for jumping causes continuous jumps; use GetKeyDown instead.
  • Missing Time.deltaTime with axis movement makes it frame rate dependent.
  • Cursor may stay locked after the game ends; unlock it on disable or game over.
  • Axis values not resetting may require adjusting Gravity in the Input Manager.
Comment
Article Tags:

Explore