GtkShortcutController

GtkShortcutController — Event controller for shortcuts

Properties

GdkModifierType mnemonic-modifiers Read / Write
GListModel * model Write / Construct Only
GtkShortcutScope scope Read / Write

Object Hierarchy

    GInterface
    ╰── GtkShortcutManager
    GObject
    ╰── GtkEventController
        ╰── GtkShortcutController

Prerequisites

GtkShortcutManager requires GObject.

Implemented Interfaces

GtkShortcutController implements GListModel and GtkBuildable.

Includes

#include <gtk/gtk.h>

Description

GtkShortcutController is an event controller that manages shortcuts.

Most common shortcuts are using this controller implicitly, e.g. by adding a mnemonic underline to a GtkLabel, or by installing a key binding using gtk_widget_class_add_binding(), or by adding accelerators to global actions using gtk_application_set_accels_for_action().

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

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

GtkShortcutController as a GtkBuildable

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

An example of a UI definition fragment with GtkShortcutController:

1
2
3
4
5
6
7
8
9
10
11
12
13
<object class='GtkButton'>
  <child>
    <object class='GtkShortcutController'>
      <property name='scope'>managed</property>
      <child>
        <object class='GtkShortcut'>
          <property name='trigger'><Control>k</property>
          <property name='action'>activate</property>
        </object>
      </child>
    </object>
  </child>
</object>

Functions

gtk_shortcut_controller_new ()

GtkEventController *
gtk_shortcut_controller_new (void);

Creates a new shortcut controller.

Returns

a newly created shortcut controller


gtk_shortcut_controller_new_for_model ()

GtkEventController *
gtk_shortcut_controller_new_for_model (GListModel *model);

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

 

Returns

a newly created shortcut controller


gtk_shortcut_controller_set_mnemonics_modifiers ()

void
gtk_shortcut_controller_set_mnemonics_modifiers
                               (GtkShortcutController *self,
                                GdkModifierType modifiers);

Sets the controller to have the given mnemonics_modifiers .

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

self

a GtkShortcutController

 

modifiers

the new mnemonics_modifiers to use

 

gtk_shortcut_controller_get_mnemonics_modifiers ()

GdkModifierType
gtk_shortcut_controller_get_mnemonics_modifiers
                               (GtkShortcutController *self);

Gets the mnemonics modifiers for when this controller activates its shortcuts. See gtk_shortcut_controller_set_mnemonics_modifiers() for details.

Parameters

Returns

the controller's mnemonics modifiers


gtk_shortcut_controller_set_scope ()

void
gtk_shortcut_controller_set_scope (GtkShortcutController *self,
                                   GtkShortcutScope scope);

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 GTK_SHORTCUT_SCOPE_LOCAL, shortcuts will only be activated when the widget has focus.

Parameters

self

a GtkShortcutController

 

scope

the new scope to use

 

gtk_shortcut_controller_get_scope ()

GtkShortcutScope
gtk_shortcut_controller_get_scope (GtkShortcutController *self);

Gets the scope for when this controller activates its shortcuts. See gtk_shortcut_controller_set_scope() for details.

Parameters

Returns

the controller's scope


gtk_shortcut_controller_add_shortcut ()

void
gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
                                      GtkShortcut *shortcut);

Adds shortcut to the list of shortcuts handled by self .

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

Parameters

self

the controller

 

shortcut

a GtkShortcut.

[transfer full]

gtk_shortcut_controller_remove_shortcut ()

void
gtk_shortcut_controller_remove_shortcut
                               (GtkShortcutController *self,
                                GtkShortcut *shortcut);

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

self

the controller

 

shortcut

a GtkShortcut

 

Types and Values

GtkShortcutController

typedef struct _GtkShortcutController GtkShortcutController;

enum GtkShortcutScope

Describes where GtkShortcuts added to a GtkShortcutController get handled.

Members

GTK_SHORTCUT_SCOPE_LOCAL

Shortcuts are handled inside the widget the controller belongs to.

 

GTK_SHORTCUT_SCOPE_MANAGED

Shortcuts are handled by the first ancestor that is a GtkShortcutManager

 

GTK_SHORTCUT_SCOPE_GLOBAL

Shortcuts are handled by the root widget.

 

GtkShortcutManager

typedef struct _GtkShortcutManager GtkShortcutManager;

This object is used to implement support for GtkShortcutScopes. Every widget that implements GtkShortcutManager will be used as a GTK_SHORTCUT_SCOPE_MANAGED.


struct GtkShortcutManagerInterface

struct GtkShortcutManagerInterface {
  void                  (* add_controller)              (GtkShortcutManager           *self,
                                                         GtkShortcutController        *controller);
  void                  (* remove_controller)           (GtkShortcutManager           *self,
                                                         GtkShortcutController        *controller);
};

The list of functions that can be implemented for the GtkShortcutManager interface.

Note that no function is mandatory to implement, the default implementation will work fine.

Property Details

The “mnemonic-modifiers” property

  “mnemonic-modifiers”       GdkModifierType

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

Owner: GtkShortcutController

Flags: Read / Write

Default value: GDK_ALT_MASK


The “model” property

  “model”                    GListModel *

A list model to take shortcuts from

Owner: GtkShortcutController

Flags: Write / Construct Only


The “scope” property

  “scope”                    GtkShortcutScope

What scope the shortcuts will be handled in.

Owner: GtkShortcutController

Flags: Read / Write

Default value: GTK_SHORTCUT_SCOPE_LOCAL