Facebook

Unity Input System: Get Key, Get Key Down, and Get Key Up

by Liam Cubicle Published January 25, 2024

An essential part of the Unity Input System is accepting and responding to keyboard inputs. Keyboard inputs serve as channels for communicating with input devices during gameplay. There are three primary key input methods in the Input class: GetKey, GetKeyDown, and GetKeyUp.

In this article, you will learn everything you need to know about each of these β€œkey” input methods. πŸ˜‰.

How to use Input.GetKey method in Unity

The GetKey method is used to query the state of input devices, including keyboards. It can be used to check whether a specific keyboard key is pressed down by the user and then perform the required actions. This method is used within the Update function in Unity. It detects input once per frame and checks whether a certain key is currently being pressed.

Here is an example implementation of the Get Key method in Unity:


using UnityEngine;
public class KeyInputExample: MonoBehaviour 
{
  void Update() 
  { 
    // Check if the space key is pressed
    if (Input.GetKey(KeyCode.Space)) 
    { 
      // Perform some action when the space key is pressed
      Debug.Log("Space key is pressed");
    }

    // Check if the A key is pressed   
    if (Input.GetKey(KeyCode.A)) 
    { 
      // Perform some action when the A key is pressed
      Debug.Log("A key is pressed");
    }
  }
}

In the code above, the Update function is called to detect input once per frame. Then, it checks whether the Space key or A key is currently being pressed using the Input.GetKey method.

πŸ“„ Resources:

How to use Input.GetKeyDown method in Unity

The get key down method of Unity’s input class is used just like the get key method explained above. It is used to check if a certain key was pressed during the current frame. It returns true during the frame the user starts pressing down a certain key. Just like the get key method, this method is also used in the Update function since it detects input once per frame.

Here is an example implementation of the get key down method in Unity:

using UnityEngine;
public class KeyInputExample: MonoBehaviour 
{

  void Update() 
  {
    // Check if the space key was pressed
    if (Input.GetKeyDown(KeyCode.Space)) 
    {
      // Perform some action when the space key is pressed
      Debug.Log("Space key was pressed");
    }

    // Check if the A key was pressed
    if (Input.GetKeyDown(KeyCode.A)) {
      // Perform some action when the A key is pressed   
      Debug.Log("A key was pressed");
    }
  }
}

In the code above, the Input.GetKeyDown method was called within the Update function to check if the space key or A key was pressed. Then, if the condition is true, the associated actions will be executed.

πŸ“„ Resources:

How to use Input.GetKeyUp method in Unity

This is another method in the Input class similar to the get key method. It returns true if a certain key was released during the current frame. Just like the other methods we’ve talked about; this method is called within the Update function.Here is an example implementation of the get key up method in Unity:

using UnityEngine;
public class KeyInputExample: MonoBehaviour {
  void Update() {
    
    // Check if the space key was released 
    if (Input.GetKeyUp(KeyCode.Space)) 
    {
      // Perform some action when the space key is released
      Debug.Log("Space key was released");
    }
    
    // Check if the A key was released 
    if (Input.GetKeyUp(KeyCode.A)) 
    {
      // Perform some action when the A key is released 
      Debug.Log("A key was released");
    }
  }
}

πŸ“„ Resources:

In the code above, the Input.GetKeyUp method was called within the Update function to check if the space key or A key was released. Then, if the condition is true, the associated actions will be executed.

Conclusion

As a developer, it is essential that you are comfortable collecting and responding to player input in your game. After all, a game is only a game if it is interactive πŸ™‚. As you continue to develop your project, be sure to use referenced KeyCodes in a global Keymap so that you can enable players to rebind input. Alternatively, consider using the new Input system, which is a little complex to start with, but offers more flexibility around input key bindings and how they map to actions.

Free download: Indie Game Marketing Checklist

Download now

Category

character controllers and input

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free