Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting key accelerators in Gtk+ (gtkmm)

I've been writing a Gtk+ application using gtkmm, and I'm trying to add a global keyboard shortcut which calls a callback. Unfortunately, the connect() method of Gtk::AccelGroup isn't available in gtkmm, apparently intentionally because you can make the connections using ActionGroups...

Anyway, I have the following code:

actions_= Gtk::ActionGroup::create();
actions_->set_accel_group(Gtk::AccelGroup::create());

actions_->add(
    Gtk::Action::create("new"), Gtk::AccelKey("<control>n"),
    sigc::mem_fun(this, &Window::new_buffer_thing)
);

_gtk_window().add_accel_group(actions_->get_accel_group());

Which compiles and runs without warning, but the keyboard shortcut does nothing. I've been fiddling with this for hours, so any help or direction would be appreciated!

Am I doing something obviously wrong? Why wouldn't the accelerator work?

like image 781
Kazade Avatar asked Nov 11 '22 19:11

Kazade


1 Answers

a bit late to answer this, but I've been working at this same problem today, even if in a different environment: python, gtk2.

as far as I understand from a little experimenting with this tutorial, actions won't be active unless associated to a toolbox or a menubar. too bad, just do that, pack the toolbar into a VBox, and make it invisible, something like this:

actiongroup = gtk.ActionGroup('window-clip-actions')
accelgroup = gtk.AccelGroup()
fake_toolbar = gtk.Toolbar()
view.get_window().add_accel_group(accelgroup)
view.get_window().get_content_area().pack_start(fake_toolbar)
for shortcut, cb in (('<ctrl><shift>c', self.on_window_clip_copy),
                     ('<ctrl><shift>v', self.on_window_clip_paste)):
    action = gtk.Action(shortcut, shortcut, 'clip-action', None)
    actiongroup.add_action_with_accel(action, shortcut)
    action.connect("activate", cb)
    action.set_accel_group(accelgroup)
    action.connect_accelerator()
    toolitem = action.create_tool_item()
    fake_toolbar.insert(toolitem, -1)
fake_toolbar.set_visible(False)

it would be interesting to know if the same approach would help the OP.

like image 109
mariotomo Avatar answered Nov 15 '22 05:11

mariotomo