Handle Input in Godot 4
Input events are a crucial part of game development, as they allow you to create responsive and interactive games. So by knowing how to handle input in Godot 4 and exploring the various options available, you can create games that are engaging and enjoyable.
Dear reader, please consider supporting my game ❤
Handle input in Godot 4 with input mapping
Input mapping is the process of defining the actions or keys that the player can use to interact with the game in the Input Map. You find it in Project Settings > Input Map, or you can create it in code. In the project settings, you can create input actions and map them to specific keys, buttons, or axes on the keyboard, mouse, game pad, or other input devices.
Input processing
The user triggers input events by pressing a key on the keyboard or clicking a button on a game pad. The game can handle these input events to perform certain actions or trigger specific behaviors. How you can handle these events depends on whether you have set up an input map.
So here is how to handle input in the _process
function for querying the state of an input with and without using the input map:
func _process(delta: float) -> void:
# Mouse with Input Map
if Input.is_action_just_pressed("MOUSE_RIGHT"):
print("Mouse right pressed")
elif Input.is_action_pressed("MOUSE_RIGHT"):
print("Mouse right down")
elif Input.is_action_just_released("MOUSE_RIGHT"):
print("Mouse right released")
# Keyboard with Input Map
if Input.is_action_just_pressed("JUMP"):
print("Jump pressed")
elif Input.is_action_pressed("JUMP"):
print("Jump down")
elif Input.is_action_just_released("JUMP"):
print("Jump released")
# Mouse without Input Map
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
print("Mouse left down")
# Keyboard without Input Map
if Input.is_physical_key_pressed(KEY_W):
print("W key down")
And here it is in the _input
function, for handling the input whenever the input event occurs:
func _input(event: InputEvent) -> void:
# Mouse with Input Map
if event.is_action_pressed("MOUSE_RIGHT"):
print("Mouse right pressed")
elif event.is_action_released("MOUSE_RIGHT"):
print("Mouse right released")
# Keyboard with Input Map
if event.is_action_pressed("JUMP"):
print("Jump pressed")
elif event.is_action("JUMP"):
print("Jump down")
elif event.is_action_released("JUMP"):
print("Jump released")
# Mouse without Input Map
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT && event.pressed:
print("Mouse left pressed")
elif event.button_index == MOUSE_BUTTON_LEFT && !event.pressed:
print("Mouse left released")
# Keyboard without Input Map
elif event is InputEventKey:
if event.keycode == KEY_W && event.pressed && !event.echo:
print("W key pressed")
elif event.keycode == KEY_W && event.pressed:
print("W key down")
elif event.keycode == KEY_W && !event.pressed:
print("W key released")
Unhandled input
The _unhandled_input
and _unhandled_key_input
functions are called whenever an input event occurs, but no node in the scene tree handles that input event. The _unhandled_key_input
performs better than the _unhandled_input
, because unrelated events are filtered out.
func _unhandled_input(event: InputEvent) -> void:
print("Unhandled input event: ", event)
func _unhandled_key_input(event: InputEvent) -> void:
print("Unhandled key input event: ", event)
So, that is how to handle input in Godot 4
Handling input is essential in game development, and Godot 4 offers a robust input system for immersive gaming. The input map and events system enable developers to manage various input sources such as keyboard, mouse, etc. This system makes it easy for developers to handle input and create engaging gaming experiences.
You find more information in the official documentation. I hope you like this little overview. Consider subscribing to the mailing list, to get notified about future posts.