0

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.

enter image description here

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

  • 2
    I am not sure I follow your question, but maybe [this](https://stackoverflow.com/questions/49564002/keycode-event-for-backspace-in-javafx/49575995#49575995) can help. – SedJ601 May 04 '21 at 07:16
  • 3
    After looking at your code, my guess is that your problem is due to using the same label to try to display two different `KeyEvents`. Maybe you need two `Labels`. `showKeyTyped` and `showKeyPressed`. You should also read the link I posted above. – SedJ601 May 04 '21 at 07:21
  • 2
    @Sedrick is correct. The `keyTyped` event will be fired immediately after the `keyPressed` event, so you replace the text you passed to the label in `setOnKeyPressed` with the text you passed to the label in `setOnKeyTyped`. – James_D May 04 '21 at 12:23

0 Answers0