I can't get the focus switch with tab key working

Godot Version

4.4.1 `

Question

Hi everyone,

Stupid question but I don’t manage to find the solution : I can’t get the focus switch to work. I have a few TextEdits and when one of them has the focus and I hit Tab (or shift-Tab) nothing happens. I’ve tried to put proper NodePaths in focus_next and focus_previous, to no avail.
I’m stuck.

What’s the obvious thing I’ve missed?

There’s no such thing!

From my testing, the TextEdit will absorb that input and insert a tab at the cursor(s).

You could add a script to the TextEdit to manually change the focused control when the user presses Tab, like below:

extends TextEdit


func _gui_input(event: InputEvent) -> void:
	var event_key := event as InputEventKey
	if event_key and event_key.pressed and event_key.keycode == KEY_TAB:
		if event_key.shift_pressed:
			find_prev_valid_focus().grab_focus.call_deferred()
		else:
			find_next_valid_focus().grab_focus.call_deferred()

Note that this example will still type the tab before focusing another control.

1 Like

It’s weird because there’s no tab written in my TextEdit. Maybe is it because it’s not shown?

I don’t know why it wouldn’t. You could enable the TextEdit’s draw_tabs property and see if they’re visible then.

I realize I made a mistake, it’s not a TextEdit but a LineEdit. I don’t know if it changes anything. But I get the Tabs drawn inside TextEdits but not inside LineEdits.

I created several LineEdits in a VBoxContainer and was able to tab through them without any issues.
Would you like to share some of your code?

I got it, and it was a stupid mistake from my side. I was wrongly linking my Nodes, mixing focus_next and focus_previous.
But I also needed your code, so thank you, it’s fixed!

It’s a bit weird that by default Line/TextEdits absorb Tab without scrolling through Controls. It’d have been neat to just check a box to get this behavior without adding code.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.