如何为gtk3应用程序添加按键绑定?

6

我正在尝试通过css为gtk3应用程序添加键绑定。目前为止,我已经做了以下工作:

// add style provider
GtkCssProvider *css = gtk_css_provider_new();                               
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),         
        GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);         
gtk_css_provider_load_from_path(css, "bindings.css", NULL);

这是在上一个代码段中加载的“bindings.css”文件:
@binding-set tree-view-bindings {
    bind "j" { "move-cursor" (display-lines, 1) };
    bind "k" { "move-cursor" (display-lines, -1) };
    bind "slash" { "start-interactive-search" () };
}

GtkTreeView {
    color: #F00;
    gtk-key-bindings: tree-view-bindings;
}

颜色设置可用,因此它不完全损坏。但是没有任何键绑定起作用。我缺少了什么?

有一个破折号缺失:应该是-gtk-key-bindings而不是gtk-key-bindings - eyelash
2个回答

0

如果我错了,请原谅,但我认为您把分号放错了位置 :)

@binding-set tree-view-bindings { bind "j" { "move-cursor" (display-lines, 1); } bind "k" { "move-cursor" (display-lines, -1); } bind "slash" { "start-interactive-search" (); } }


0

以下方法对我有效:

treeview { 
  -gtk-key-bindings: tree-view-bindings;
}

所以GtkTreeView变成了treeviewgtk-key-bindings变成了-gtk-key-bindings

备注:

  • 为了使用正确的选择器,您可以始终使用检查器:使用GTK_DEBUG=interactive启动您的应用程序

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接