Museum of Lost Lights · Lesson 06 of 50 · Beginner · About 35 min
Two visitors can choose different controls for the same job. One is using a touch screen and the other is sitting at the keyboard.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/05 when the lesson builds on prior work, or copy the checkpoints/06 folder to try this lesson on its own. Read the file names in the steps before editing.

Build it step by step
Language and tools in this lesson InputEvent, is, and, not, ==, keyboard, tooltip, handlers.
-
Open the dock and find its two paths
Import and run Lesson 06. Click or tap Recharge and record the number. Press R once and record it again. Find recharge() in the script.
Why it works Both paths call the same function. Sharing behaviour keeps the button and the key from falling out of step.
-
Follow the button’s path
Find the connection to recharge and compare it with Lesson 05. Use the Tab key if needed to move keyboard focus to the button, then use Space to activate it.
recharge_button.pressed.connect(recharge)Why it works A focused Godot Button can receive keyboard activation. grab_focus moves keyboard attention to the button when the scene first starts.
-
Follow the keyboard’s path
Read _unhandled_key_input and look for the argument named event. Pause at the is check and decide what kind of message the next block expects.
func _unhandled_key_input(event: InputEvent) -> void: if event is InputEventKey:Why it works Godot calls this function for keyboard input the interface has not handled earlier. is checks the type of a value before the code uses the properties of that type.
-
Rule out repeats and unrelated keys
Read the three parts of the inner if. Try a quick R press, a different letter, and holding R for a moment. Compare your results.
if event.pressed and not event.echo and event.keycode == KEY_R:Why it works and needs all parts to be true. pressed skips the release event. not flips echo, so operating-system repeats are skipped. == compares keycode with KEY_R to match the right key.
-
Read back the shared state
Follow charge += 1 into refresh_readout. Read the %d in the quoted words, then point to the percent sign outside the quoted words.
readout.text = "Charge: %d · Press R or use the button" % chargeWhy it works %d marks a place for a whole number. The outside % formats the String by putting the current charge in that place. The set_input_as_handled line tells Godot this game has dealt with the key press.
Watch the values change
-
Tap Recharge The button’s pressed signal calls recharge
What you see. Charge becomes 1. -
Press R once The event is a matching pressed key and not a repeat
What you see. Charge becomes 2. -
Release R or press K The press filter or key comparison fails
What you see. Charge stays at 2. -
Hold R down again The initial press passes; the repeated events are echoes
What you see. Charge becomes 3 only once.
Predict before you run If every key now recharges the lamp, which comparison should you look for?
Reveal the answer
event.keycode == KEY_R. It selects R. Without it the other checks could accept any non-repeated pressed key.
Complete checkpoint
Starting from 0, a tap and one press of R produce charge 2. Holding R increases the charge once for that physical press.
main.gd
extends Control
var charge: int = 0
var readout: Label
func _ready() -> void:
make_heading()
readout = Label.new()
readout.position = Vector2(54, 114)
readout.add_theme_font_size_override("font_size", 27)
add_child(readout)
var recharge_button := Button.new()
recharge_button.text = "Recharge (+1)"
recharge_button.position = Vector2(54, 182)
recharge_button.custom_minimum_size = Vector2(250, 64)
recharge_button.tooltip_text = "Tap here or press the R key."
recharge_button.add_theme_font_size_override("font_size", 24)
recharge_button.pressed.connect(recharge)
add_child(recharge_button)
recharge_button.grab_focus()
refresh_readout()
func _unhandled_key_input(event: InputEvent) -> void:
if event is InputEventKey:
if event.pressed and not event.echo and event.keycode == KEY_R:
recharge()
get_viewport().set_input_as_handled()
func recharge() -> void:
charge += 1
refresh_readout()
func refresh_readout() -> void:
readout.text = "Charge: %d · Press R or use the button" % charge
func make_heading() -> void:
var heading := Label.new()
heading.text = "Night shift · Recharge dock"
heading.position = Vector2(52, 36)
heading.add_theme_font_size_override("font_size", 38)
heading.add_theme_color_override("font_color", Color("#ffe1a8"))
add_child(heading)
GDScript line by line
Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.
Read main.gd line by line
-
extends ControlThe scene root behaves like a Control, the kind of Godot node used for a game’s text, controls and other interface pieces. -
var charge: int = 0Begin with an empty rechargeable lantern. Button and keyboard inputs both add to this same value. -
var readout: LabelReserve a name for a Label node. The setup function puts the real Label there before the game first displays it. -
func _ready() -> void:Godot automatically calls this setup function after bringing this node and its children into the scene. The code below it prepares the first frame. -
make_heading()Build the title before the room makes its other controls. -
readout = Label.new()Create the Label now. Its stored reference lets later signal handlers change the same sign. -
readout.position = Vector2(54, 114)Use two numbers to give this control its position, measured right and down from the root. -
readout.add_theme_font_size_override("font_size", 27)Set a larger or smaller font here without changing the typography on the other controls. -
add_child(readout)Put the stored Label on the visible scene. Changes to its text now show on screen. -
var recharge_button := Button.new()Make the button that adds a unit of charge. -
recharge_button.text = "Recharge (+1)"Updates recharge_button.text, which controls the words a player sees. The expression uses the text inside the quotes. -
recharge_button.position = Vector2(54, 182)Use two numbers to give this control its position, measured right and down from the root. -
recharge_button.custom_minimum_size = Vector2(250, 64)Set a large target so fingers and mouse pointers have room to activate it. -
recharge_button.tooltip_text = "Tap here or press the R key."Set a short help message that Godot may show when the pointer hovers over the control. -
recharge_button.add_theme_font_size_override("font_size", 24)Set a larger or smaller font here without changing the typography on the other controls. -
recharge_button.pressed.connect(recharge)Have taps and ordinary button activation use the recharge function. -
add_child(recharge_button)Adds the new node to the scene tree so that Godot can run and draw it. -
recharge_button.grab_focus()Start keyboard focus on the button so Space can activate it. R has its own route in the input callback below. -
refresh_readout()Ask the display function to read the current game state and put it into words. -
func _unhandled_key_input(event: InputEvent) -> void:Godot passes keyboard events here if the interface has not handled them first. -
if event is InputEventKey:Confirm this is an InputEventKey before reading its key-specific fields. -
if event.pressed and not event.echo and event.keycode == KEY_R:Require a press, exclude the repeated events from holding down a key and match the R key code. All three conditions must pass. -
recharge()Use exactly the function the button uses. The two input paths now make the same state change. -
get_viewport().set_input_as_handled()Mark this event as taken care of so Godot’s remaining input stages can skip it. -
func recharge() -> void:Keep the state change and readout update together in one function for both input paths. -
charge += 1Add one unit to the previous stored battery charge. -
refresh_readout()Display the just-updated charge and remind the player which controls they can use. -
func refresh_readout() -> void:Keep the code that changes the on-screen words together so both setup and player actions can call it. -
readout.text = "Charge: %d · Press R or use the button" % chargeInsert the int into %d. The percent sign outside the quotes performs that substitution. -
func make_heading() -> void:Group the instructions that make the panel title. The room calls this once at startup. -
var heading := Label.new()Create a Label node and keep a reference under the name heading so the next lines all customise the same sign. -
heading.text = "Night shift · Recharge dock"Show the name of this museum task above its controls. -
heading.position = Vector2(52, 36)Put the title 52 pixels to the right and 36 pixels down from the root’s upper-left corner. -
heading.add_theme_font_size_override("font_size", 38)Set this heading’s font size to 38. The other labels keep their own sizes. -
heading.add_theme_color_override("font_color", Color("#ffe1a8"))Give only this label a warm gold text colour. The six characters after # describe red, green and blue light. -
add_child(heading)Connect the title to the scene tree so Godot can display it.
Find and fix a bug
If R changes nothing, click inside the game window so the editor is not receiving your key presses. Check the nested indentation and the original values R, pressed and echo.
Try a change
Ages 10–12: Test which keys work and write a one-sentence player hint. Ages 13–16: Add a second key that calls the same recharge function using a parenthesised or check, and test it against the original key and a key that should do nothing.