_unhandled_input() vs _input(), what is the difference?

Godot Version

v4.3.stable.official [77dcf97d8]

Question

What is the difference between _unhandled_input() and _input()? In my projects, both do the same thing, but sometimes people will use only _unhandled_input() or only _input(). Why is this?

Hi,

From the Docs:

Input events are propagated through the SceneTree from the root node to all child nodes by calling Node._input. For UI elements specifically, it makes more sense to override the virtual method _gui_input, which filters out unrelated input events, such as by checking z-order, mouse_filter, focus, or if the event was inside of the control’s bounding box.

Call accept_event so no other node receives the event. Once you accept an input, it becomes handled so Node._unhandled_input will not process it.

So if you don’t handle the event in the _input() then it becomes unhandled so _unhandled_input() could be used to catch it.

I use _input() for checking for Key Presses and the like and _gui_input() for mouse and such like.

Ryn

The difference between the two is just the time when they are called.
So if the event is set to handled during _gui_input, _shortcut_input or _unhandled_key_input, then the event will not be available in _unhandled_input.

This allows for example Gui nodes to take precedence on input events, before you can act on them in _unhandled_input.

There are many situations, in which it doesn’t make any difference, if you use _input or _unhandled_input. But there are cases, where handling events in _unhandled_input will give you more consistent behavior than handling events in _input.

1 Like