[GTK C] How do you add shorcuts for buttons with icon

Hello, I’m adding buttons to my Application, and it works fine, but I would also like to add keyboard shortcuts to them. For buttons that show text I used mnemonic and it is OK (even if it took me time that it needs to have ALT press for the keys to press to show), but for icon buttons I don’t see how I can do that. Those icon buttons allow the user to move/rotate the selected piece, so I’m willing to bind them to arrow keys for movements and page Up/Down keys for rotations, and it would be great if the magnitude could be to 1 unit when combined with CTRL and 10 with ALT or any possible combination. I looked on GTK Demos and HOWDOIs but didn’t find anything.

code I’m using

  bModeP = gtk_toggle_button_new_with_mnemonic(gtexte(31));
  gtk_box_append (GTK_BOX (box), bModeP);
  bModeF = gtk_toggle_button_new_with_mnemonic(gtexte(32));
  gtk_box_append (GTK_BOX (box), bModeF);
  bModeL = gtk_toggle_button_new_with_mnemonic(gtexte(33));
  gtk_box_append (GTK_BOX (box), bModeL);

  gtk_toggle_button_set_group(GTK_TOGGLE_BUTTON(bModeP), GTK_TOGGLE_BUTTON(bModeF));
  gtk_toggle_button_set_group(GTK_TOGGLE_BUTTON(bModeL), GTK_TOGGLE_BUTTON(bModeF));
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bModeP), TRUE);
  dd.mode = MODE_PIECE;

  eRech = gtk_entry_new();
  gtk_entry_set_placeholder_text(GTK_ENTRY(eRech), "123");
  gtk_editable_set_width_chars(GTK_ENTRY(eRech), 3);
  gtk_box_append (GTK_BOX (box), eRech);

  GtkWidget *bZP = gtk_button_new_from_icon_name("zoom-in-symbolic");
  gtk_box_append (GTK_BOX (box), bZP);
  g_signal_connect(bZP, "clicked", G_CALLBACK(zoom_in_clicked), bZP);

  GtkWidget *bZM = gtk_button_new_from_icon_name("zoom-out-symbolic");
  gtk_box_append (GTK_BOX (box), bZM);
  g_signal_connect(bZM, "clicked", G_CALLBACK(zoom_out_clicked), bZM);

  // bouton tourner gauche
  GtkWidget *bTG = gtk_button_new_from_icon_name("object-rotate-left-symbolic");
  gtk_box_append (GTK_BOX (box), bTG);
  g_signal_connect(bTG, "clicked", G_CALLBACK(tournePiece), (gpointer *)R_GAUCHE);

  // bouton tourner droite
  GtkWidget *bTD = gtk_button_new_from_icon_name("object-rotate-right-symbolic");
  gtk_box_append (GTK_BOX (box), bTD);
  g_signal_connect(bTD, "clicked", G_CALLBACK(tournePiece), (gpointer *)R_DROITE);

  // gauche haut bas droite
  GtkWidget *bDH= gtk_button_new_from_icon_name("go-up-symbolic");
  gtk_box_append (GTK_BOX (box), bDH);
  g_signal_connect(bDH, "clicked", G_CALLBACK(deplacePiece), (gpointer *)D_HAUT);

  GtkWidget *bDG= gtk_button_new_from_icon_name("go-previous-symbolic");
  gtk_box_append (GTK_BOX (box), bDG);
  g_signal_connect(bDG, "clicked", G_CALLBACK(deplacePiece), (gpointer *)D_GAUCHE);

You could add a keyboard shortcut on the whole GtkApplication.

See gtk_application_set_accels_for_action() and GAction from GIO.

Thank you, I had a hard time to understand how to use Actions for the menu, and didn’t use it for buttons, so I first need to make my buttons use action as in the HowDoI GAction example. Then use gtk_application_set_accels_for_action(). Indeed it seems that it would had been easier to do if my UI was made from a .ui file but I didn’t understand lots of things with Builder, from its syntax to the way to use it as resource instead of file, so I made my interface with API calls. I finally understood how to use action for my buttons and I am slowly doing this.

It worked well using gtk_application_set_accels_for_action().

That’s more a matter of taste. Some developers need or needed to edit *.ui files by hand, while XML is at the same time criticized, so it is kind of contradictory.

See Glade Not Recommended – Chris's Design & Development

I personally prefer writing API calls directly, it also permits to have more re-usable code, by writing small utility functions.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.