Skip to content

Gtk.ShortcutController

class — extends EventController, Gio.ListModel, Buildable

Manages keyboard shortcuts and their activation.

Most common shortcuts are using this controller implicitly, e.g. by adding a mnemonic underline to a Label, or by installing a key binding using WidgetClass.add_binding, or by adding accelerators to global actions using Application.set_accels_for_action.

But it is possible to create your own shortcut controller, and add shortcuts to it.

GtkShortcutController implements Gio.ListModel for querying the shortcuts that have been added to it.

GtkShortcutController as GtkBuildable

GtkShortcutControllers can be created in Builder ui files, to set up shortcuts in the same place as the widgets.

An example of a UI definition fragment with GtkShortcutController:

  <object class='GtkButton'>
    <child>
      <object class='GtkShortcutController'>
        <property name='scope'>managed</property>
        <child>
          <object class='GtkShortcut'>
            <property name='trigger'>&lt;Control&gt;k</property>
            <property name='action'>activate</property>
          </object>
        </child>
      </object>
    </child>
  </object>

This example creates a ActivateAction for triggering the activate signal of the Button. See ShortcutAction.parse_string for the syntax for other kinds of ShortcutAction. See ShortcutTrigger.parse_string to learn more about the syntax for triggers.

Constructors

new

@classmethod
def new(cls) -> EventController

Creates a new shortcut controller.

new_for_model

@classmethod
def new_for_model(cls, model: Gio.ListModel) -> EventController

Creates a new shortcut controller that takes its shortcuts from the given list model.

A controller created by this function does not let you add or remove individual shortcuts using the shortcut controller api, but you can change the contents of the model.

Parameters:

  • model — a GListModel containing shortcuts

Methods

add_shortcut

def add_shortcut(self, shortcut: Shortcut) -> None

Adds shortcut to the list of shortcuts handled by self.

If this controller uses an external shortcut list, this function does nothing.

Parameters:

  • shortcut — a GtkShortcut

get_mnemonics_modifiers

def get_mnemonics_modifiers(self) -> Gdk.ModifierType

Gets the mnemonics modifiers for when this controller activates its shortcuts.

get_scope

def get_scope(self) -> ShortcutScope

Gets the scope for when this controller activates its shortcuts.

See ShortcutController.set_scope for details.

remove_shortcut

def remove_shortcut(self, shortcut: Shortcut) -> None

Removes shortcut from the list of shortcuts handled by self.

If shortcut had not been added to controller or this controller uses an external shortcut list, this function does nothing.

Parameters:

  • shortcut — a GtkShortcut

set_mnemonics_modifiers

def set_mnemonics_modifiers(self, modifiers: Gdk.ModifierType | int) -> None

Sets the controller to use the given modifier for mnemonics.

The mnemonics modifiers determines which modifiers need to be pressed to allow activation of shortcuts with mnemonics triggers.

GTK normally uses the Alt modifier for mnemonics, except in GtkPopoverMenus, where mnemonics can be triggered without any modifiers. It should be very rarely necessary to change this, and doing so is likely to interfere with other shortcuts.

This value is only relevant for local shortcut controllers. Global and managed shortcut controllers will have their shortcuts activated from other places which have their own modifiers for activating mnemonics.

Parameters:

  • modifiers — the new mnemonics_modifiers to use

set_scope

def set_scope(self, scope: ShortcutScope | int) -> None

Sets the controller to have the given scope.

The scope allows shortcuts to be activated outside of the normal event propagation. In particular, it allows installing global keyboard shortcuts that can be activated even when a widget does not have focus.

With ShortcutScope.LOCAL, shortcuts will only be activated when the widget has focus.

Parameters:

  • scope — the new scope to use

Properties

item_type

item_type: type | GObject.Type  # read-only

The type of items. See Gio.ListModel.get_item_type.

mnemonic_modifiers

mnemonic_modifiers: Gdk.ModifierType | int  # read/write

The modifiers that need to be pressed to allow mnemonics activation.

model

model: Gio.ListModel  # read/write

A list model to take shortcuts from.

n_items

n_items: int  # read-only

The number of items. See Gio.ListModel.get_n_items.

scope

scope: ShortcutScope | int  # read/write

What scope the shortcuts will be handled in.