0
0
Unityframework~10 mins

Keyboard input (GetKey, GetKeyDown) in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the space key is currently held down.

Unity
if (Input.[1](KeyCode.Space)) {
    Debug.Log("Space key is held down.");
}
Drag options to blanks, or click blank then click option'
AGetKeyUp
BGetKeyDown
CGetKey
DIsKeyPressed
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetKeyDown instead of GetKey for continuous key press detection.
2fill in blank
medium

Complete the code to detect when the player just pressed the 'W' key this frame.

Unity
if (Input.[1](KeyCode.W)) {
    Debug.Log("W key was just pressed.");
}
Drag options to blanks, or click blank then click option'
AGetKey
BGetKeyDown
CGetKeyUp
DIsKeyPressed
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetKey instead of GetKeyDown for single press detection.
3fill in blank
hard

Fix the error in the code to detect when the 'Escape' key is released.

Unity
if (Input.[1](KeyCode.Escape)) {
    Debug.Log("Escape key was released.");
}
Drag options to blanks, or click blank then click option'
AGetKeyUp
BGetKeyDown
CIsKeyReleased
DGetKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetKeyDown or GetKey instead of GetKeyUp for key release detection.
4fill in blank
hard

Fill both blanks to create a dictionary that maps keys to whether they were just pressed this frame.

Unity
var keysPressed = new Dictionary<KeyCode, bool> {
    { KeyCode.A, Input.[1](KeyCode.A) },
    { KeyCode.D, Input.[2](KeyCode.D) }
};
Drag options to blanks, or click blank then click option'
AGetKeyDown
BGetKey
CGetKeyUp
DIsKeyPressed
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetKey or GetKeyUp which do not detect initial press correctly.
5fill in blank
hard

Fill all three blanks to create a dictionary that maps keys to whether they are held down or just released.

Unity
var keyStates = new Dictionary<KeyCode, bool> {
    { KeyCode.LeftArrow, Input.[1](KeyCode.LeftArrow) },
    { KeyCode.RightArrow, Input.[2](KeyCode.RightArrow) },
    { KeyCode.UpArrow, Input.[3](KeyCode.UpArrow) }
};
Drag options to blanks, or click blank then click option'
AGetKeyDown
BGetKey
CGetKeyUp
DIsKeyPressed
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetKeyDown instead of GetKey for held keys, or vice versa.