toggle button focus

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lalel345

I want to switch the focus of a button when I press a key so that the focus entered and exited
i made this and it does not work :

func _physics_process(delta):
	if Input.is_action_just_pressed("ui_accept"):
		$Button.grab_focus() = !$Button.grab_focus()
:bust_in_silhouette: Reply From: jgodfrey

grab_focus() is just a function that sets the focus on the control it’s called on. So, you can’t set it to a boolean value as you’re attempting to do.

To set the focus on $Button, you simply need to call $Button.grab_focus(). If you need to first see if $Button already has focus, you can call $Button.has_focus().

:bust_in_silhouette: Reply From: haha-lolcat1

try this:

func _physics_process(delta):
    if Input.is_action_just_pressed("ui_accept"):
        if $Button.has_focus():
			$Button.release_focus()
		else:
			$Button.grab_focus()