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!");
}
Aggregations