How to add clicked letters on a LineEdit node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Syl


Got letters and their area2d/CollisionShape to be clicked, and i want them to be written in my LineEdit node. How could i do that please?

:bust_in_silhouette: Reply From: jgodfrey

Assuming this is some sort of GUI-based “keyboard”, I might do it by making the keys out of TextureButton nodes - one per key.

Here’s an outline:

  • Add a LineEdit node to the scene to display the clicked letters
  • Add a TextureButton for each letter with an appropriate texture for the letter on each button.
  • Name each node after the letter it represents (A - Z for example)
  • Put all of the TextureButton nodes in a common group (letter below)

Add the following script to the scene parent…

extends CanvasLayer

func _ready():
	for button in get_tree().get_nodes_in_group("letter"):
		button.connect("pressed", self, "_on_Button_pressed", [])

func _on_Button_pressed(name):
	$LineEdit.text += name

That will loop through each button in the letter group and connect all of their pressed signals to a common function (_on_Button_pressed) and pass in the name of the pressed button.

In the _on_Button_pressed function, we take the name of the pressed button (again, that should be the letter the button represents) and add it to the text of the LineEdit control.

The result is that the letter represented by each button press is added to the LineEdit control.

You can certainly do something similar with Area2D / CollisionShape2D, but this is probably easier…

Here again, you made it! :slight_smile: Cheeeers!

Erh, there’s a way to hide the LineEdit button, keeping only its text? And a lil issue is that clicking the letters doesn’t make the text_entered with enter pressed… Enter just add the last letter selected.

Syl | 2020-02-17 12:53

Not sure what you mean by the “hide the LineEdit button”. What button is that? A LineEdit control is just a text-control, so no button as far as I know…

Also, not sure I follow the “clicking the letters doesn’t make the text_entered with enter pressed”.

The way the sample code works, each time a letter button is clicked, it’s character is added to the text in the LineEdit control.

If you want Enter to add a letter, how would you indicate which letter? I guess you’d have to point at it with the mouse first. But, if you have to do that anyway, it sounds more convenient to just click the mouse rather than press Enter.

Maybe I don’t understand…

jgodfrey | 2020-02-17 15:37

Sry, by hiding i mean giving it an ‘alpha’ texture so that only the text gets displayed. And for the ‘enter’, it’s a way to close the text, working if the letters are typed in the box, and not clicked.

Syl | 2020-02-17 15:56

If you only want the text itself to show, you probably want to use a Label instead of an LineEdit control. If you add a Label, you can insert text exactly the same way the above-posted script does for the LineEdit control. So…

$Label.text += name

jgodfrey | 2020-02-17 16:08

And, for hiding the control based on some keypress, you can simply change the visible property of the control when you get the appropriate input. Something like this:

func _process(delta):
	if Input.is_action_just_pressed("ui_right"):
		$Label.visible = !$Label.visible

Note, that will toggle the Label visibility (off, on, off, …) each time the right-arrow is pressed. Just adjust as necessary for your use-case…

jgodfrey | 2020-02-17 16:15

The good with LineEdit was that it had a signal ‘text_entered’ emitted with enter pressed, wiich triggered my AnimationPlayer for a change of scene. And, in its signal (), had ‘new_text’ that was the player name, like this:

extends AnimationPlayer

func _on_LineEdit_text_entered(new_text):
	GlobalP.player_name = new_text

Trouble is, with that method of clicking the letters, ‘enter’ is no more pressed in the LineEdit…

Syl | 2020-02-17 21:03