0
0
Unityframework~10 mins

Keyboard input (GetKey, GetKeyDown) in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Keyboard input (GetKey, GetKeyDown)
Start Frame
Check GetKeyDown
Trigger GetKeyDown Action
Check GetKey
Trigger GetKey Action
End Frame
Back to Start Frame
Each frame, Unity checks if a key was just pressed (GetKeyDown) or is being held (GetKey), then triggers actions accordingly.
Execution Sample
Unity
void Update() {
  if (Input.GetKeyDown(KeyCode.Space)) {
    Debug.Log("Jump!");
  }
  if (Input.GetKey(KeyCode.Space)) {
    Debug.Log("Holding space");
  }
}
This code logs "Jump!" once when space is first pressed, and "Holding space" every frame while space is held.
Execution Table
FrameSpace Key StateInput.GetKeyDown(Space)Input.GetKey(Space)Action TakenOutput
1Not pressedFalseFalseNone
2Pressed this frameTrueTrueGetKeyDown and GetKey triggeredJump! Holding space
3Held downFalseTrueGetKey triggeredHolding space
4Held downFalseTrueGetKey triggeredHolding space
5ReleasedFalseFalseNone
💡 Space key released at frame 5, so no input detected.
Variable Tracker
VariableStartAfter Frame 1After Frame 2After Frame 3After Frame 4After Frame 5
Space Key StateNot pressedNot pressedPressed this frameHeld downHeld downReleased
Input.GetKeyDown(Space)FalseFalseTrueFalseFalseFalse
Input.GetKey(Space)FalseFalseTrueTrueTrueFalse
Key Moments - 2 Insights
Why does GetKeyDown only trigger once even if the key is held?
GetKeyDown returns true only on the exact frame the key is first pressed, as shown in frame 2 of the execution_table. Subsequent frames with the key held down return false for GetKeyDown.
Why does GetKey return true on multiple frames?
GetKey returns true every frame the key is held down, as seen in frames 2, 3, and 4 in the execution_table, allowing continuous detection while holding.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is Input.GetKeyDown(Space) at frame 3?
ATrue
BNull
CFalse
DDepends on previous frame
💡 Hint
Check the 'Input.GetKeyDown(Space)' column at frame 3 in the execution_table.
At which frame does the action 'Jump!' get logged?
AFrame 2
BFrame 1
CFrame 3
DFrame 5
💡 Hint
Look at the 'Action Taken' and 'Output' columns in the execution_table.
If the space key is held for 4 frames, how many times does 'Holding space' get logged?
A1
B4
C3
D2
💡 Hint
Check the 'Output' column for frames where Input.GetKey(Space) is true.
Concept Snapshot
Unity keyboard input:
- GetKeyDown detects key pressed this frame (once)
- GetKey detects key held down (every frame)
- Use in Update() for frame-based input
- GetKeyDown triggers action once per press
- GetKey triggers action continuously while held
Full Transcript
In Unity, keyboard input is checked every frame inside the Update() method. The function Input.GetKeyDown returns true only on the exact frame when a key is first pressed. This triggers actions that should happen once, like jumping. Input.GetKey returns true every frame while the key is held down, useful for continuous actions like running or charging. The execution table shows how these functions behave over several frames when the space key is pressed, held, and released. Beginners often confuse GetKeyDown with GetKey because GetKeyDown triggers only once per press, while GetKey triggers repeatedly while holding. Understanding this difference helps control game actions precisely.