How do I handle keypresses simultaneously in JavaFX?

Viewed 47

I'm trying to make a Bomber-Man game. I'm using javafx for it but there is a problem I faced:

I'm trying to implement planting bombs which you should press "SPACE" and it will wait for a certain time and then it will explode.
But I tried to implement it using JavaFX's "setOnKeyPressed()" method but when i try to put thread.sleep() for handling of Pressing "SPACE", from that moment it will not handle any other Key Presses.

e.g: I press "SPACE" to plant the bomb and I want to escape from it but when I press any of the movement keys the player doesn't move which I understand that it is still sleeping in the bomb handling method.
Here is a simple demonstration of what I want to do:

@Override
public void start(Stage primaryStage) throws Exception {
    Pane pane = new Pane();
    Scene scene = new Scene(pane);

    scene.setOnKeyPressed(event -> {
        if (event.getCode() == KeyCode.UP) {
            player.move(Direction.UP);
        }
        if (event.getCode() == KeyCode.SPACE) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}
0 Answers
Related