Search in sources :

Example 1 with InputMap

use of org.fxmisc.wellbehaved.event.InputMap in project RichTextFX by FXMisc.

the class OverrideBehaviorDemo method start.

@Override
public void start(Stage primaryStage) {
    InlineCssTextArea area = new InlineCssTextArea();
    InputMap<Event> preventSelectionOrRightArrowNavigation = InputMap.consume(anyOf(// prevent selection via (CTRL + ) SHIFT + [LEFT, UP, DOWN]
    keyPressed(LEFT, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(KP_LEFT, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(UP, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(KP_UP, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(DOWN, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(KP_DOWN, SHIFT_DOWN, SHORTCUT_ANY), // prevent selection via mouse events
    eventType(MouseEvent.MOUSE_DRAGGED), eventType(MouseEvent.DRAG_DETECTED), mousePressed().unless(e -> e.getClickCount() == 1 && !e.isShiftDown()), // prevent any right arrow movement, regardless of modifiers
    keyPressed(RIGHT, SHORTCUT_ANY, SHIFT_ANY), keyPressed(KP_RIGHT, SHORTCUT_ANY, SHIFT_ANY)));
    Nodes.addInputMap(area, preventSelectionOrRightArrowNavigation);
    area.replaceText(String.join("\n", "You can't move the caret to the right via the RIGHT arrow key in this area.", "Additionally, you cannot select anything either", "", ":-p"));
    area.moveTo(0);
    CheckBox addExtraEnterHandlerCheckBox = new CheckBox("Temporarily add an EventHandler to area's `onKeyPressedProperty`?");
    addExtraEnterHandlerCheckBox.setStyle("-fx-font-weight: bold;");
    Label checkBoxExplanation = new Label(String.join("\n", "The added handler will insert a newline character at the caret's position when [Enter] is pressed.", "If checked, the default behavior and added handler will both occur: ", "\tthus, two newline characters should be inserted when user presses [Enter].", "When unchecked, the handler will be removed."));
    checkBoxExplanation.setWrapText(true);
    EventHandler<KeyEvent> insertNewlineChar = e -> {
        if (e.getCode().equals(ENTER)) {
            area.insertText(area.getCaretPosition(), "\n");
            e.consume();
        }
    };
    addExtraEnterHandlerCheckBox.selectedProperty().addListener((obs, ov, isSelected) -> area.setOnKeyPressed(isSelected ? insertNewlineChar : null));
    VBox vbox = new VBox(area, addExtraEnterHandlerCheckBox, checkBoxExplanation);
    vbox.setSpacing(10);
    vbox.setPadding(new Insets(10));
    primaryStage.setScene(new Scene(vbox, 700, 350));
    primaryStage.show();
    primaryStage.setTitle("An area whose behavior has been overridden permanently and temporarily!");
}
Also used : KeyEvent(javafx.scene.input.KeyEvent) EventHandler(javafx.event.EventHandler) KeyCode(javafx.scene.input.KeyCode) Scene(javafx.scene.Scene) Label(javafx.scene.control.Label) InputMap(org.fxmisc.wellbehaved.event.InputMap) MouseEvent(javafx.scene.input.MouseEvent) Event(javafx.event.Event) CheckBox(javafx.scene.control.CheckBox) Nodes(org.fxmisc.wellbehaved.event.Nodes) KeyEvent(javafx.scene.input.KeyEvent) VBox(javafx.scene.layout.VBox) KeyCombination(javafx.scene.input.KeyCombination) Application(javafx.application.Application) Insets(javafx.geometry.Insets) Stage(javafx.stage.Stage) EventPattern(org.fxmisc.wellbehaved.event.EventPattern) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Insets(javafx.geometry.Insets) CheckBox(javafx.scene.control.CheckBox) Label(javafx.scene.control.Label) MouseEvent(javafx.scene.input.MouseEvent) Event(javafx.event.Event) KeyEvent(javafx.scene.input.KeyEvent) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Scene(javafx.scene.Scene) VBox(javafx.scene.layout.VBox)

Aggregations

Application (javafx.application.Application)1 Event (javafx.event.Event)1 EventHandler (javafx.event.EventHandler)1 Insets (javafx.geometry.Insets)1 Scene (javafx.scene.Scene)1 CheckBox (javafx.scene.control.CheckBox)1 Label (javafx.scene.control.Label)1 KeyCode (javafx.scene.input.KeyCode)1 KeyCombination (javafx.scene.input.KeyCombination)1 KeyEvent (javafx.scene.input.KeyEvent)1 MouseEvent (javafx.scene.input.MouseEvent)1 VBox (javafx.scene.layout.VBox)1 Stage (javafx.stage.Stage)1 InlineCssTextArea (org.fxmisc.richtext.InlineCssTextArea)1 EventPattern (org.fxmisc.wellbehaved.event.EventPattern)1 InputMap (org.fxmisc.wellbehaved.event.InputMap)1 Nodes (org.fxmisc.wellbehaved.event.Nodes)1