简体   繁体   中英

Java FX Scene Builder How to make event key pressed

I'm new in Java and Java FX and I'm trying to make a panel with buttons using scene builder. I want my application to respond only on arrow key pressed. I made the following method in my Controller class:

public void keyPressed(KeyEvent key) {
    switch(key.getCode()) {
        ...some code here
    }
} 

After that I selected this method in scene builder, but when I run my application nothing happens when I press an arrow key. Can somebody help me?

Without seeing the rest of your code and FXML it is difficult to tell, here is full example

Possible things you missed

  • Adding keyPress as an action in the FXML
  • Adding the @FXML annotation to the keyPressed() method

Code

public class Main extends Application {

    private class Controller {
        @FXML  // <== perhaps you had this missing??
        void keyPressed(KeyEvent event) {
            switch (event.getCode()) {
            case LEFT:
            case KP_LEFT:
                System.out.println("to the left");
                break;
            case RIGHT:
            case KP_RIGHT:
                System.out.println("to the right");
                break;
            default:
                break;
            }
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/foo.fxml"));
        loader.setController(new Controller());
        primaryStage.setScene(new Scene(loader.load()));
        primaryStage.show();
    }

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

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane onKeyPressed="#keyPressed" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
   <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
   </rowConstraints>
   <children>
      <Button mnemonicParsing="false" text="Button" />
   </children>
</GridPane>

KeyCode also allows to compare with a specific key

@FXML
private void keyPressed(KeyEvent keyEvent)
    if (keyEvent.getCode() == KeyCode.ENTER) {
        // do some actions 
    }
}

You can get all the key code from here . Without using switch it's one of the good way.

KeyCode has a method isArrowKey() , so if you're calling your keyPressed method from your event handler, you could do:

public void keyPressed(KeyEvent key){
    if(key.getCode().isArrowKey()){
        ...some code here
    }
}

If you need to do different things based on which arrow key is pressed, make sure your switch cases are comparing to KeyCode.UP / DOWN / LEFT / RIGHT . If they are, it's likely that either you didn't set the event handler properly or your GUI is hung because of a threading problem. Post where your handling the event if you need more help.

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