简体   繁体   中英

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.

Please go through the documentation carefully, they are full of examples !

As Matt has suggested you must go through the Handling JavaFX Events section carefully.

If you are just looking for an example you must try the KeyboardExample

NB If you are facing issues trying to implement it, please post the code which you have tried !

Alternatively, you could try KeyCombination present in JavaFX. Below is a link to KeyCombination documentation: KeyCombination Simple example is as follows:

@FXML
MenuItem menuItem;

menuItem.setAccelerator(new KeyCodeCombination(KeyCode.M, KeyCombination.CONTROL_DOWN));

I hope this would help.

This is how the keyboard is supposed to work.

https://wiki.openjdk.java.net/display/OpenJFX/Keyboard+Navigation

And I think you might mean something called mnemonics

http://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/Mnemonic.html

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM