Keybindings in gtk-rs

I have a mostly working gtk-rs application that I want to add some more keybindings to. I've created the ui in Glade and can easily add accelerators to a widget.

My issue is that I want to keep the ui as simple as possible, so there are certain actions for which I don't want a widget to be visible and adding clutter. I have tried adding an accel to a widget that is not visible, and the problem is that they only work when the widget is visible. What I need is a generic way to attach a function or closure to a key press event. I don't mind using external crates within reason.

It turns out that I was able to solve it on my own after quite a bit of searching through the docs. It requires pulling in some functionality from gdk. I'll post a small snippet here for anyone interested in the future. This can easily be extended to handle any number of bindings, but this here just allows to close the program with Ctrl-q.

    window.connect_key_press_event(move |_,gdk| {
        let key = gdk.get_keycode().unwrap();
        if gdk.get_state().contains(ModifierType::CONTROL_MASK) {
            match key {
                24  => gtk::main_quit(),
                _ => {}

1 Like