GtkWidget::add_accelerator

void add_accelerator(string signal, GtkAccelGroup group , int accel_key, GdkModifyerType modifiers, GtkAccelFlags flags);

Adds an accelerator (shortcut key) for the widget, so that the button/widget can be accessed by using the keyboard, without moving the mouse or tabbing through widgets.

signal is the name of a signal that should be invoked on the widget once the accelerator is pressed. For a button, you most likely want to pass clicked here.

group is a GtkAccelGroup and should be the one set active for the window. Maybe you need to create the group and add it to the window first via add_accel_group() to be able to use it here.

accel_key defines the key to be pressed, see Symbolic names for keys to find the right one.

modifiers tells which additional button has to be pressed to emit the signal. Normally, you want to use Gdk::CONTROL_MASK (Ctrl key) or Gdk::MOD1_MASK (Alt key).

Example 149. Adding a shortcut for a button

//Our window
$wnd = new GtkWindow();
//Accelerator group
$group = new GtkAccelGroup();
//Add group to window, making it active
$wnd->add_accel_group($group);

//"back" button
$btn = GtkButton::new_from_stock(Gtk::STOCK_GO_BACK);
$btn->add_accelerator(
    'clicked', $group, Gdk::KEY_Left, Gdk::MOD1_MASK, 0
);
$wnd->add($btn);

In this example, you can use the shortcut Alt+Left to activate the back-button.

See also: remove_accelerator()