KeyListener JavaFX

KeyListener JavaFX,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,我在网上找,但没有找到好的信息。我试图在每次应用程序运行时检测按键。我正在使用JavaFX并使用FXML运行它。我试了很多东西,但没有成功。请帮帮我。你应该查看一下。下面是关键的侦听器代码 /** * Copyright (c) 2008, 2012 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. */ import javafx.application.Applic

我在网上找,但没有找到好的信息。我试图在每次应用程序运行时检测按键。我正在使用JavaFX并使用FXML运行它。我试了很多东西,但没有成功。请帮帮我。

你应该查看一下。下面是关键的侦听器代码

/**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.PerspectiveTransform;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

/**
 * A sample that demonstrates various key events and their usage. Type in the
 * text box to view the triggered events: key pressed, key typed and key 
 * released. Pressing the Shift, Ctrl, and Alt keys also trigger events.
 *
 * @see javafx.scene.input.KeyCode
 * @see javafx.scene.input.KeyEvent
 * @see javafx.event.EventHandler
 */
public class KeyEventsSample extends Application {

    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setScene(new Scene(root));
        //create a console for logging key events
        final ListView<String> console = new ListView<String>(FXCollections.<String>observableArrayList());
        // listen on the console items and remove old ones when we get over 20 items in the list
        console.getItems().addListener(new ListChangeListener<String>() {
            @Override public void onChanged(Change<? extends String> change) {
                while (change.next()) {
                    if (change.getList().size() > 20) change.getList().remove(0);
                }
            }
        });
        // create text box for typing in
        final TextField textBox = new TextField();
        textBox.setPromptText("Write here");
        textBox.setStyle("-fx-font-size: 34;");
        //add a key listeners
        textBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                console.getItems().add("Key Pressed: " + ke.getText());
            }
        });
        textBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                console.getItems().add("Key Released: " + ke.getText());
            }
        });
        textBox.setOnKeyTyped(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                String text = "Key Typed: " + ke.getCharacter();
                if (ke.isAltDown()) {
                    text += " , alt down";
                }
                if (ke.isControlDown()) {
                    text += " , ctrl down";
                }
                if (ke.isMetaDown()) {
                    text += " , meta down";
                }
                if (ke.isShiftDown()) {
                    text += " , shift down";
                }
                console.getItems().add(text);
            }
        });
        VBox vb = new VBox(10);
        vb.getChildren().addAll(textBox, console);
        root.getChildren().add(vb);
    }

    @Override public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
    }
    public static void main(String[] args) { launch(args); }
}
/**
*版权所有(c)2008、2012 Oracle和/或其附属公司。
*版权所有。使用受许可条款的约束。
*/
导入javafx.application.application;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.stage.stage;
导入javafx.collections.FXCollections;
导入javafx.collections.ListChangeListener;
导入javafx.event.EventHandler;
导入javafx.scene.Group;
导入javafx.scene.Node;
导入javafx.scene.control.ListView;
导入javafx.scene.control.TextField;
导入javafx.scene.effect.DropShadow;
导入javafx.scene.effect.PerspectiveTransform;
导入javafx.scene.input.KeyEvent;
导入javafx.scene.layout.VBox;
导入javafx.scene.paint.Color;
/**
*演示各种关键事件及其用法的示例。输入
*查看触发事件的文本框:按键、键入键和
*释放。按下Shift、Ctrl和Alt键也会触发事件。
*
*@请参阅javafx.scene.input.KeyCode
*@请参阅javafx.scene.input.KeyEvent
*@请参阅javafx.event.EventHandler
*/
公共类KeyEventsSample扩展了应用程序{
私有void init(阶段primaryStage){
组根=新组();
primaryStage.setScene(新场景(根));
//创建用于记录密钥事件的控制台
最终ListView控制台=新建ListView(FXCollections.observableArrayList());
//当列表中的项目超过20个时,请收听控制台项目并删除旧项目
console.getItems().addListener(新ListChangeListener()){
@更改后覆盖公共无效(更改这对我来说很有效:

在FXML中,在元素中添加onKeyPressed属性。下面是一个示例,请注意onKeyPressed属性在AnchorPane元素中

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" onKeyPressed="#handleKeyPressed"  xmlns:fx="http://javafx.com/fxml" fx:controller="com.jtetris.jtetris.FXMLController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>
最后,在start方法中加载fxml

public class MainApp extends Application {

 @Override
 public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));

    Scene scene = new Scene(root);

    stage.setTitle("JavaFX and Maven");
    stage.setScene(scene);
    stage.show();
 }
 public static void main(String[] args) {
    launch(args);
 }

}
public class MainApp extends Application {

 @Override
 public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));

    Scene scene = new Scene(root);

    stage.setTitle("JavaFX and Maven");
    stage.setScene(scene);
    stage.show();
 }
 public static void main(String[] args) {
    launch(args);
 }

}