0 votes

I have programmed a menu, using VBoxLayouts and Buttons inside it. How can I tell godot to keep scrolling through the focus of the buttons, while I keep ui_down and ui_up pressed. Right now, I have to re-press the buttons every time I want to focus the next Button.
enter image description here
Until now I have not needed to script anything, and it "just works". I would love to not have to reimplement the whole Input-handling for focus switching by myself to solve it (but I will, if I have to). Does anyone know of a way, where I don't have to?

Thanks in advance!

asked Mar 27 in Engine by julkip (38 points)
edited Mar 27 by julkip

1 Answer

0 votes
Best answer

Does anyone know of a way, where I don't have to?

There is no way! Take a look at Godot's source code. It explicitly checks for just_pressed there, meaning it will only fire once (on the first frame the button is pressed) and not again (when the button is hold down).

To make it work nonetheless, add this script to each of your buttons:

extends Button

func _gui_input(event):
    if has_focus() and event is InputEventKey:
        if event.is_action_pressed("ui_up", true):
            accept_event() # prevent the normal focus-stuff from happening
            get_node(focus_neighbour_top).grab_focus()
        elif event.is_action_pressed("ui_down", true):
            accept_event() # prevent the normal focus-stuff from happening
            get_node(focus_neighbour_bottom).grab_focus()

Make sure the focus_neighbour_bottom|top-properties are set correctly on each button. I just did it for the "uiup"-actions and "uidown"-actions, but the principle should be clear and expanding this on your own shouldn't be too hard.

answered Mar 28 by njamster (8,874 points)
selected Mar 28 by julkip

Thank you, this works great!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.