This is my ui glade file:
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<menu id="menu1">
<item>
<attribute name="label" translatable="yes">A_dd</attribute>
<attribute name="action">win.add</attribute>
</item>
<section>
<item>
<attribute name="label" translatable="yes">_About...</attribute>
<attribute name="action">win.about</attribute>
<attribute name="accel"><Primary>a</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Close</attribute>
<attribute name="action">app.close</attribute>
</item>
</section>
</menu>
<object class="GtkApplicationWindow" id="toplevel">
<child>
<object class="GtkGrid" id="grid">
<child>
<object class="GtkMenuButton">
<property name="icon-name">menu</property>
<property name="always-show-arrow">true</property>
<property name="menu-model">menu1</property>
<property name="has-frame">true</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
</object>
</child>
<property name="child">grid</property>
</object>
</interface>
In on_activate function:
void on_activate(GtkApplication *app)
{
const GActionEntry entries[] = {
{"about", on_about, NULL, NULL, NULL, {0,0,0}}
};
GtkBuilder* builder = gtk_builder_new_from_resource(GLADE_FILE);
GtkWindow *toplevel = GTK_WINDOW(gtk_builder_get_object(builder, "toplevel"));
GtkIconTheme *icon_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
g_action_map_add_action_entries(G_ACTION_MAP(toplevel), entries, G_N_ELEMENTS (entries), app);
gtk_window_set_application (toplevel, app);
gtk_window_set_title (toplevel, PROGRAM_TITLE);
gtk_icon_theme_add_resource_path (icon_theme, PROGRAM_RC_PATH);
gtk_window_set_default_icon_name (PROGRAM_NAME);
gtk_window_present(toplevel);
g_object_unref(builder);
}
While in my application I can see the label accelerator "Ctrl+A", is not triggered with the keyboard, any ideas?
For me it works with:
<attribute name="accel"><Control>a</attribute>
However, the menu must be opened.
so "Enter + Ctrl + A"
Direct access via "Ctrl + A" must be implemented in the program at Gtk4.
Here is an example from Gtk-demo:
...
controller = gtk_shortcut_controller_new ();
gtk_shortcut_controller_set_scope (GTK_SHORTCUT_CONTROLLER (controller),
GTK_SHORTCUT_SCOPE_GLOBAL);
gtk_widget_add_controller (window, controller);
gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller),
gtk_shortcut_new (gtk_keyval_trigger_new (GDK_KEY_n, GDK_CONTROL_MASK),
gtk_named_action_new ("win.new")));
...