Skip to main content
Header image by NoName_13 © NoName_13

How to Get the Node With the Focus in Godot

This tutorial will teach you how to get the node with the focus in Godot and how to listen to a fired signal when the focus changes.

This tutorial is using Godot 4.

Using the Viewport #

Godot has a built-in method to get the node with the focus. The method is called gui_get_focus_owner(), and you can find this method in the class Viewport.

Here is an example of how to use this method:

var node_focused = get_viewport().gui_get_focus_owner()

It will return the node that has the focus.

Using a signal #

Still inside the Viewport class, you can find a signal called gui_focus_changed. This signal is fired when the focus changes.

You can use it like this:

class_name MyAwesomeClass
extends Node

func _ready() -> void:
	get_viewport().gui_focus_changed.connect(_on_gui_focus_changed)

func _on_gui_focus_changed(node: Node) -> void:
	# Do stuff with the node.
	print(node)

The method _on_gui_focus_changed will be called when the focus changes, giving the new focused node as the first parameter.

Tags

See Also

No results...