How to always keep the caret on the right side in Godot 4 LineEdit?

Godot Version

V4.2.2

Question

Hey Guys,

I’m working on a project in Godot 4, and I’m trying to ensure that the caret (cursor) in a LineEdit node always remains on the right side of the text and never on the left side. For instance, when typing, I want the caret to be positioned like this: “Book|” rather than like this: “|Book”.

any ideas how might I achieve this? thanks!

set the caret_column to text.length(). Doesn’t look like there is a signal for the caret moving so you may just have to set it every so often while focused.

I’d say a callback connected to the LineEdit’s gui_input signal would be a good place:

func _on_LineEdit_gui_input(event: InputEvent) -> void:
	$LineEdit.caret_column = $LineEdit.text.length()

That being said, you will still see the caret moving before it’s reset to the right side. If that bothers you, you can block all relevant inputs yourself – quick and dirty example:

func _on_LineEdit_gui_input(event: InputEvent) -> void:
	# blocks *all* mouse inputs, which might be more than you want
	if event is InputEventMouseButton:
		get_viewport().set_input_as_handled()
	# blocks all keyboard input actions related to cursor movement
	elif event.is_action("ui_text_caret_word_left") or \
		event.is_action("ui_text_caret_left") or \
		event.is_action("ui_text_caret_up") or \
		event.is_action("ui_text_caret_line_start") or \
		event.is_action("ui_text_caret_page_up"):
			get_viewport().set_input_as_handled()
1 Like