0
0
Unityframework~20 mins

Keyboard input (GetKey, GetKeyDown) in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Keyboard Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when pressing the spacebar once?
Consider the following Unity C# script attached to a GameObject. What will be printed to the console when the spacebar is pressed once and held for 3 frames?
Unity
void Update() {
    if (Input.GetKeyDown(KeyCode.Space)) {
        Debug.Log("Pressed Down");
    }
    if (Input.GetKey(KeyCode.Space)) {
        Debug.Log("Held Down");
    }
}
APressed Down
BPressed Down\nHeld Down\nHeld Down\nHeld Down
CHeld Down\nHeld Down\nHeld Down\nHeld Down
DPressed Down\nPressed Down\nPressed Down\nPressed Down
Attempts:
2 left
💡 Hint
GetKeyDown triggers only once when the key is first pressed. GetKey triggers every frame while the key is held.
Predict Output
intermediate
2:00remaining
What happens if you replace GetKeyDown with GetKey in this code?
Given this code snippet, what will be the output if the player presses and holds the "A" key for 2 frames? void Update() { if (Input.GetKeyDown(KeyCode.A)) { Debug.Log("Jump"); } }
Unity
void Update() {
    if (Input.GetKey(KeyCode.A)) {
        Debug.Log("Jump");
    }
}
ANo output
BJump
CJump\nJump
DJump\nJump\nJump
Attempts:
2 left
💡 Hint
GetKey returns true every frame the key is held down.
🔧 Debug
advanced
2:00remaining
Why does this code never print "Action triggered"?
Examine the code below. The developer expects "Action triggered" to print once when the player releases the "F" key, but it never does. What is the cause?
Unity
void Update() {
    if (Input.GetKey(KeyCode.F)) {
        if (Input.GetKeyUp(KeyCode.F)) {
            Debug.Log("Action triggered");
        }
    }
}
AGetKeyUp inside GetKey is redundant and never true, so the inner condition never triggers.
BGetKeyUp must be outside the GetKey condition to work properly.
CThe code has a syntax error due to nested if statements.
DThe key code KeyCode.F is incorrect and should be KeyCode.FKey.
Attempts:
2 left
💡 Hint
GetKeyUp is true only on the frame the key is released, but inside GetKey it is never true.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in Unity C#?
Which of the following code snippets will cause a syntax error when used inside the Update() method?
Aif (Input.GetKeyDown(KeyCode.Space)) Debug.Log("Jump");
Bif (Input.GetKeyDown(KeyCode.LeftArrow)) { Debug.Log("Turn left"); }
Cif (Input.GetKey(KeyCode.S)) { Debug.Log("Move backward"); }
Dif Input.GetKey(KeyCode.W) { Debug.Log("Move forward"); }
Attempts:
2 left
💡 Hint
Check the syntax of the if statement carefully.
🚀 Application
expert
2:00remaining
How many times will "Fire!" print if the player taps the "F" key quickly 3 times?
Given the code below, how many times will "Fire!" be printed if the player taps the "F" key quickly three separate times (press and release each time)? void Update() { if (Input.GetKeyDown(KeyCode.F)) { Debug.Log("Fire!"); } }
A3
B1
C0
DDepends on frame rate
Attempts:
2 left
💡 Hint
GetKeyDown triggers once per key press, regardless of how fast the taps are.