Class KeyboardEventTrigger
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ShortcutTrigger
keydown, keyup). Exposes the
KeyboardEvent properties as static Action.Input fields on
KeyboardEventTrigger.EventData, and supports filtering by key via forKeys(Key...)
so only the configured keys produce a server-side fire.
The single-argument constructor defaults to keydown, the standard
event for keyboard shortcuts. A typical shortcut wiring looks like:
new KeyboardEventTrigger(ui).forKeys(Key.ENTER, Key.ESCAPE).triggers(action);
Without a filter, the trigger fires on every keyboard event of the configured
name. With one or more forKeys(Key...) calls, the client compares
event.key against the configured set and only invokes the action on a
match — unmatched events do not cross the network.
The modifier-key fields (shiftKey, ctrlKey, altKey,
metaKey) are intentionally duplicated from
MouseEventTrigger.EventData: although the underlying JS property is
the same, each field is class-scoped so a single instance can't be safely
shared across unrelated event families. Subclasses bound to a specific event
name may extend KeyboardEventTrigger.EventData to add event-specific properties.
For internal use only. May be renamed or removed in a future release.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTheKeyboardEventproperties exposed as staticAction.Inputsources. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a keyboard-event trigger that fires onkeydown.KeyboardEventTrigger(Component host, String eventName) Creates a keyboard-event trigger that fires when the host receives a DOM event with the given name. -
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.Restricts the trigger to fire only whenevent.keymatches one of the given keys.Configures the trigger to callevent.preventDefault()when it fires, suppressing the browser's default action (e.g. submitting a form on Enter, scrolling on Space).Configures the trigger to callevent.stopPropagation()when it fires, preventing the event from bubbling to ancestor listeners.Methods inherited from class com.vaadin.flow.component.trigger.internal.DomEventTrigger
appendInstallPrelude, install
-
Constructor Details
-
KeyboardEventTrigger
Creates a keyboard-event trigger that fires onkeydown. Use this constructor for shortcut wirings;keydownis the standard event for that purpose (it fires immediately on press, repeats while held, and is dispatched before the browser's default action).- Parameters:
host- the component whose root element listens for the event, notnull
-
KeyboardEventTrigger
Creates a keyboard-event trigger that fires when the host receives a DOM event with the given name.- Parameters:
host- the component whose root element listens for the event, notnulleventName- the DOM keyboard-event name (e.g."keydown","keyup"), notnull
-
-
Method Details
-
forKeys
Restricts the trigger to fire only whenevent.keymatches one of the given keys. Multiple calls accumulate; with no call the trigger fires on every keyboard event of the configured name.Matching is done client-side against every printable representation in
Key.getKeys(), so a key likeKey.SPACE(whoseevent.keyvalue is" ") matches the spacebar press. Events that don't match are dropped in the browser handler — they do not reach the server.- Parameters:
keys- the keys to allow; notnull, none of the entriesnull- Returns:
- this trigger, for chaining
-
preventDefault
Description copied from class:DomEventTriggerConfigures the trigger to callevent.preventDefault()when it fires, suppressing the browser's default action (e.g. submitting a form on Enter, scrolling on Space). AffectsTrigger.triggers(Action...)calls made after this method; existing wirings are not retroactively changed.- Overrides:
preventDefaultin classDomEventTrigger- Returns:
- this trigger, for chaining
-
stopPropagation
Description copied from class:DomEventTriggerConfigures the trigger to callevent.stopPropagation()when it fires, preventing the event from bubbling to ancestor listeners. AffectsTrigger.triggers(Action...)calls made after this method; existing wirings are not retroactively changed.- Overrides:
stopPropagationin classDomEventTrigger- Returns:
- this trigger, for chaining
-
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 classDomEventTrigger- 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.
-