Godot Version
4.2.1
Question
I’m messing around with keyboard inputs for the first time and I found a lot of examples use _unhandled_input()
(or _unhandled_key_input()
) for handling key events in GUIs. I have a simple GUI, and I added an input map for the esc key called “Exit Menu”. To test this, I added a few lines:
func _gui_input(event: InputEvent) -> void:
if event.is_action_pressed("Exit Menu"):
print("accepted")
This never seems to work. However, this does:
func _unhandled_key_input(event: InputEvent) -> void:
if event.is_action_pressed("Exit Menu"):
print("accepted")
I’m confused as to why, what I’ve read seems to suggest _gui_input()
should work for this.
As I wrote this, I realized the test I did on my top-level UI element (which is a CanvasLayer
) probably shouldn’t work with _gui_input()
. But I’ve also tested it on the Control
that’s right below it, as well as a menu that I bring up that’s a MarginContainer
. In all these cases, _unhandled_key_input()
seems to work, but _gui_input()
does not.
As I have a path forward with _unhandled_key_input()
this isn’t dire, but it’s more a matter of understanding what functions I’m supposed to be using.