3

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?

brandizzi
  • 26,083
  • 8
  • 103
  • 158

1 Answers1

8

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.)

Community
  • 1
  • 1
brandizzi
  • 26,083
  • 8
  • 103
  • 158