+2 votes

In Godot 3.0, I am trying to implement a PopupMenu which allows scrolling through its buttons using the keyboard.

My scene structure is

PopupMenu
    > ColorRect
    > Button1
    > Button2

Focus mode on the PopupMenu is set to All.

I have a very simple script on the PopupMenu which attempts to assign focus to Button2:

extends PopupMenu

func _ready():
    get_node("Button2").grab_focus()

When I press escape, I call show() on the popup and it is displayed, but neither of my buttons are focused on. So my question is: How can I set the focus on buttons in a PopupMenu and scroll through them with keyboard?

I noticed that if the buttons are children of a VBoxContainer as the root node, it works as desired. But not if the VBoxContainer is a child of the PopupMenu.

in Engine by (1,602 points)

1 Answer

+1 vote
Best answer

First of all, I'm pretty sure that to open a Popup, you're supposed to be using methods like popup(), not show(). Then again I might be wrong on that.

Either way: _ready() is called when the node gets added to the scene, not when it is shown. You'll want to hook the NOTIFICATION_POST_POPUP notification (using the _notification virtual) to run code after the popup is opened. (If I was wrong and show() is the correct way to open it, you'll probably want the visibility_changed signal of Control)

by (112 points)
selected by

Thanks for the answer. I bet you're right about using popup() instead of show(). How do I use the _notification virtual? My PopupMenu doesn't have that listed in its signals.

_notification is a virtual function you can override just like _ready. It's defined on the base Object

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.