How to Handle Input in Godot

How to Handle Input in Godot



There are not many things more essential to a video game, than the user input. In fact, it is what differentiates video games from any other medium. So obviously, input plays a major role in game development. Now in this post, I want to talk about the different ways, in which you can handle input in Godot. Also, we want to take a look at the different input methods too. So, essentially there are two main ways to handle input: Either you will handle the input inside the _process(delta) method, or you handle the input event inside the _input(event) method. There are some differences when it comes to the two approaches. But we will touch on them later on.

How to Handle Input in Godot? With Input Map!

Generally, I would recommend to always work with the Input Map in Godot. If you don’t know what it is and how to work with it, please check out this tutorial. An Input Map is useful for a variety of reasons. For example, you may want different buttons to perform the same action. This is no problem with the Input Map. Or if you want your players to customize their controls, you also should use the Input Map (check the aforementioned tutorial for that too)

There is not really an argument for not using the Input Map, as far as I am concerned. But for the sake of completion, I will also show, how you can do it with the key directly.

Polling Input

So, the first way to handle input in Godot is to poll an event inside the _process(delta) method. For example, you can print something, as long as a button is pressed (let’s say the shift-key). I will show you the code for the key directly, as well as via the Input Map. For the Input Map to work, you first want to name a new input event, in our case we call it “STING”. Again, if you are not sure how to do that, please have a look here first. When that is done, you can refer to it like this:

func _process(delta):
	# Key Directly
	if Input.is_key_pressed(KEY_SHIFT):
		print("Ouch...")
		
	# Input Map ("STING" has to be defined in the Input Map)
	if Input.is_action_pressed("STING"):
		print("Ouch!!!")

There are different possibilities when it comes to how exactly you want to handle the inputs. For example, you can use the following code to refer to the input only once in the first frame and the last frame respectively (do not forget to set up the aforementioned Input Map).

func _process(delta):
	if Input.is_action_just_pressed("STING"):
		print("ka...")
		
	if Input.is_action_just_released("STING"):
		print("...tsching!")

Check out the official documentation for more information on the different methods.

Mouse Input

There is also the possibility to register mouse inputs. You can either assign a mouse input to actions in your Input Map, or you can use this code to handle input from your mouse directly.

func _process(delta):
	if Input.is_mouse_button_pressed(BUTTON_LEFT):
		print("Hi!")

Again, when it comes to all the different input devices (for example mouse, gamepad, etc…), the easiest way is to use the Input Map.

Handle InputEvents

The other possibility is to handle every input event, that gets registered. You can do that in the _input(event) method. Say, for example, you want to print “jump” whenever you press the space bar. You can do that like this (do not forget to create the “JUMP” action in the Input Map):

func _input(event):
	if event is InputEventKey: #You have to make sure, that a key got pressed
		if event.scancode == KEY_SPACE:
			print("jump...")
		
	if event.is_action("JUMP"):
		print("jump!!!")

Here too you have different possibilities for how exactly the InputEvent should be handled. For example, you can use event.is_echo() to make sure, the event is only once registered. Further, event.is_pressed() can be used for only registering the first time the button is pressed. The same is true if you use event.is_action_pressed() or event.is_action_released() respectively. Try out the following code:

func _input(event):
	if event.is_action("JUMP") && !event.is_echo() && event.is_pressed():
		print("jump...")
		
	if event.is_action_released("JUMP"):
		print("Up and down!")

It may also be useful to check if the InputEvent is a certain kind of type. For example, you may want to check if the input event was caused by the mouse:

func _input(event):
	if event is InputEventMouseMotion:
		print("Leave the mouse alone!")

How to Handle Input in Godot – Unhandled InputEvents

Also, if you want, you can catch all unhandled input events. You can use the following code to do that:

func _unhandled_input(event):
	print("There was an unhandled event!!!")

So, I hope this was useful to you. If it was, let me know in the comments below. Also, check out other Godot topics here on my website. Maybe you also want to check out the post I made about Godot Themes. Or this one about inheritance in Godot.


To learn more about the Godot Engine I can recommend Godot Engine Game Development Projects by Chris Bradfield. If you are interested, you can follow the affiliate link to also support this website.


How to Handle Input in Godot
How to Handle Input in Godot

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

Post comment