Class ShortcutTrigger

All Implemented Interfaces:
Serializable

public class ShortcutTrigger extends KeyboardEventTrigger
Fires when a specific key + modifier combination is pressed on the host's root element. Built on top of KeyboardEventTrigger with the typical shortcut defaults pre-applied:
  • listens for keydown,
  • filters by the configured Key (matching either event.key or event.code),
  • requires an exact match of the configured modifiers — modifiers passed to the constructor must be pressed, all others must NOT be pressed (so Ctrl+S doesn't fire on Ctrl+Shift+S, leaving that combo free to bind separately),
  • calls preventDefault() and stopPropagation() so the browser doesn't act on the shortcut (e.g. open the Save dialog for Ctrl+S) and ancestor shortcut handlers don't double-fire.

Example:


 new ShortcutTrigger(layout, Key.KEY_S, KeyModifier.CONTROL)
         .triggers(saveAction);
 

Pass zero modifiers for a plain-key shortcut: new ShortcutTrigger(host, Key.ESCAPE).

KeyModifier.ALT_GRAPH is not supported — KeyboardEvent has no altGraphKey flag, and supporting it via getModifierState("AltGraph") is left for a follow-up.

For internal use only. May be renamed or removed in a future release.

See Also:
  • Constructor Details

    • ShortcutTrigger

      public ShortcutTrigger(Component host, Key key, KeyModifier... modifiers)
      Creates a shortcut trigger that fires when key is pressed with exactly the given modifiers held down.
      Parameters:
      host - the component whose root element listens for the shortcut, not null
      key - the key that completes the shortcut, not null
      modifiers - the modifiers that must be held; pass none for a plain-key shortcut. Must not contain KeyModifier.ALT_GRAPH.
  • Method Details

    • appendHandlerBody

      protected void appendHandlerBody(StringBuilder body, List<Object> extraCaptures)
      Description copied from class: DomEventTrigger
      Hook for subclasses to contribute statements to the wrapper handler body and extra captures referenced by those statements. The base implementation appends e.preventDefault(); and e.stopPropagation(); when the corresponding flags are set.

      Subclasses overriding this method typically prepend their own guard statements (and call super.appendHandlerBody afterwards) so the guard runs before preventDefault — preventing the default action on events the guard would have rejected would be wrong. Each entry added to extraCaptures is appended after the event name capture, so the first extra is referenced as $2 in the body, the next as $3, and so on.

      Overrides:
      appendHandlerBody in class KeyboardEventTrigger
      Parameters:
      body - the wrapper body so far; statements appended here run inside the const h = e => { ... $0(e); } wrapper, in order, with the event available as e
      extraCaptures - mutable list of captures appended after the event-name capture ($1); the first capture added is $2, etc.