javajavafxkeyboardkeyevent

Why are my KeyCode/KeyEvent functions returning either as an invalid character or "UNDEFINDED"? (JavaFX)


I am very new to JavaFX, and I am trying to work with KeyEvents and KeyCodes. Researching on how to use these properly, I looked at this webpage for how to do that.

I (somewhat) copied it's code to try to implement it to my program. Mainly what I am trying to do is to make it where when the user presses F11, it will go fullscreen. It works, but what I don't understand is that when it is both typed and pressed, it either returns as an unreconizable character, or it just says UNDEFINED.

I feel like that I will run into this problem at some point when I try to use these methods. The question is: Why are my KeyCode/KeyEvent functions returning either as an invalid character or "UNDEFINDED"?

Screenshot of the results

results

Code

onKeyTyped()

static void onKeyTyped(KeyEvent evt) {
    String k = evt.getCharacter() ;
    System.out.println(k + " is typed") ; // What prints when it is typed

/*
    switch (k) {
        case "f" -> {
            isFullscreen = !isFullscreen ;
        }
    }
*/
}

onKeyPressed()

static void onKeyPressed(KeyEvent evt) {
    KeyCode key = evt.getCode() ;
    System.out.println(key + " is pressed") ;
}

onKeyReleased()

static void onKeyReleased(KeyEvent evt) {
    KeyCode key = evt.getCode() ;
    System.out.println(key + " is released") ;

    if (key == KeyCode.F11) {
        stage.setFullScreen(!isFullscreen) ;
    }
}

Implentations to the stage

@Override
public void start(Stage primaryStage) {
    Image icon = new Image("C:\\Users\\8018156\\IdeaProjects\\Galactify\\src\\main\\java\\com\\galactify\\tiles\\gicon.png") ;

    stage = primaryStage ;
    stage.setTitle("Galactify - 0.0.1") ;
    stage.getIcons().add(icon) ;
    stage.setScene(scene);
    stage.setFullScreenExitHint("") ;

    scene.setOnKeyPressed(e -> onKeyTyped(e)) ;
    scene.setOnKeyTyped(e -> onKeyPressed(e)) ;
    scene.setOnKeyReleased(e -> onKeyReleased(e)) ;

    stage.show();
}

Class attributes

public static Stage stage ;
static boolean isFullscreen = false ;
static FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("demo-view.fxml")) ;
static Scene scene ;

static {
    try {
        scene = new Scene(fxmlLoader.load(), 800, 400);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Solution

  • The results are weird because you accidentally switched the onKeyPressed() and onKeyTyped() methods. To fix this, just simply set the start() method as shown:

    @Override
    public void start(Stage primaryStage) {
        Image icon = new Image("C:\\Users\\8018156\\IdeaProjects\\Galactify\\src\\main\\java\\com\\galactify\\tiles\\gicon.png") ;
    
        stage = primaryStage ;
        stage.setTitle("Galactify - 0.0.1") ;
        stage.getIcons().add(icon) ;
        stage.setScene(scene);
        stage.setFullScreenExitHint("") ;
    
        scene.setOnKeyPressed(e -> onKeyPressed(e)) ; // Where you accidentally
        scene.setOnKeyTyped(e -> onKeyTyped(e)) ; // switched the methods
        scene.setOnKeyReleased(e -> onKeyReleased(e)) ;
    
        stage.show();
    }