I am learning JavaFX and following a tutorial. When I press ENTER, SPACE or COMMA the program doesn't detect the button pressed for SPACE, ENTER and COMMA (so doesn't display its right message) but goes in setOnKeyTyped.
I do not see what is wrong here.In the picture just below I have pressed COMMA.
package com.mathieuascain;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application {
Label prompt;
Label showKey;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Handle KeyBoard events");
FlowPane rootNode = new FlowPane(Orientation.VERTICAL, 0, 10);
rootNode.setAlignment(Pos.CENTER);
Scene scene = new Scene(rootNode, 300, 100);
primaryStage.setScene(scene);
prompt = new Label("Press a key");
showKey = new Label("");
scene.setOnKeyTyped(keyEvent -> showKey.setText("You typed : " + keyEvent.getCharacter()));
scene.setOnKeyPressed(ke -> {
switch (ke.getCode()) {
case RIGHT:
showKey.setText("You pressed right arrow");
break;
case LEFT:
showKey.setText("You pressed left arrow");
break;
case DOWN:
showKey.setText("You pressed down arrow");
case UP:
showKey.setText("You pressed up arrow");
break;
case F10:
showKey.setText("You pressed F10");
break;
case ALT:
showKey.setText("You pressed ALT");
break;
case ENTER:
showKey.setText("You pressed ENTER");
break;
case SPACE:
showKey.setText("You pressed SPACE");
break;
case COMMA:
showKey.setText("You pressed COMMA");
break;
}
});
rootNode.getChildren().addAll(prompt, showKey);
primaryStage.show();
}
}
Thank you in advance