Top Description Inners Fields Methods
org.fxmisc.wellbehaved.event

public Interface InputMap<E extends Event>

Additional top-level classes in compilation unit: PatternActionMap, InputMapChain.

Known Direct Implementers
org.fxmisc.wellbehaved.event.PatternActionMap, org.fxmisc.wellbehaved.event.InputMapChain, org.fxmisc.wellbehaved.event.template.InputMapTemplateInstance
Annotations
@FunctionalInterface
Type Parameters
<E>
type of events that this InputMap may handle. That is, InputMap<E> certainly does not handle any events that are not of type E; it does not mean it handles any event of type E.
Imports
java.util.Arrays, .Objects, java.util.function.BooleanSupplier, .Consumer, .Function, java.util.stream.Stream, javafx.event.Event, .EventType, org.fxmisc.wellbehaved.event.InputHandler.Result

Pattern matching for Events.

General Concept as an Analogy

Think of an InputMap as a powerful switch statement:

switch (eventType) {
    case KEY_PRESSED:    // the basic EventPattern
         doCodeWithEvent(e); // the basic InputHandler
         break;

    // an EventPattern with a condition
    // it only matches KeyTyped events whose text is a space character
    // other KeyTyped events don't run the space insertion code beow
    case KEY_TYPED e if e.getText().equals(" "):
         addSpaceToText();
         break;

    // an EventPattern with another condition
    case MOUSE_MOVED && otherNode.isVisible():
         otherNode.hide();
         thirdNode.show();
         break;

    // runs the same behavior for multiple event types
    case MOUSE_PRESSED || MOUSE_RELEASED || MOUSE_DRAGGED || MOUSE_ENTERED_TARGET:
         showPopupThatSays("You did something with the mouse!")

    // sometimes there is no "default" behavior and the event bubbles back up
    default:
         break;
}

Types of InputMaps

There are a few types of InputMaps:

Examples


InputMap<KeyEvent> typeTheLetter = consume(
     // event pattern (e.g. the "case" line in a switch statement)
     keyTyped(),

     // input handler (e.g. the "block of code" for a case in a switch statement
     e -> textField.addLetter(e.getText)
);

InputMap<KeyEvent> copy = consume(
     anyOf(
         keyPressed(KeyCode.C, KeyCode.SHORTCUT),
         keyPressed(KeyCode.COPY)
     ),
     e -> textField.copyToClipboard()
);

InputMap<KeyEvent> pasteThenClearTextField = consume(
     // event pattern
     anyOf(
         keyPressed(KeyCode.V, KeyCode.SHORTCUT),
         keyPressed(KeyCode.PASTE)
     ),
     e -> textField.copyToClipboard()
).ifConsumed(ignoreMe -> textField.clearText();

InputMap<CustomEvent> someCustomEventInputMap = // imagine I put something here...

// notice the very generic "Event" event type
InputMap<? super Event> allBehaviorForCustomTextField = sequence(
     // when an event occurs, check whether the below input map's EventPattern matches the given event
     typeTheLetter,
     // only pattern match against the below input map if the previous one didn't match
     copy,
     // check this one only if the above two didn't match
     pasteThenClearTextField,

     someCustomEventInputMap

     // If reach this part, none of them matched, so do nothing
);

// at this point, "allBehaviorForCustomTextField" could be installed into a Node
Nodes.addFallbackInputMap(node, allBehaviorForCustomTextField);

// when user types a letter, the text field adds that letter
// when user moves the mouse over the text field, nothing happens

Last Words

When creating a subclass of a node that will be instantiated multiple times, use org.fxmisc.wellbehaved.event.template.InputMapTemplate to create an InputMap for each created javafx.scene.Node as this saves some memory. See the template class' javadoc for more details.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Field Summary

Modifier and TypeField and Description
public static final InputMap<?>

Method Summary

Modifier and TypeMethod and Description
public static <T extends Event, U extends T> InputMap<U>
consume(EventPattern<? super T, ? extends U> eventPattern, Consumer<? super U> action)

If the given EventPattern matches the given event type, runs the given action, consumes the event, and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event> InputMap<T>
consume(EventType<? extends T> eventType, Consumer<? super T> action)

When the given event type occurs, runs the given action, consumes the event, and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event, U extends T> InputMap<U>
consume(EventPattern<? super T, ? extends U> eventPattern)

If the given EventPattern matches the given event type, consumes the event and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event> InputMap<T>
consume(EventType<? extends T> eventType)

When the given event type occurs, consumes the event and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event, U extends T> InputMap<U>
consumeUnless(EventPattern<? super T, ? extends U> eventPattern, BooleanSupplier condition, Consumer<? super U> action)

If the given EventPattern matches the given event type and condition is false, consumes the event and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event> InputMap<T>
consumeUnless(EventType<? extends T> eventType, BooleanSupplier condition, Consumer<? super T> action)

When the given event type occurs and condition is false, consumes the event and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event, U extends T> InputMap<U>
consumeWhen(EventPattern<? super T, ? extends U> eventPattern, BooleanSupplier condition, Consumer<? super U> action)

If the given EventPattern matches the given event type and condition is true, consumes the event and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event> InputMap<T>
consumeWhen(EventType<? extends T> eventType, BooleanSupplier condition, Consumer<? super T> action)

When the given event type occurs and condition is true, consumes the event and does not attempt to match additional InputMaps (if they exist).

public static <E extends Event> InputMap<E>
public void
forEachEventType(InputMap.HandlerConsumer<? super E> f)

For each EventPattern that matches the given event, run the corresponding InputHandler

public default InputMap<E>
ifConsumed(Consumer<? super E> postConsumption)

Executes some additional handler if the event was consumed (e.g. InputHandler#process(Event) returns Result#CONSUME).

public default InputMap<E>
ifIgnored(Consumer<? super E> postIgnore)

Executes some additional handler if the event was ignored (e.g. InputHandler#process(Event) returns Result#IGNORE).

public default InputMap<E>
ifProcessed(Consumer<? super E> postProceed)

Executes some additional handler if the event was matched but not consumed (e.g. InputHandler#process(Event) returns Result#PROCEED).

public static <T extends Event, U extends T> InputMap<U>
ignore(EventPattern<? super T, ? extends U> eventPattern)

If the given EventPattern matches the given event type, does nothing and does not attempt to match additional InputMaps (if they exist).

public static <T extends Event> InputMap<T>
ignore(EventType<? extends T> eventType)

When the given event type occurs, does nothing and does not attempt to match additional InputMaps (if they exist).

public default InputMap<E>
orElse(InputMap<? extends E> that)

Shorthand for sequence(this, that)

public static <E extends Event> InputMap<E>
postResult(InputMap<? extends E> map, InputHandler.Result checkedResult, Consumer<? super E> postDesiredResult)

public static <T extends Event, U extends T> InputMap<U>
process(EventPattern<? super T, ? extends U> eventPattern, Function<? super U, InputHandler.Result> action)

If the given EventPattern matches the given event type, runs the given action, and then attempts to pattern match the event type with the next InputMap (if one exists).

public static <T extends Event> InputMap<T>
process(EventType<? extends T> eventType, Function<? super T, InputHandler.Result> action)

When the given event type occurs, runs the given action, and then attempts to pattern match the event type with the next InputMap (if one exists).

public static <E extends Event> InputMap<E>
sequence(InputMap<? extends E>... inputMaps)

Creates a single InputMap that pattern matches a given event type against all the given input maps.

public static <T extends Event> InputMap<T>
unless(BooleanSupplier condition, InputMap<T> im)

When the given condition is false, pattern matches the event with the given InputMap or proceeds to the next InputMap (if it exists).

public static <E extends Event> InputMap<E>
upCast(InputMap<? extends E> inputMap)

public static <T extends Event> InputMap<T>
when(BooleanSupplier condition, InputMap<T> im)

When the given condition is true, pattern matches the event with the given InputMap or proceeds to the next InputMap (if it exists).

public default InputMap<E>
without(InputMap<?> that)

Returns an InputMap that does nothing when this.equals(that); otherwise, returns this input map.

Field Detail

EMPTYback to summary
public static final InputMap<?> EMPTY

Method Detail

consumeback to summary
public static <T extends Event, U extends T> InputMap<U> consume(EventPattern<? super T, ? extends U> eventPattern, Consumer<? super U> action)

If the given EventPattern matches the given event type, runs the given action, consumes the event, and does not attempt to match additional InputMaps (if they exist).

consumeback to summary
public static <T extends Event> InputMap<T> consume(EventType<? extends T> eventType, Consumer<? super T> action)

When the given event type occurs, runs the given action, consumes the event, and does not attempt to match additional InputMaps (if they exist).

consumeback to summary
public static <T extends Event, U extends T> InputMap<U> consume(EventPattern<? super T, ? extends U> eventPattern)

If the given EventPattern matches the given event type, consumes the event and does not attempt to match additional InputMaps (if they exist).

consumeback to summary
public static <T extends Event> InputMap<T> consume(EventType<? extends T> eventType)

When the given event type occurs, consumes the event and does not attempt to match additional InputMaps (if they exist).

consumeUnlessback to summary
public static <T extends Event, U extends T> InputMap<U> consumeUnless(EventPattern<? super T, ? extends U> eventPattern, BooleanSupplier condition, Consumer<? super U> action)

If the given EventPattern matches the given event type and condition is false, consumes the event and does not attempt to match additional InputMaps (if they exist). If condition is true, continues to try to pattern match the event type with the next InputMap (if one exists).

consumeUnlessback to summary
public static <T extends Event> InputMap<T> consumeUnless(EventType<? extends T> eventType, BooleanSupplier condition, Consumer<? super T> action)

When the given event type occurs and condition is false, consumes the event and does not attempt to match additional InputMaps (if they exist). If condition is true, continues to try to pattern match the event type with the next InputMap (if one exists).

consumeWhenback to summary
public static <T extends Event, U extends T> InputMap<U> consumeWhen(EventPattern<? super T, ? extends U> eventPattern, BooleanSupplier condition, Consumer<? super U> action)

If the given EventPattern matches the given event type and condition is true, consumes the event and does not attempt to match additional InputMaps (if they exist).

consumeWhenback to summary
public static <T extends Event> InputMap<T> consumeWhen(EventType<? extends T> eventType, BooleanSupplier condition, Consumer<? super T> action)

When the given event type occurs and condition is true, consumes the event and does not attempt to match additional InputMaps (if they exist).

emptyback to summary
public static <E extends Event> InputMap<E> empty()
forEachEventTypeback to summary
public void forEachEventType(InputMap.HandlerConsumer<? super E> f)

For each EventPattern that matches the given event, run the corresponding InputHandler

ifConsumedback to summary
public default InputMap<E> ifConsumed(Consumer<? super E> postConsumption)

Executes some additional handler if the event was consumed (e.g. InputHandler#process(Event) returns Result#CONSUME).

ifIgnoredback to summary
public default InputMap<E> ifIgnored(Consumer<? super E> postIgnore)

Executes some additional handler if the event was ignored (e.g. InputHandler#process(Event) returns Result#IGNORE).

ifProcessedback to summary
public default InputMap<E> ifProcessed(Consumer<? super E> postProceed)

Executes some additional handler if the event was matched but not consumed (e.g. InputHandler#process(Event) returns Result#PROCEED).

ignoreback to summary
public static <T extends Event, U extends T> InputMap<U> ignore(EventPattern<? super T, ? extends U> eventPattern)

If the given EventPattern matches the given event type, does nothing and does not attempt to match additional InputMaps (if they exist).

ignoreback to summary
public static <T extends Event> InputMap<T> ignore(EventType<? extends T> eventType)

When the given event type occurs, does nothing and does not attempt to match additional InputMaps (if they exist).

orElseback to summary
public default InputMap<E> orElse(InputMap<? extends E> that)

Shorthand for sequence(this, that)

postResultback to summary
public static <E extends Event> InputMap<E> postResult(InputMap<? extends E> map, InputHandler.Result checkedResult, Consumer<? super E> postDesiredResult)
processback to summary
public static <T extends Event, U extends T> InputMap<U> process(EventPattern<? super T, ? extends U> eventPattern, Function<? super U, InputHandler.Result> action)

If the given EventPattern matches the given event type, runs the given action, and then attempts to pattern match the event type with the next InputMap (if one exists).

processback to summary
public static <T extends Event> InputMap<T> process(EventType<? extends T> eventType, Function<? super T, InputHandler.Result> action)

When the given event type occurs, runs the given action, and then attempts to pattern match the event type with the next InputMap (if one exists).

sequenceback to summary
public static <E extends Event> InputMap<E> sequence(InputMap<? extends E>... inputMaps)

Creates a single InputMap that pattern matches a given event type against all the given input maps. This is often the InputMap installed on a given node since it contains all the other InputMaps.

Annotations
@SafeVarargs
unlessback to summary
public static <T extends Event> InputMap<T> unless(BooleanSupplier condition, InputMap<T> im)

When the given condition is false, pattern matches the event with the given InputMap or proceeds to the next InputMap (if it exists).

upCastback to summary
public static <E extends Event> InputMap<E> upCast(InputMap<? extends E> inputMap)
whenback to summary
public static <T extends Event> InputMap<T> when(BooleanSupplier condition, InputMap<T> im)

When the given condition is true, pattern matches the event with the given InputMap or proceeds to the next InputMap (if it exists).

withoutback to summary
public default InputMap<E> without(InputMap<?> that)

Returns an InputMap that does nothing when this.equals(that); otherwise, returns this input map.

org.fxmisc.wellbehaved.event back to summary

public Interface InputMap.HandlerConsumer<E extends Event>

Annotations
@FunctionalInterface

Method Summary

Modifier and TypeMethod and Description
public <F extends E> void
accept(EventType<? extends F> t, InputHandler<? super F> h)

Method Detail

acceptback to summary
public <F extends E> void accept(EventType<? extends F> t, InputHandler<? super F> h)