+1 vote

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( ? )
in Engine by (201 points)

1 Answer

+2 votes

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 )
by (1,328 points)

Did not know "LineEdit" how does it work?

Much better with LineEdit, simple like this:

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

get_node("LabelInfo").set_text(txtLabel)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.