0

所以我一直在试验 Gtkmm,因为我想将我的一些代码迁移到 C++,我认为它会更容易。

我曾经能够在 C 中使用看起来像这样的东西:

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

但似乎当我尝试在 Gtkmm 中使用类似的系统时:

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

它完全错过了所有键盘按键,除了 shift 键和 tab 等。

谁能解释一下为什么?

4

1 回答 1

0

首先连接您的处理程序

标志:最后运行

#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);
}
于 2018-06-18T12:55:24.060 回答