【问题标题】:How to add keyboard shortcuts / accelerator keys for a menu item created by a Gedit plugin如何为 Gedit 插件创建的菜单项添加键盘快捷键/加速键
【发布时间】:2011-10-31 21:00:51
【问题描述】:

我创建了一个 Gedit 2 插件,它可以向菜单中添加一个项目,如 here 所述。如何将键盘快捷键/加速键/加速键绑定到此菜单项?

【问题讨论】:

    标签: python gtk keyboard-shortcuts pygtk gedit


    【解决方案1】:

    按照给定的教程,您的插件在某处有如下几行:

    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)
    

    只需替换中的第二个None 参数

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

    通过您想要的键盘快捷键 - 让我们说,ControlR

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

    您可能也使用过手动构建的动作(这至少是我最喜欢的使用方式):

    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)
    

    在这种情况下,只需将action_group.add_action() 替换为action_group.add_action_with_accel()

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

    (因thisthis 而自问自答;我花了一些时间寻找它,我认为这将是一个很好的参考。)

    【讨论】:

    • 知道是否/如何使用“0”之类的快捷方式吗?我也尝试了“zero”,但它们都不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    相关资源
    最近更新 更多