KeyEvent Problem: I am trying to print "Hello" whenever I press "A", however the console doesn't show up anything

Viewed 43

I am trying to print "Hello" whenever I press "A", however the console doesn't show up anything. I have found many ways to fix this but couldn't. Can you guys help me?

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        Group root = new Group();
        Scene scene = new Scene(root);

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent event) {

                switch(event.getCode()) {
                    case A:
                        System.out.println("Hello");
                }
            }
        });

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
0 Answers
Related