Top Description Fields Methods
org.fxmisc.wellbehaved.event

public Interface EventPattern<T extends Event, U extends T>

Static Imports
javafx.scene.input.KeyCombination.ALT_ANY, .KeyCombination.META_ANY, .KeyCombination.SHIFT_ANY, .KeyCombination.SHORTCUT_ANY, javafx.scene.input.KeyEvent.*, javafx.scene.input.MouseEvent.*

Helper class for pattern-matching one or more EventTypes (e.g. the "case" line in a powerful switch statement). When match(Event) returns a non-empty Optional, the corresponding InputHandler will be called.

Usages

This class provides a number of static factory methods that provide the base pattern to match.

Once a base pattern is created, one can further define the pattern for which to match for by adding what are known as "guards" in pattern matching: andThen(EventPattern), onlyIf(Predicate), unless(Predicate), and anyOf(EventPattern[]). See each method's javadoc for more info.

Examples


// a pattern that matches any key pressed event
keyPressed()

// a pattern that matches only key pressed events where the user
// pressed a digit key
keyPressed().onlyIf(pressedKey -> pressedKey.getCode().isDigitKey())

Field Summary

Modifier and TypeField and Description
public static final KeyCombination.Modifier[]

Method Summary

Modifier and TypeMethod and Description
public default <V extends U> EventPattern<T, V>
andThen(EventPattern<? super U, V> next)

Returns an EventPattern that matches the given event type only when this event pattern matches it and the next EventPattern matches it.

public static <T extends Event, U extends T> EventPattern<T, U>
anyOf(EventPattern<T, ? extends U>... events)

Returns an EventPattern that matches the given event type when any of the given EventPatterns match the given event type; useful when one wants to specify the same behavior for a variety of events (i.e. the "copy" action when a user press "CTRL+C" on Windows or "COMMAND+C" on Mac).

public static EventPattern<Event, MouseEvent>
public static <T extends Event> EventPattern<Event, T>
eventType(EventType<? extends T> eventType)

public Set<EventType<? extends U>>
public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
keyPressed(String character, KeyCombination.Modifier... modifiers)

public static EventPattern<Event, KeyEvent>
keyPressedNoMod(String character)

Matches the given key pressed event regardless of modifiers; this should only be used for the rare KeyEvents which require a pressed modifier (e.g. Shift) to generate it (e.g. "{").

public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
keyReleased(String character, KeyCombination.Modifier... modifiers)

public static EventPattern<Event, KeyEvent>
keyReleasedNoMod(String character)

Matches the given key released event regardless of modifiers; this should only be used for the rare KeyEvents which require a pressed modifier (e.g. Shift) to generate it (e.g. "{").

public static EventPattern<Event, KeyEvent>
public static EventPattern<Event, KeyEvent>
keyTyped(Predicate<String> charTest, KeyCombination.Modifier... modifiers)

public static EventPattern<Event, KeyEvent>
keyTyped(String character, KeyCombination.Modifier... modifiers)

public static EventPattern<Event, KeyEvent>
keyTypedNoMod(String character)

Matches the given key typed event regardless of modifiers; this should only be used for the rare KeyEvents which require a pressed modifier (e.g. Shift) to generate it (e.g. "{").

public Optional<? extends U>
match(T event)

Returns a non-empty Optional when a match is found.

public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public static EventPattern<Event, MouseEvent>
public default EventPattern<T, U>
onlyIf(Predicate<? super U> condition)

Returns an EventPattern that matches the given event type only if this event pattern matches it and the event type passed the given condition

public default EventPattern<T, U>
unless(Predicate<? super U> condition)

Returns an EventPattern that matches the given event type only if this event pattern matches it and the event type fails the given condition

Field Detail

ALL_MODIFIERS_AS_ANYback to summary
public static final KeyCombination.Modifier[] ALL_MODIFIERS_AS_ANY

Method Detail

andThenback to summary
public default <V extends U> EventPattern<T, V> andThen(EventPattern<? super U, V> next)

Returns an EventPattern that matches the given event type only when this event pattern matches it and the next EventPattern matches it.

anyOfback to summary
public static <T extends Event, U extends T> EventPattern<T, U> anyOf(EventPattern<T, ? extends U>... events)

Returns an EventPattern that matches the given event type when any of the given EventPatterns match the given event type; useful when one wants to specify the same behavior for a variety of events (i.e. the "copy" action when a user press "CTRL+C" on Windows or "COMMAND+C" on Mac).

Annotations
@SafeVarargs
dragDetectedback to summary
public static EventPattern<Event, MouseEvent> dragDetected()
eventTypeback to summary
public static <T extends Event> EventPattern<Event, T> eventType(EventType<? extends T> eventType)
getEventTypesback to summary
public Set<EventType<? extends U>> getEventTypes()
keyPressedback to summary
public static EventPattern<Event, KeyEvent> keyPressed()
keyPressedback to summary
public static EventPattern<Event, KeyEvent> keyPressed(KeyCombination combination)
keyPressedback to summary
public static EventPattern<Event, KeyEvent> keyPressed(KeyCode code, KeyCombination.Modifier... modifiers)
keyPressedback to summary
public static EventPattern<Event, KeyEvent> keyPressed(Predicate<KeyCode> keyTest, KeyCombination.Modifier... modifiers)
keyPressedback to summary
public static EventPattern<Event, KeyEvent> keyPressed(String character, KeyCombination.Modifier... modifiers)
keyPressedNoModback to summary
public static EventPattern<Event, KeyEvent> keyPressedNoMod(String character)

Matches the given key pressed event regardless of modifiers; this should only be used for the rare KeyEvents which require a pressed modifier (e.g. Shift) to generate it (e.g. "{"). If passed in a regular character (e.g. "a") and this appears before another EventPattern (e.g. keyPressed("a", SHORTCUT_DOWN)) in an InputMap#sequence(InputMap[]), the second EventPattern will never run.

keyReleasedback to summary
public static EventPattern<Event, KeyEvent> keyReleased()
keyReleasedback to summary
public static EventPattern<Event, KeyEvent> keyReleased(KeyCombination combination)
keyReleasedback to summary
public static EventPattern<Event, KeyEvent> keyReleased(KeyCode code, KeyCombination.Modifier... modifiers)
keyReleasedback to summary
public static EventPattern<Event, KeyEvent> keyReleased(Predicate<KeyCode> keyTest, KeyCombination.Modifier... modifiers)
keyReleasedback to summary
public static EventPattern<Event, KeyEvent> keyReleased(String character, KeyCombination.Modifier... modifiers)
keyReleasedNoModback to summary
public static EventPattern<Event, KeyEvent> keyReleasedNoMod(String character)

Matches the given key released event regardless of modifiers; this should only be used for the rare KeyEvents which require a pressed modifier (e.g. Shift) to generate it (e.g. "{"). If passed in a regular character (e.g. "a") and this appears before another EventPattern (e.g. keyReleased("a", SHORTCUT_DOWN)) in an InputMap#sequence(InputMap[]), the second EventPattern will never run.

keyTypedback to summary
public static EventPattern<Event, KeyEvent> keyTyped()
keyTypedback to summary
public static EventPattern<Event, KeyEvent> keyTyped(Predicate<String> charTest, KeyCombination.Modifier... modifiers)
keyTypedback to summary
public static EventPattern<Event, KeyEvent> keyTyped(String character, KeyCombination.Modifier... modifiers)
keyTypedNoModback to summary
public static EventPattern<Event, KeyEvent> keyTypedNoMod(String character)

Matches the given key typed event regardless of modifiers; this should only be used for the rare KeyEvents which require a pressed modifier (e.g. Shift) to generate it (e.g. "{"). If passed in a regular character (e.g. "a") and this appears before another EventPattern (e.g. keyTyped("a", SHORTCUT_DOWN)) in an InputMap#sequence(InputMap[]), the second EventPattern will never run.

matchback to summary
public Optional<? extends U> match(T event)

Returns a non-empty Optional when a match is found.

mouseClickedback to summary
public static EventPattern<Event, MouseEvent> mouseClicked()
mouseClickedback to summary
public static EventPattern<Event, MouseEvent> mouseClicked(MouseButton button)
mouseDraggedback to summary
public static EventPattern<Event, MouseEvent> mouseDragged()
mouseEnteredback to summary
public static EventPattern<Event, MouseEvent> mouseEntered()
mouseEnteredTargetback to summary
public static EventPattern<Event, MouseEvent> mouseEnteredTarget()
mouseExitedback to summary
public static EventPattern<Event, MouseEvent> mouseExited()
mouseExitedTargetback to summary
public static EventPattern<Event, MouseEvent> mouseExitedTarget()
mouseMovedback to summary
public static EventPattern<Event, MouseEvent> mouseMoved()
mousePressedback to summary
public static EventPattern<Event, MouseEvent> mousePressed()
mousePressedback to summary
public static EventPattern<Event, MouseEvent> mousePressed(MouseButton button)
mouseReleasedback to summary
public static EventPattern<Event, MouseEvent> mouseReleased()
mouseReleasedback to summary
public static EventPattern<Event, MouseEvent> mouseReleased(MouseButton button)
onlyIfback to summary
public default EventPattern<T, U> onlyIf(Predicate<? super U> condition)

Returns an EventPattern that matches the given event type only if this event pattern matches it and the event type passed the given condition

unlessback to summary
public default EventPattern<T, U> unless(Predicate<? super U> condition)

Returns an EventPattern that matches the given event type only if this event pattern matches it and the event type fails the given condition