Class ShortcutController<T extends GObject>

All Implemented Interfaces:
Iterable<T>, Collection<T>, List<T>, SequencedCollection<T>, ListModel<T>, Buildable, Proxy, ListModelJavaList<T>

@Generated("org.javagi.JavaGI") public class ShortcutController<T extends GObject> extends EventController implements ListModel<T>, 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.addBinding, or by adding accelerators to global actions using Application.setAccelsForAction(String, String[]).

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

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

GtkShortcutController as GtkBuildable

GtkShortcutControllers can be created in GtkBuilder 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.parseString(String) for the syntax for other kinds of ShortcutAction. See ShortcutTrigger.parseString(String) to learn more about the syntax for triggers.

  • Constructor Details

    • ShortcutController

      public ShortcutController(MemorySegment address)
      Create a ShortcutController instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • ShortcutController

      public ShortcutController()
      Create a new ShortcutController.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the ShortcutController class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected ShortcutController asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class EventController
      Returns:
      the instance as if it were its parent type
    • forModel

      public static EventController forModel(ListModel 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
    • addShortcut

      public void addShortcut(Shortcut shortcut)

      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
    • getMnemonicsModifiers

      public Set<ModifierType> getMnemonicsModifiers()
      Gets the mnemonics modifiers for when this controller activates its shortcuts.
      Returns:
      the controller's mnemonics modifiers
    • getScope

      public ShortcutScope getScope()

      Gets the scope for when this controller activates its shortcuts.

      See setScope(ShortcutScope) for details.

      Returns:
      the controller's scope
    • removeShortcut

      public void removeShortcut(Shortcut 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:
      shortcut - a GtkShortcut
    • setMnemonicsModifiers

      public void setMnemonicsModifiers(Set<ModifierType> modifiers)

      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
    • setMnemonicsModifiers

      public void setMnemonicsModifiers(ModifierType... modifiers)

      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
    • setScope

      public void setScope(ShortcutScope 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 ShortcutScope.LOCAL, shortcuts will only be activated when the widget has focus.

      Parameters:
      scope - the new scope to use
    • builder

      public static ShortcutController.Builder<? extends ShortcutController.Builder> builder()
      A ShortcutController.Builder object constructs a ShortcutController with the specified properties. Use the various set...() methods to set properties, and finish construction with ShortcutController.Builder.build().
      Returns:
      the builder object