Problem handling text input in Godot 4.x with LineEdit

I am developing an application in Godot 4.x where I need to enter text into a LineEdit and process it when Enter or Tab is pressed. I’ve tried hooking up the text_submitted signal but it doesn’t seem to be working as expected. Here is my current code:

"
extends Node2D

@onready var input_field = $InputField
var word_list =

func _ready():
input_field.connect(“text_submitted”, self, “_on_text_submitted”)

func _on_text_submitted(new_text):
var text = new_text.strip_edges()
if Input.is_key_pressed(KEY_ENTER):
word_list.append(text)
_update_word_display()
input_field.clear()

func _update_word_display():
# Code to update the display of words

func _on_button_pressed():
# code to manage button
"

I use Godot 4.x and have verified that my scene has a LineEdit called InputField.
There are no error messages in the Godot console, but pressing Enter does not add the word to word_list.
I’ve tried adjusting the logic inside _on_text_submitted to handle KEY_TAB and KEY_ENTER as per the Godot documentation, but it still doesn’t work correctly.
I appreciate any suggestions or corrections you can offer. Thank you!

1 Like

I’m seeing the same issue in my project. The call back doesn’t trigger when the Enter key is pressed. I’m doing it like this:

@export var name_input: LineEdit
func register_score(amount: int) -> void:
	name_input.visible = true
	name_input.grab_focus()
	name_input.text_submitted.connect(
		func():
			name_input.visible = false
			update_leader_board(name_input.text, amount),
		CONNECT_ONE_SHOT
	)

You need to wrap posted code in code tags to keep the formatting (tabs) otherwise its guesswork on our part what your code looks like.
I’m guessing it looks like this:

func _on_text_submitted(new_text):
   var text = new_text.strip_edges()
   if Input.is_key_pressed(KEY_ENTER):
      word_list.append(text)
      _update_word_display()
      input_field.clear()

The if statement is not working here. As long as your text_submitted signal connection is valid then there is no need to test for key_pressed since that is how you got to this function.

func _on_text_submitted(new_text):
   var text = new_text.strip_edges()
   word_list.append(text)
   _update_word_display()
   input_field.clear()

In your case it is likely that the function signature doesn’t match the signal signature.
The text_submitted signal requires a string parameter:

name_input.text_submitted.connect(
		func(new_text:String):
			name_input.visible = false
			update_leader_board(new_text, amount),
		CONNECT_ONE_SHOT
	)

PS: That parameter is the text in the lineedit.

That was totally my issue, it was the signature mismatch. There WAS an error in the debugger, but I didn’t see it because usually those kinds of type errors crash the game or pull focus, and when that didn’t happen, I forgot to actually check the debugger…lesson learned, thanks!