How to deal with text input?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PerduGames
:warning: Old Version Published before Godot 3 was released.

I’m trying to deal with text input from the keyboard but I do not know how to pick up the keys and update the label. I’m doing this:

func _ready():
	set_process_input(true)

func _input(event):
	if event.type == InputEvent.KEY:
		get_node("../LabelInfo").set_text( ? )
:bust_in_silhouette: Reply From: YeOldeDM

Is there any reason you need to use a Label node rather than a LineEdit node?

Otherwise, you should be able to use OS.get_scancode_string( s ) where ‘s’ will be the result of event.scancode. You would also want to keep the string you’re creating in a global var, with your input handling appending letters to the string. You would also want to check if the input is pressed, and somehow check for only alphanumeric key inputs.

var inputstr = "" setget _set_inputstr

func _input(event):
  if event.type == InputEvent.KEY and event.pressed:
    var s = OS.get_scancode_string( event.scancode )
    self.inputstr = self.inputstr + s

func _set_inputstr( what ):
  inputstr = what
  get_node("Label").set_text( inputstr )

Did not know “LineEdit” how does it work?

PerduGames | 2017-07-25 18:23

Much better with LineEdit, simple like this:

var txtLabel = get_node("LineEdit").get_text()

get_node("LabelInfo").set_text(txtLabel)

PerduGames | 2017-07-25 19:41