如何在 Java FXML 中创建 KeyEvents?

How do I create KeyEvents in Java FXML?

我创建了这个小项目来测试关键事件。但是当我按下键时,它的行为并不像我想要的那样。实际上我的计算器项目需要关键事件。我创建了一个计算器项目,除了鼠标点击之外,我想添加一个可以从键盘输入数字或运算符的功能。任何人都可以检查一下并帮助使其更实用吗?

public class FXMLDocumentController implements Initializable{

@FXML
private Label label;

@FXML
private Button backSpace;

@FXML
private Button spaceBar;

@FXML
private Button enter;

@FXML
void typedBS(KeyEvent event) {
    if (event.getCode() == KeyCode.BACK_SPACE) {
        label.setText(event.getText() + " typed.");
    }
}

@FXML
void typedE(KeyEvent event) {
    if (event.getCode()==KeyCode.ENTER) {
        label.setText(event.getText() + " typed");
    }
}

@FXML
void typedSB(KeyEvent event) {
    if (event.getCode()==KeyCode.SPACE) {
        label.setText(event.getText()+" typed");
    }
}

@FXML
void PressBackSpace(KeyEvent event) {
    if (event.getCode() == KeyCode.BACK_SPACE) {
        label.setText("You pressed Back Space key!");
    }
}

@FXML
void clickBackSpace(ActionEvent event) {
    label.setText("You clicked Back Space key!");
}

@FXML
void clickEnter(ActionEvent event) {
    label.setText("You clicked Enter key!");
}

@FXML
void clickSpaceBar(ActionEvent event) {
    label.setText("You clicked SpaceBar key!");
}

@FXML
void pressEnter(KeyEvent event) {
    if (event.getCode() == KeyCode.ENTER) {
        label.setText("You pressed Enter key!");
    }
}

@FXML
void pressSpaceBar(KeyEvent event) {
    if (event.getCode() == KeyCode.SPACE) {
        label.setText("You pressed SpaceBar key!");
    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

}

在同一事物中定义事件和处理。 尝试这样的事情: 如果您使用边框作为示例

borderPane.setOnKeyPressed(this::handleKeyPress);

private void handleKeyPress(KeyEvent event) {
    if (event.getCode() == KeyCode.BACK_SPACE) {
        label.setText(event.getText() + " typed.");
    }
    if (event.getCode() == KeyCode.SPACE) {
        label.setText("You pressed SpaceBar key!");
    }
     //TODO
}

希望对您有所帮助

If you look at this FXML file you can see my root node(The AnchorPane) has an onKeyPressed and onKeyReleased.

<AnchorPane id="AnchorPane" onKeyPressed="#handleOnKeyPressed" onKeyReleased="#handleOnKeyReleased" prefHeight="650.0" prefWidth="855.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="atmfx.FXMLDocumentController">
   <children>
      <BorderPane layoutX="263.0" layoutY="94.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <left>
            <AnchorPane fx:id="apLeftDisplay" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <children>

In The Controller my method that handles these two KeyEvents are

@FXML
private void handleOnKeyReleased(KeyEvent event)
{
    System.out.println();
    Button tempButton = buttons.get(event.getText());
    System.out.println("Released key text: " + event.getText());
    System.out.println("Released key code: " + event.getCode());

    if (tempButton != null) {
        tempButton.disarm();
        tempButton.setStyle("");
    }
    else if (event.getCode().equals(KeyCode.ENTER)) {
        tempButton = buttons.get("enter");
        tempButton.disarm();
        tempButton.setStyle("");
    }
    else if (event.getCode().equals(KeyCode.BACK_SPACE)) {
        tempButton = buttons.get("backspace");
        tempButton.disarm();
        tempButton.setStyle("");
    }
    else if (event.getCode().equals(KeyCode.SPACE)) {
        tempButton = buttons.get("space");
        tempButton.disarm();
        tempButton.setStyle("");
    }
}

@FXML
private void handleOnKeyPressed(KeyEvent event)
{
    Button tempButton = buttons.get(event.getText());
    System.out.println("Pressed key text: " + event.getText());
    System.out.println("Pressed key code: " + event.getCode());
    if (tempButton != null) {
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
    else if (event.getCode().equals(KeyCode.ENTER)) {
        tempButton = buttons.get("enter");
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
    else if (event.getCode().equals(KeyCode.BACK_SPACE)) {
        tempButton = buttons.get("backspace");
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
    else if (event.getCode().equals(KeyCode.SPACE)) {
        tempButton = buttons.get("space");
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
}

In this code I added all of my buttons to a HasMap. That way in my keyevent handlers I was able to retrieve the button depending on the keyboard key I pressed.

Map<String, Button> buttons = new HashMap<>();
TheButtonsParentNode.getChildren().stream().filter((tempNode)
            -> (tempNode instanceof Button)).map((
            tempNode) -> (Button) tempNode).forEachOrdered((tempButton) -> {
        buttons.put(tempButton.getText().toLowerCase(), tempButton);//adding button text and the button to hashmap
    });