Class ShortcutTrigger
java.lang.Object
com.vaadin.flow.component.trigger.internal.Trigger
com.vaadin.flow.component.trigger.internal.DomEventTrigger
com.vaadin.flow.component.trigger.internal.KeyboardEventTrigger
com.vaadin.flow.component.trigger.internal.ShortcutTrigger
- All Implemented Interfaces:
Serializable
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 eitherevent.keyorevent.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+Sdoesn't fire onCtrl+Shift+S, leaving that combo free to bind separately), - calls
preventDefault()andstopPropagation()so the browser doesn't act on the shortcut (e.g. open the Save dialog forCtrl+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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.KeyboardEventTrigger
KeyboardEventTrigger.EventData -
Constructor Summary
ConstructorsConstructorDescriptionShortcutTrigger(Component host, Key key, KeyModifier... modifiers) Creates a shortcut trigger that fires whenkeyis pressed with exactly the givenmodifiersheld down. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendHandlerBody(StringBuilder body, List<Object> extraCaptures) Hook for subclasses to contribute statements to the wrapper handler body and extra captures referenced by those statements.Methods inherited from class com.vaadin.flow.component.trigger.internal.KeyboardEventTrigger
forKeys, preventDefault, stopPropagationMethods inherited from class com.vaadin.flow.component.trigger.internal.DomEventTrigger
appendInstallPrelude, install
-
Constructor Details
-
ShortcutTrigger
Creates a shortcut trigger that fires whenkeyis pressed with exactly the givenmodifiersheld down.- Parameters:
host- the component whose root element listens for the shortcut, notnullkey- the key that completes the shortcut, notnullmodifiers- the modifiers that must be held; pass none for a plain-key shortcut. Must not containKeyModifier.ALT_GRAPH.
-
-
Method Details
-
appendHandlerBody
Description copied from class:DomEventTriggerHook for subclasses to contribute statements to the wrapper handler body and extra captures referenced by those statements. The base implementation appendse.preventDefault();ande.stopPropagation();when the corresponding flags are set.Subclasses overriding this method typically prepend their own guard statements (and call
super.appendHandlerBodyafterwards) so the guard runs beforepreventDefault— preventing the default action on events the guard would have rejected would be wrong. Each entry added toextraCapturesis appended after the event name capture, so the first extra is referenced as$2in the body, the next as$3, and so on.- Overrides:
appendHandlerBodyin classKeyboardEventTrigger- Parameters:
body- the wrapper body so far; statements appended here run inside theconst h = e => { ... $0(e); }wrapper, in order, with the event available aseextraCaptures- mutable list of captures appended after the event-name capture ($1); the first capture added is$2, etc.
-