Search code examples
c++gtkmm3

gtkmm entry missing keys on signal_key_press_event


So I have been experimenting with the Gtkmm, as I want to migrate some of my code to C++ and I figured it would be easier.

I used to be able to use something that looks like so in C:

    g_signal_connect(entry, "key-release-event", G_CALLBACK(receiveKeyPressed), NULL);

But it seems when I try to use the similar system in Gtkmm:

    entry->signal_key_pressed().connect( sigc::ptr_fun(*receiveKeyPressed) );

It entirely misses all keyboard presses except for the shift keys and tab, etc.

Can anyone please explain why?


Solution

  • Connect your handler as first:

    Flags: Run Last

    #include <gtkmm.h>
    #include <iostream>
    
    int main()
    {
        auto app = Gtk::Application::create();
    
        Gtk::Window window;
        Gtk::Entry entry;
        window.add(entry);
    
        entry.signal_key_press_event().connect([&](GdkEventKey* event)->bool{
                std::cout<<"pressed: "<<std::hex<<event->keyval<<" "<<std::hex<<event->state<<std::endl; 
                return false; //to propagate the event further
            }, false); //run before other handlers (from entry)
    
        window.show_all();
        return app->run(window);
    }