I was trying to figure out how to focus the next
UI element from code for Godot 4.2.2, but I couldn't
find a suitable function on the Control
class. Looking at the Godot docs, it recommends creating
an input event to send back to the game:
var ev = InputEventAction.new()
# Set as ui_left, pressed.
= "ui_left"
ev.action = true
ev.pressed # Feedback.
parse_input_event(ev) Input.
I tried this by changing the action to
ui_focus_next
and it didn't work when
my UI was in a SubViewport
. I guessed
that this was because the input event was being sent
to the wrong SubViewport
and found this
solution:
var event := InputEventAction.new()
= "ui_focus_next"
event.action = true
event.pressed get_viewport().push_input(event)