How to switch focus neighbors that are left and right Trees

Godot Version

4.3

Question

Very simple question, I have two tree control nodes that act as lists for data, and are left/right of each other. I need to be able to switch focus between the two, but a problem arises as tree nodes seem to automatically consume ui_left and ui_right, meaning setting them as focus neighbors doesn’t do anything. I’ve tried to override this via catching the input with _input() and _unhandled_input() and processing it, but that doesn’t seem to affect it.

You should use Control._gui_input() and call Control.accept_event() to stop propagating the event.

Example:

extends Tree


func _ready() -> void:
	var root = create_item()
	root.set_text(0, "Root %s" % name)
	for i in 10:
		var item = create_item(root)
		item.set_text(0, "Item %s" % (i+1))


func _gui_input(event: InputEvent) -> void:
	if event.is_action_pressed(&"ui_left"):
		var neighbor = find_valid_focus_neighbor(SIDE_LEFT)
		if neighbor:
			neighbor.grab_focus()
			accept_event()
	if event.is_action_pressed(&"ui_right"):
		var neighbor = find_valid_focus_neighbor(SIDE_RIGHT)
		if neighbor:
			neighbor.grab_focus()
			accept_event()