logicslab 15 Unverified User

Dear Friends,
I am a newbie in GTK3 development using C. I created a file menu with some sample menu options. But I need to activate each menu using keyboard shortcut. Please provide a sample code based on the given working code .

#include <gtk/gtk.h>

static void open_activated(GtkWidget *f)
{
    g_print("File -> Open activated.\n");
}

static void quit_activated(GtkWidget *f)
{
    g_print("File -> Quit activated...bye.\n");
    gtk_main_quit();
}

static void another_activated(GtkWidget *widget, gpointer data)
{
    g_print("%s clicked.\n", (gchar*)data);
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *menubar;
    GtkWidget *filemenu;
    GtkWidget *file;
    GtkWidget *open;
    GtkWidget *quit;

    GtkWidget *anothermenu;
    GtkWidget *another;
    GtkWidget *anothermenuitem;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    //gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    menubar = gtk_menu_bar_new();

    filemenu = gtk_menu_new();
    file = gtk_menu_item_new_with_label("File");
    open = gtk_menu_item_new_with_label("Open");
    quit = gtk_menu_item_new_with_label("Quit");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
    //Connects GCallback function open_activated to "activate" signal for "open" menu item
    g_signal_connect(G_OBJECT(open), "activate", G_CALLBACK(open_activated), NULL);
    //Connects GCallback function quit_activated to "activate" signal for "quit" menu item
    g_signal_connect(G_OBJECT(quit), "activate", G_CALLBACK(quit_activated), NULL);

    anothermenu = gtk_menu_new();
    another = gtk_menu_item_new_with_label("Another");
    anothermenuitem = gtk_menu_item_new_with_label("Another Menu Item");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(another), anothermenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(anothermenu), anothermenuitem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), another);
    //Connects GCallback function another_activated to "activate" signal for another
    g_signal_connect(G_OBJECT(anothermenuitem), "activate", G_CALLBACK(another_activated), "anothermenuitem");
    g_signal_connect(G_OBJECT(another), "activate", G_CALLBACK(another_activated), "another");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box), menubar, FALSE, FALSE, 3);
    gtk_container_add(GTK_CONTAINER(window), box);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

Waiting your fast reply .

Thanks
Anes P.A

Dani AI

Generated

Two useful things for keyboard access are mnemonics (Alt+F opens the File menu) and accelerators (Ctrl+O triggers Open immediately). In your code you already connect the menu-items' "activate" callbacks; the simplest approach is to add a GtkAccelGroup to the window and attach accelerators that invoke the same "activate" signal.

Add an accel group (once), attach it to the top-level window, then add accelerators for the menu items before showing the window:

/* create accel group and attach to the window */
GtkAccelGroup *accel = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(window), accel);

/* make Ctrl+O activate the "open" item and Ctrl+Q activate "quit" */
gtk_widget_add_accelerator(open,  "activate", accel,
                           GDK_KEY_o, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
gtk_widget_add_accelerator(quit,  "activate", accel,
                           GDK_KEY_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

/* optional: give top-level menus mnemonics (Alt+F, Alt+A) */
file    = gtk_menu_item_new_with_mnemonic("_File");
another = gtk_menu_item_new_with_mnemonic("_Another");

Notes and troubleshooting:

  • Use GTK_ACCEL_VISIBLE so the shortcut string appears in the menu. Add the accel group to the window before gtk_widget_show_all().
  • The accelerators call the same "activate" signal your g_signal_connect(..., "activate", ...) handlers already handle, so no change to your callbacks is needed.
  • To let users rebind shortcuts persistently, use gtk_widget_set_accel_path() together with the gtk_accel_map API, or consider GAction/GtkApplication for newer apps.
  • For a11y, keep menu labels descriptive and use mnemonics for keyboard navigation. If a GDKKEY* symbol is undefined, include the appropriate GDK header or ensure your GTK development headers are up to date.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.