Search code examples
gtkvala

Gtk.Application key accelerators not working


Ths is my problem :

I am trying to add keyboard control to my application using a dictionary of (string,VoidFunc)

I start by creationg an action named "app." + key_name("h" for example) and doing

action.activate.connect( () => f() ) where f is the function (VoidFunc instance).

Then I add that action to my app.

Then I create an accelerator using Gtk.accelerator_name and add that to my app.

I am subclassing Gtk.Application.

Here are the two main functions that do the work.

private void install_keyboard_support()
{
    Gee.HashMap<string,VoidFunc> actions = new Gee.HashMap<string,VoidFunc>();
    fill_actions(ref actions);
    int i = 0;int sz = actions.size;
    while(i < sz)
    {
        string k = actions.keys.to_array()[i];
        var f = actions[k];
        var action_name = "app." + k;

        var action = new SimpleAction(action_name,null);
        action.activate.connect(() => f());
        this.add_action(action);

        var accel_name = Gtk.accelerator_name(k.data[0],Gdk.ModifierType.CONTROL_MASK);
        this.add_accelerator(accel_name,action_name,null);

        _puts(@"Added accelerator with name = $accel_name and which trigers $action_name action!");

        i++;
    }

    _puts("Installed keyboard support!");
}

private void fill_actions(ref Gee.HashMap<string,VoidFunc> actions)
{
    actions["h"] = () => Process.spawn_command_line_async("xdg-open /usr/share/doc/ewns-viewer/help.html");
}

As you see everything should work :).

One thing that works is doing

this.add_accelerator("<Primary>h","app.help",null)

app.help is a simple action definied as

var help_action = new SimpleAction("help",null);
    help_action.activate.connect(() => Process.spawn_command_line_async("xdg-open /usr/share/doc/ewns-viewer/help.html"));
    this.add_action(help_action);

Solution

  • The problem was that I created actions with "app." + k while the app. prefix was not needed.