Rajdeep Singha
Rajdeep Singha

Reputation: 65

event.key.keyval signal not connecting

public class Epoch.CitiesChooser : Gtk.MenuButton {
    private Epoch.CitiesSearch cities_search;
    
    construct {
        var button_grid = new Gtk.Grid ();
        button_grid.column_spacing = 6;
        var button_label = new Gtk.Label ("Dimapur");
        button_grid.add (button_label);
        button_grid.add (new Gtk.Image.from_icon_name ("pan-down-symbolic", Gtk.IconSize.MENU));
        add (button_grid);
        
        cities_search = new Epoch.CitiesSearch ();
        cities_search.show_all ();
        
        /* Use a event.eventkey.keypress here to add update the label of the menu_button using set_text */
        if (cities_search.location_entry.visible) {
            key_press_event.connect ((event) => {
                stdout.printf ("Keyval: 0x%X\n", event.key.keyval);
                if (event.key.keyval == 0xFF57) {
                    button_label.set_text (cities_search.location_entry.get_text ());
                    cities_search.location_entry.hide ();
                }
                return false;
            });
        }
        
        popover = new Gtk.Popover (this);
        popover.width_request = 310;
        popover.add (cities_search);
    }
}

I want to update the label of the menu_button when the 'end' key is pressed on the keyboard.

The code compiles without error but the signal is not connected and stdout.printf ("Keyval: 0x%X\n", event.key.keyval); does not print anything.

The label is also not updated.

Upvotes: 0

Views: 45

Answers (1)

Rajdeep Singha
Rajdeep Singha

Reputation: 65

I got this answer from an amazing guy at the vala discord server

Since the Gtk.LocationEntry is a subclass of Gtk.Entry, so we can use the activate signal.

Then I used this code to update the labels

cities_search.location_entry.activate.connect ((obj) => {
            button_label.set_text (cities_search.location_entry.get_text ());
            if (this.get_active ()) {
                this.set_active (false);
            }
        });

It fulfils my purpose but its not an answer to the original question.

Upvotes: 1

Related Questions