How to convert code to use EventControllerKey?

Recently I tried to use EventControllerKey instead connect keys signal, but sadly I found that functions which I choose, don’t ever execute.
This code in GTK 3 worked fine

tree_view.connect_key_press_event(opening_enter_function);

but after change only this lines (also in GTK 3), clicking multiple times at tree_view content, don’t print anything to console(simple prints are inside opening_enter_function_ported function):

let evk = EventControllerKey::new(&tree_view);
evk.connect_key_pressed(opening_enter_function_ported);

You do need to add the EventControllerKey object to the widget you want to handle events from. In C that function is gtk_widget_add_controller().

In GTK3, you will need to keep the EventController instance around, otherwise it will be collected at the end of the scope and won’t exist any more.

In GTK4, you add the EventController to the widget, using gtk4::WidgetExt::add_controller.

2 Likes

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