Bird
Raised Fist0
Unityframework~5 mins

Keyboard input (GetKey, GetKeyDown) in Unity - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does Input.GetKey do in Unity?

Input.GetKey checks if a specific key is being held down during the current frame. It returns true as long as the key is pressed.

Click to reveal answer
beginner
How is Input.GetKeyDown different from Input.GetKey?

Input.GetKeyDown returns true only on the exact frame when the key is first pressed down, unlike Input.GetKey which returns true while the key is held.

Click to reveal answer
beginner
Write a simple Unity C# code snippet that prints "Jump!" once when the spacebar is pressed.
void Update() {
    if (Input.GetKeyDown(KeyCode.Space)) {
        Debug.Log("Jump!");
    }
}
Click to reveal answer
intermediate
Why might you use Input.GetKeyDown instead of Input.GetKey for a jump action?

Because jumping usually happens once per key press, Input.GetKeyDown ensures the jump triggers only once when the key is pressed, avoiding repeated jumps while holding the key.

Click to reveal answer
beginner
What will Input.GetKey(KeyCode.LeftArrow) return if the left arrow key is not pressed?

It will return false because the key is not being held down.

Click to reveal answer
Which Unity input method returns true only on the frame a key is first pressed?
AInput.GetKeyDown
BInput.GetKey
CInput.GetKeyUp
DInput.GetButton
What does Input.GetKey(KeyCode.A) return if the 'A' key is held down?
AFalse
BOnly true on first frame
CTrue
DOnly true on key release
If you want to detect when a key is released, which method should you use?
AInput.GetButtonDown
BInput.GetKey
CInput.GetKeyDown
DInput.GetKeyUp
Which method would you use to check if the spacebar is currently pressed every frame?
AInput.GetKeyDown(KeyCode.Space)
BInput.GetKey(KeyCode.Space)
CInput.GetKeyUp(KeyCode.Space)
DInput.GetButtonDown("Jump")
What will happen if you use Input.GetKeyDown inside Update() to move a character continuously while holding a key?
ACharacter moves only once per key press
BCharacter does not move
CCharacter moves continuously
DCharacter moves on key release
Explain the difference between Input.GetKey and Input.GetKeyDown in Unity.
Think about holding a key versus pressing it once.
You got /3 concepts.
    Describe a scenario in a game where using Input.GetKeyDown is better than Input.GetKey.
    Consider actions that should not repeat if key is held.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does Input.GetKeyDown(KeyCode.Space) detect in Unity?
      easy
      A. If the space key is being held down continuously
      B. If any key is pressed
      C. If the space key is released
      D. The exact frame when the space key is first pressed

      Solution

      1. Step 1: Understand GetKeyDown behavior

        Input.GetKeyDown returns true only on the first frame the key is pressed.
      2. Step 2: Apply to space key

        When KeyCode.Space is used, it detects the first press of the space bar only.
      3. Final Answer:

        The exact frame when the space key is first pressed -> Option D
      4. Quick Check:

        GetKeyDown = first press frame [OK]
      Hint: GetKeyDown triggers only once on key press [OK]
      Common Mistakes:
      • Confusing GetKeyDown with GetKey
      • Thinking it detects key release
      • Assuming it detects any key
      2. Which of the following is the correct syntax to check if the "W" key is being held down in Unity?
      easy
      A. Input.GetKey(KeyCode.W)
      B. Input.GetKeyDown(KeyCode.W)
      C. Input.GetKeyUp(KeyCode.W)
      D. Input.IsKeyPressed(KeyCode.W)

      Solution

      1. Step 1: Identify method for continuous key hold

        Input.GetKey returns true every frame the key is held down.
      2. Step 2: Match syntax for "W" key

        The correct syntax is Input.GetKey(KeyCode.W) to check if "W" is held.
      3. Final Answer:

        Input.GetKey(KeyCode.W) -> Option A
      4. Quick Check:

        GetKey = held down check [OK]
      Hint: Use GetKey for continuous key press detection [OK]
      Common Mistakes:
      • Using GetKeyDown instead of GetKey for hold detection
      • Using non-existent method IsKeyPressed
      • Confusing GetKeyUp with GetKey
      3. What will be printed if the following Unity code runs and the player holds down the "A" key for 3 seconds?
      void Update() {
        if (Input.GetKeyDown(KeyCode.A)) {
          Debug.Log("Pressed A");
        }
        if (Input.GetKey(KeyCode.A)) {
          Debug.Log("Holding A");
        }
      }
      medium
      A. "Pressed A" printed many times, "Holding A" once
      B. "Pressed A" and "Holding A" printed once each
      C. "Pressed A" once and "Holding A" many times over 3 seconds
      D. No output because GetKeyDown doesn't detect held keys

      Solution

      1. Step 1: Understand GetKeyDown behavior

        Input.GetKeyDown(KeyCode.A) triggers only once when the key is first pressed.
      2. Step 2: Understand GetKey behavior

        Input.GetKey(KeyCode.A) triggers every frame while the key is held down, so it prints repeatedly during 3 seconds.
      3. Final Answer:

        "Pressed A" once and "Holding A" many times over 3 seconds -> Option C
      4. Quick Check:

        GetKeyDown = once, GetKey = continuous [OK]
      Hint: GetKeyDown prints once; GetKey prints repeatedly while held [OK]
      Common Mistakes:
      • Thinking GetKeyDown prints repeatedly
      • Confusing GetKey with GetKeyDown
      • Assuming no output for held keys
      4. Identify the error in this Unity code snippet:
      void Update() {
        if (Input.GetKeyDown("space")) {
          Debug.Log("Jump!");
        }
      }
      medium
      A. The code is correct and will print "Jump!"
      B. GetKeyDown requires a KeyCode, not a string
      C. The method name should be GetKeyUp
      D. Debug.Log cannot be called inside Update

      Solution

      1. Step 1: Verify GetKeyDown parameter

        Input.GetKeyDown requires a KeyCode parameter, not a string like "space".
      2. Step 2: Confirm error

        Passing a string will cause a compile-time error; the correct usage is Input.GetKeyDown(KeyCode.Space).
      3. Final Answer:

        GetKeyDown requires a KeyCode, not a string -> Option B
      4. Quick Check:

        GetKeyDown("space") is invalid [OK]
      Hint: GetKeyDown requires KeyCode enum, not string [OK]
      Common Mistakes:
      • Believing GetKeyDown accepts strings
      • Thinking Debug.Log can't be in Update
      • Suggesting GetKeyUp instead
      5. You want to make a player jump only once when the spacebar is pressed, but also allow holding the left arrow key to move continuously. Which code snippet correctly implements this behavior?
      void Update() {
        // Your code here
      }
      hard
      A. if (Input.GetKeyDown(KeyCode.Space)) { Jump(); } if (Input.GetKey(KeyCode.LeftArrow)) { MoveLeft(); }
      B. if (Input.GetKey(KeyCode.Space)) { Jump(); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { MoveLeft(); }
      C. if (Input.GetKeyUp(KeyCode.Space)) { Jump(); } if (Input.GetKey(KeyCode.LeftArrow)) { MoveLeft(); }
      D. if (Input.GetKeyDown(KeyCode.Space)) { Jump(); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { MoveLeft(); }

      Solution

      1. Step 1: Use GetKeyDown for single jump press

        Jump should trigger once when spacebar is first pressed, so use Input.GetKeyDown(KeyCode.Space).
      2. Step 2: Use GetKey for continuous left movement

        Holding left arrow should move continuously, so use Input.GetKey(KeyCode.LeftArrow).
      3. Final Answer:

        if (Input.GetKeyDown(KeyCode.Space)) { Jump(); } if (Input.GetKey(KeyCode.LeftArrow)) { MoveLeft(); } -> Option A
      4. Quick Check:

        Jump = GetKeyDown, Move = GetKey [OK]
      Hint: Jump once with GetKeyDown; move continuously with GetKey [OK]
      Common Mistakes:
      • Using GetKey for jump causes repeated jumps
      • Using GetKeyDown for movement causes single-step moves
      • Using GetKeyUp incorrectly for jump
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard