Search code examples
javakeyboardjavafxscene

How do you add keyboard commands to a preexisting javafx scene?


I have been trying to find out how to add keyboard commands to a preexisting javafx scene and i can not seem to find the answer anywhere. Want to have a keyboard method run with a key is pressed. He is what i would like to add the keyboard commands/controls to.

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;


    public class Main extends Application {
public void start(Stage primaryStage) {
    try {
        Parent root =    FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
        Scene scene = new Scene(root,600,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Keyboard test app");
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}
public void key(){

}
}

This would really help, thanks.


Solution

  • After about a week i figured it out! You need to add a textField and use that to colect keyboard data. Here is the code:

    Main:

    package application;
    
    import java.io.IOException;
    
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Group;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;
    import javafx.scene.input.KeyEvent;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
    @FXML
    TextField textBox;
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    public void start(Stage primaryStage){
            try {
                Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
                Scene scene = new Scene(root,600,400);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setResizable(false);
                primaryStage.setScene(scene);
                primaryStage.setTitle("Keyboard test app");
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
    }
    }
    

    Main Control:

    package application;
    
    import java.net.URL;
    import java.util.ResourceBundle;
    
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.shape.Polygon;
    
        public class MainControl implements Initializable {
    @FXML
    Polygon player_ship;
    @FXML
    TextField textBox;
    public void initialize(URL arg0, ResourceBundle arg1) {
     textBox.setPromptText("Write here");
    
        textBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                System.out.println("Key Pressed: " + ke.getText());
            }
        });
    
        textBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                System.out.println("Key Released: " + ke.getText());
        if(ke.getText().equalsIgnoreCase("b")){
                    System.out.println("Yay!");
        //What you want the computer to do goes here!
                }
            }
        });
    }
    

    } The applictation.css can be anything and the main.fxml just has t have a text field everywhere you want keyboard commands is enabled. This code was adapted from: http://docs.oracle.com/javafx/2/events/convenience_methods.htm