← Back to blog character controllers and input

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

by Michael Sacco Jan 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.

Continue reading

Non-static method requires a targetHow to make a countdown timer in UnityPricing Strategy for Indie Game DevelopersUnity: How to Change Your SkyboxHow to add Friction to a Rigidbody in UnityUnity Standard Assets: The Essential GuideTop 10 Recent Games Developed with UnityUnity Sprite MasksHow to move or copy a Unity projectThe Ultimate Guide to Sun and Moon Positioning in Super Simple Skybox for Unity

Build your game with OccaSoftware

Learn on the go. Try OccaSoftware for free, and explore all the tools and assets you need to start, build, and launch your game.

Start now

Try OccaSoftware for free, no commitment required.