Search code examples
gtk3

Accelerator on a Gtk.Button without a label?


So I have a Gtk.Button. I want to add a accelator (shortcut key), such as Ctrl+O.

If I had a label, I could use a underscore under the letter to bind, such as "_Open".

My button doesn't have any label though, it has a icon. It is a button I have in my Gtk.HeaderBar.

How do I place a accelerator on a Gtk.Button with a icon but no label?


Solution

  • If using XML then add a <accelerator> element to the object.

    <object class="GtkButton" id="foo_button">
      <property name="visible">1</property>
      <property name="can-focus">1</property>
      <signal name="clicked" handler="_on_foo_button_clicked" swapped="no"/>
      <accelerator key="n" signal="activate" modifiers="GDK_CONTROL_MASK"/>
      <child>
        <object class="GtkImage">
          <property name="visible">1</property>
          <property name="icon-name">folder-new-symbolic</property>
        </object>
      </child>
    </object>