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.
ev.action = "ui_left"
ev.pressed = true
# Feedback.
Input.parse_input_event(ev)

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()
event.action = "ui_focus_next"
event.pressed = true
get_viewport().push_input(event)