brandizzi
brandizzi

Reputation: 27050

How to add keyboard shortcuts / accelerator keys for a menu item created by a Gedit plugin

I created a Gedit 2 plugin which adds an item to a menu as described here. How could I bind a keyboard shortcut / accel key / accelerator key to this menu item?

Upvotes: 3

Views: 2300

Answers (1)

brandizzi
brandizzi

Reputation: 27050

Following the given tutorial, your plugin have some lines like the ones below somewhere:

self._action_group = gtk.ActionGroup("ExamplePyPluginActions")
self._action_group.add_actions([("ExamplePy", None, _("Clear document"),
         None, _("Clear the document"),
         self.on_clear_document_activate)])
manager.insert_action_group(self._action_group, -1)

Just replace the second None argument in

self._action_group.add_actions([("ExamplePy", None, _("Clear document"),
        None, _("Clear the document"),
        self.on_clear_document_activate)])

by your desired keyboard shortcut - let us say, ControlR:

self._action_group.add_actions([("ExamplePy", None, _("Clear document"),
        "<control>r", _("Clear the document"), # <- here
        self.on_clear_document_activate)])

You may have used a manually constructed action as well (this is at least my favorite way of working with it):

action = gtk.Action("ExamplePy", 
        _("Clear document"), 
        _("Clear the document"), None)
action.connect("activate", self.on_open_regex_dialog)
action_group = gtk.ActionGroup("ExamplePyPluginActions")
action_group.add_action(action)

In this case, just replace action_group.add_action() by action_group.add_action_with_accel():

action_group = gtk.ActionGroup("ExamplePyPluginActions")
action_group.add_action_with_accel(action, "<control>r")

(Asked and aswered by myself because of this and this; I lost some real time looking for it and I thought it will be a good reference.)

Upvotes: 8

Related Questions