How to Exit on 'Return' button Key Press while in entry field Gtk4 Gtk.Entry

we are creating an extension, below is the code for a Gtk.Entry that takes the input.
However after we finish writing the text we want to, we are not able to exit with a ‘tab’ key or ‘return’.

Is it possible to exit the box or close the window on 'tab press or return ‘key’ press?

addTextUrl() {
        let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, margin_top: 5 });
        this.entry = new Gtk.Entry({ hexpand: true, margin_start: 20 });
        this.entry.set_placeholder_text("type your 'One Thing' here.")

        this.entry.set_text(this._settings.get_string('text'));
        this.entry.connect('changed', (entry) => {
            this._settings.set_string('text', entry.get_text());
        });

        hbox.append(this.entry);

        return hbox;
    }

1

2 Likes

I have no idea if it may help in your case, but most of the time when I use a GtkEntry I connect to the “activate” signal. That one is emitted when user presses ENTER or some other Meta-Keys like ESC. I use the changed signal only when I need update for every keystroke, e.g. when I print the input somewhere else. See Gtk.Entry::activate

3 Likes

That’s a quick and excellent answer. It worked like magic. Thank you very much.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.