Java Code Examples for javafx.scene.Scene#addEventHandler()

The following examples show how to use javafx.scene.Scene#addEventHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainApp.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
private void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new CustomFXMLLoader(MainApp.class.getResource("fxml/Root.fxml"));
        rootLayout = loader.load();

        menuController = loader.getController();
        menuController.setMainApp(this);

        scene = new Scene(rootLayout);
        scene.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
            if(event.isAltDown())
                event.consume();
        });
        primaryStage.setScene(scene);
        primaryStage.show();
        loadCombinason();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 2
Source File: Main.java    From training with MIT License 6 votes vote down vote up
private void addKeyHandler(Scene scene, SpriteView.Shepherd mary) {
    scene.addEventHandler(KeyEvent.KEY_PRESSED, ke -> {
        KeyCode keyCode = ke.getCode();
        switch (keyCode) {
            case W:
            case UP:
                mary.move(Direction.UP);
                break;
            case A:
            case LEFT:
                mary.move(Direction.LEFT);
                break;
            case S:
            case DOWN:
                mary.move(Direction.DOWN);
                break;
            case D:
            case RIGHT:
                mary.move(Direction.RIGHT);
                break;
            case ESCAPE:
                Platform.exit();
        }
    });
}
 
Example 3
Source File: PeerInfoWithTagEditor.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void onShow() {
    super.display();

    if (stage != null) {
        focusListener = (observable, oldValue, newValue) -> {
            if (!newValue)
                hide();
        };
        stage.focusedProperty().addListener(focusListener);

        Scene scene = stage.getScene();
        if (scene != null)
            scene.addEventHandler(KeyEvent.KEY_RELEASED, keyEventEventHandler);
    }
}
 
Example 4
Source File: Main.java    From quantumjava with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addKeyHandler(Scene scene, SpriteView.Shepherd mary) {
    scene.addEventHandler(KeyEvent.KEY_PRESSED, ke -> {
        KeyCode keyCode = ke.getCode();
        switch (keyCode) {
            case W:
            case UP:
                mary.move(Direction.UP);
                break;
            case A:
            case LEFT:
                mary.move(Direction.LEFT);
                break;
            case S:
            case DOWN:
                mary.move(Direction.DOWN);
                break;
            case D:
            case RIGHT:
                mary.move(Direction.RIGHT);
                break;
            case X:
                chickenCoop.toggleActive();
                break;
            case H:
                nest.toggleActive();
                break;
            case C:
                rainbow.toggleActive();
                break;
            case ESCAPE:
                Platform.exit();
        }
    });
}
 
Example 5
Source File: PlaceholderStageGameMode.java    From Path-of-Leveling with MIT License 5 votes vote down vote up
public void loadSettings(){
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/poe/level/fx/preferences.fxml"));
    AnchorPane ap = null;
    try {
        ap = loader.load();
    } catch (IOException ex) {
        Logger.getLogger(ZoneOverlay_Stage.class.getName()).log(Level.SEVERE, null, ex);
    }
    controller = loader.<Preferences_Controller>getController();
    controller.hookGameModeOn(parent_gameModeOn);
    Scene scene = new Scene(ap);
    scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.ESCAPE) {
                close();
            }
        }
    });
    scene.getStylesheets().add(getClass().getResource("/styles/style.css").toExternalForm());

    this.setScene(scene);
    //this.setAlwaysOnTop(true);
    this.setOnCloseRequest(event -> {
        this.hide();
    });

    this.show();
}
 
Example 6
Source File: UI.java    From HubTurbo with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void setupGlobalKeyboardShortcuts(Scene scene) {
    globalHotkey.init();
    scene.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
        if (SHOW_ISSUE_PICKER.match(event)) {
            triggerEvent(new ShowIssuePickerEvent(logic.getModels().getIssues(), true));
        }
    });
}
 
Example 7
Source File: EventStreams.java    From ReactFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <T extends Event> EventStream<T> eventsOf(
        Scene scene, EventType<T> eventType) {
    return new EventStreamBase<T>() {
        @Override
        protected Subscription observeInputs() {
            EventHandler<T> handler = this::emit;
            scene.addEventHandler(eventType, handler);
            return () -> scene.removeEventHandler(eventType, handler);
        }
    };
}
 
Example 8
Source File: GroupVsPaneDemo.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void start(Stage primaryStage)
{
    Pane pane = new Pane();
    Group group = new Group();

    VBox.setVgrow(group, Priority.NEVER);
    VBox.setVgrow(pane, Priority.NEVER);

    VBox vbox = new VBox(group, pane);

    Rectangle rect1 = new Rectangle(100, 100, 100, 100);
    Rectangle rect2 = new Rectangle(100, 100, 100, 100);
    Rectangle rect3 = new Rectangle(200, 200, 100, 100);
    Rectangle rect4 = new Rectangle(200, 200, 100, 100);
    rect1.setFill(Color.BLUE);
    rect2.setFill(Color.BLUE);
    rect3.setFill(Color.GREEN);
    rect4.setFill(Color.GREEN);

    group.getChildren().addAll(rect1, rect3);
    pane.getChildren().addAll(rect2, rect4);

    Scene scene = new Scene(vbox, 800, 800);
    scene.addEventHandler(KeyEvent.KEY_PRESSED, e ->
    {
        double deltaX ;
        switch(e.getCode())
        {
            case LEFT:
                deltaX = -10 ;
                break ;
            case RIGHT:
                deltaX = 10 ;
                break ;
            default:
                deltaX = 0 ;
        }
        rect3.setX(rect3.getX() + deltaX);
        rect4.setX(rect4.getX() + deltaX);
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}
 
Example 9
Source File: GroupVsPaneDemo.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void start(Stage primaryStage) {
    Pane pane = new Pane();
    Group group = new Group();

    VBox.setVgrow(group, Priority.NEVER);
    VBox.setVgrow(pane, Priority.NEVER);

    VBox vbox = new VBox(group, pane);


    Rectangle rect1 = new Rectangle(100, 100, 100, 100);
    Rectangle rect2 = new Rectangle(100, 100, 100, 100);
    Rectangle rect3 = new Rectangle(200, 200, 100, 100);
    Rectangle rect4 = new Rectangle(200, 200, 100, 100);
    rect1.setFill(Color.BLUE);
    rect2.setFill(Color.BLUE);
    rect3.setFill(Color.GREEN);
    rect4.setFill(Color.GREEN);

    group.getChildren().addAll(rect1, rect3);
    pane.getChildren().addAll(rect2, rect4);

    Scene scene = new Scene(vbox, 800, 800);
    scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
        double deltaX ;
        switch(e.getCode()) {
            case LEFT:
                deltaX = -10 ;
                break ;
            case RIGHT:
                deltaX = 10 ;
                break ;
            default:
                deltaX = 0 ;
        }
        rect3.setX(rect3.getX() + deltaX);
        rect4.setX(rect4.getX() + deltaX);
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}
 
Example 10
Source File: MainWindow.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
MainWindow() {
	fileEditorTabPane = new FileEditorTabPane(this);
	fileEditorManager = new FileEditorManager(fileEditorTabPane);
	projectPane = new ProjectPane(fileEditorManager);

	Preferences state = MarkdownWriterFXApp.getState();
	double dividerPosition = state.getDouble("projectPaneDividerPosition", 0.2);

	SplitPane splitPane = new SplitPane(projectPane.getNode(), fileEditorTabPane.getNode()) {
		private int layoutCount;

		@Override
		protected void layoutChildren() {
			super.layoutChildren();
			if (layoutCount < 2) {
				layoutCount++;
				setDividerPosition(0, dividerPosition);
				super.layoutChildren();
			}
		}
	};
	SplitPane.setResizableWithParent(projectPane.getNode(), false);
	splitPane.setDividerPosition(0, dividerPosition);
	splitPane.getDividers().get(0).positionProperty().addListener((ob, o, n) -> {
		Utils.putPrefsDouble(state, "projectPaneDividerPosition", n.doubleValue(), 0.2);
	});

	BorderPane borderPane = new BorderPane();
	borderPane.getStyleClass().add("main");
	borderPane.setPrefSize(800, 800);
	borderPane.setTop(createMenuBarAndToolBar());
	borderPane.setCenter(splitPane);

	scene = new Scene(borderPane);
	scene.getStylesheets().add("org/markdownwriterfx/MarkdownWriter.css");
	scene.windowProperty().addListener((observable, oldWindow, newWindow) -> {
		newWindow.setOnCloseRequest(e -> {
			if (!fileEditorTabPane.canCloseAllEditos())
				e.consume();
		});

		// workaround for a bug in JavaFX: unselect menubar if window looses focus
		newWindow.focusedProperty().addListener((obs, oldFocused, newFocused) -> {
			if (!newFocused) {
				// send an ESC key event to the menubar
				menuBar.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED,
						KeyEvent.CHAR_UNDEFINED, "", KeyCode.ESCAPE,
						false, false, false, false));
			}
		});
	});

	Utils.fixSpaceAfterDeadKey(scene);

	// workaround for a bad JavaFX behavior: menu bar always grabs focus when ALT key is pressed,
	// but should grab it when ALT key is releases (as all other UI toolkits do) to give other
	// controls the chance to use Alt+Key shortcuts.
	scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
		if (e.isAltDown())
			e.consume();
	});

	// open markdown files dropped to main window
	scene.setOnDragOver(e -> {
		if (e.getDragboard().hasFiles())
			e.acceptTransferModes(TransferMode.COPY);
		e.consume();
	});
	scene.setOnDragDropped(e -> {
		boolean success = false;
		if (e.getDragboard().hasFiles()) {
			fileEditorTabPane.openEditors(e.getDragboard().getFiles(), 0, -1);
			success = true;
		}
		e.setDropCompleted(success);
		e.consume();
	});

	Platform.runLater(() -> stageFocusedProperty.bind(scene.getWindow().focusedProperty()));
}
 
Example 11
Source File: BisqApp.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
private void addSceneKeyEventHandler(Scene scene, Injector injector) {
    scene.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
        if (Utilities.isCtrlPressed(KeyCode.W, keyEvent) ||
                Utilities.isCtrlPressed(KeyCode.Q, keyEvent)) {
            shutDownByUser();
        } else {
            if (Utilities.isAltOrCtrlPressed(KeyCode.E, keyEvent)) {
                injector.getInstance(BtcEmptyWalletWindow.class).show();
            } else if (Utilities.isAltOrCtrlPressed(KeyCode.B, keyEvent)) {
                injector.getInstance(BsqEmptyWalletWindow.class).show();
            } else if (Utilities.isAltOrCtrlPressed(KeyCode.M, keyEvent)) {
                injector.getInstance(SendAlertMessageWindow.class).show();
            } else if (Utilities.isAltOrCtrlPressed(KeyCode.F, keyEvent)) {
                injector.getInstance(FilterWindow.class).show();
            } else if (Utilities.isAltOrCtrlPressed(KeyCode.H, keyEvent)) {
                log.warn("We re-published all proposalPayloads and blindVotePayloads to the P2P network.");
                injector.getInstance(MissingDataRequestService.class).reRepublishAllGovernanceData();
            } else if (Utilities.isAltOrCtrlPressed(KeyCode.T, keyEvent)) {
                // Toggle between show tor logs and only show warnings. Helpful in case of connection problems
                String pattern = "org.berndpruenster.netlayer";
                Level logLevel = ((Logger) LoggerFactory.getLogger(pattern)).getLevel();
                if (logLevel != Level.DEBUG) {
                    log.info("Set log level for org.berndpruenster.netlayer classes to DEBUG");
                    Log.setCustomLogLevel(pattern, Level.DEBUG);
                } else {
                    log.info("Set log level for org.berndpruenster.netlayer classes to WARN");
                    Log.setCustomLogLevel(pattern, Level.WARN);
                }
            } else if (Utilities.isAltOrCtrlPressed(KeyCode.J, keyEvent)) {
                WalletsManager walletsManager = injector.getInstance(WalletsManager.class);
                if (walletsManager.areWalletsAvailable())
                    new ShowWalletDataWindow(walletsManager).show();
                else
                    new Popup().warning(Res.get("popup.warning.walletNotInitialized")).show();
            } else if (Utilities.isAltOrCtrlPressed(KeyCode.G, keyEvent)) {
                if (injector.getInstance(BtcWalletService.class).isWalletReady())
                    injector.getInstance(ManualPayoutTxWindow.class).show();
                else
                    new Popup().warning(Res.get("popup.warning.walletNotInitialized")).show();
            } else if (DevEnv.isDevMode()) {
                if (Utilities.isAltOrCtrlPressed(KeyCode.Z, keyEvent))
                    showDebugWindow(scene, injector);
            }
        }
    });
}
 
Example 12
Source File: CameraController.java    From FXyzLib with GNU General Public License v3.0 4 votes vote down vote up
private void setEventHandlers(Scene scene) {
    scene.addEventHandler(KeyEvent.ANY, k -> handleKeyEvent(k));
    scene.addEventHandler(MouseEvent.ANY, m -> handleMouseEvent(m));
    scene.addEventHandler(ScrollEvent.ANY, s -> handleScrollEvent(s));
}
 
Example 13
Source File: ControllerImpl.java    From halfnes with GNU General Public License v3.0 4 votes vote down vote up
public ControllerImpl(final Scene scene, final int controllernum) {
    this(controllernum);
    scene.addEventHandler(javafx.scene.input.KeyEvent.KEY_PRESSED, e -> pressKey(e.getCode().impl_getCode()));
    scene.addEventHandler(javafx.scene.input.KeyEvent.KEY_RELEASED, e -> releaseKey(e.getCode().impl_getCode()));
}