0 votes

I'm trying to create a Terminal/Debug Console-like scene.

When I press the KEY_UP key while using LineEdit, I make the text change. Then I want caret_position to come to the end of the text. But when we press the KEY_UP key, LineEdit *automatically* takes the caret_position to the *beginning*.

How can I turn off resetting this caretposition. This does not happen when using another key instead of KEYUP. Using a key other than KEY_UP for this job feels inappropriate for the job

extends Control

var texts = ["hello","world","good","day","to","you"]
var history_line = 4

func _ready():
    $LineEdit.grab_focus()
    $LineEdit.text = texts[history_line]

func _input(event):
    if event.is_action_pressed("ui_up") :
        if history_line == 0 : return
        history_line -= 1
        $LineEdit.text = texts[history_line]
        move_cursor_to_end()
    if event.is_action_pressed("ui_down") : 
        if history_line == texts.size() -1 :return
        history_line += 1
        $LineEdit.text = texts[history_line]
        move_cursor_to_end()


func move_cursor_to_end() :
    $LineEdit.caret_position = $LineEdit.text.length()

Godot Engine v3.2.3
English is not my first language, I'm sorry if there are mistakes

in Engine by (15 points)

1 Answer

+1 vote
Best answer

I tweak your code a little bit, now it should work.

extends Control

var texts = ["hello","world","good","day","to","you"]
var history_line = 4

func _ready():
    $LineEdit.grab_focus()
    $LineEdit.text = texts[history_line]
    move_cursor_to_end()

func _input(event):
    if event.is_action_pressed("ui_up") :
        if history_line == 0 :
            accept_event()
            return
        history_line -= 1
        $LineEdit.text = texts[history_line]
        move_cursor_to_end()
    if event.is_action_pressed("ui_down") :
        if history_line == texts.size() -1 :return
        history_line += 1
        $LineEdit.text = texts[history_line]
        move_cursor_to_end()

func move_cursor_to_end() :
    $LineEdit.caret_position = $LineEdit.text.length()
    accept_event()
by (598 points)
selected by

Thank you! accept_event() does just what I want.

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.