JavaFX KeyEvent没有触发

时间:2015-08-15 12:39:12

标签: java javafx keyevent eventhandler

我正在尝试使用JavaFX创建一个GUI环境(仍然处于非常早期阶段),我已经实现了EventHandler,MouseEvent工作正常,但KeyEvent由于某些原因似乎不起作用。我已经在构造函数中调用了setOnKeyPressed,setOnKeyTyped等,所以理论上它应该可以工作。 但我看不出究竟是什么导致了这个问题。

GUI.java

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import java.util.ArrayList;

/**
 * @author JAY
 */
public class GUI extends Pane implements EventHandler<Event>{

    private boolean up = false;
    private boolean down = false;
    private boolean left = false;
    private boolean right = false;
    private boolean spacebar = false;
    private boolean shift = false;
    private boolean ctrl = false;

    private double mouseX;
    private double mouseY;

    public GUI(int width, int height) {
        super();
        setStyle("-fx-background-color: black;");
        setPrefSize(width, height);

        ArrayList<Node> things = new ArrayList<>();
        getChildren().addAll(things);

        setOnKeyPressed(this);
        setOnKeyTyped(this);
        setOnKeyReleased(this);

        setOnMouseMoved(this);
        setOnMouseClicked(this);
        setOnMouseDragged(this);
    }

    @Override
    public void handle(Event event) {
        System.out.println(event.getEventType().toString());

        if(event instanceof KeyEvent) {
            if(event.getSource() == getOnKeyPressed()) {
                keyPressed((KeyEvent) event);
            }
            if(event.getSource() == getOnKeyReleased()) {
                keyReleased((KeyEvent) event);
            }

            System.out.println(((KeyEvent) event).getCode());
        }
        if(event instanceof MouseEvent) {
            if(event.getSource() == getOnMouseMoved()) {
                mouseMoved((MouseEvent) event);
            }

            System.out.println("mouse at x: " + mouseX + " y: " + mouseY);
        }
    }

    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
    }

    public void keyPressed(KeyEvent e) {
        KeyCode code = e.getCode();

        if (code == KeyCode.UP)
            up = true;
        if (code == KeyCode.DOWN)
            down = true;
        if (code == KeyCode.LEFT)
            left = true;
        if (code == KeyCode.RIGHT)
            right = true;
        if (code == KeyCode.SPACE)
            spacebar = true;
        if (code == KeyCode.SHIFT)
            shift = true;
        if (code == KeyCode.CONTROL)
            ctrl = true;
    }

    public void keyReleased(KeyEvent e) {
        KeyCode code = e.getCode();

        if (code == KeyCode.UP)
            up = false;
        if (code == KeyCode.DOWN)
            down = false;
        if (code == KeyCode.LEFT)
            left = false;
        if (code == KeyCode.RIGHT)
            right = false;
        if (code == KeyCode.SPACE)
            spacebar = false;
        if (code == KeyCode.SHIFT)
            shift = false;
        if (code == KeyCode.CONTROL)
            ctrl = false;
    }

}

Main.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * @author JAY
 */
public class Main extends Application {

    private GUI arena = new GUI(600, 600);

    public void start(Stage primaryStage) {
        primaryStage.setTitle("My GUI");
        primaryStage.setScene(new Scene(arena));
        primaryStage.show();
    }

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

}

2 个答案:

答案 0 :(得分:1)

正如@Francis Fredrick Valero所说,现场是接收这些关键事件的场景。

在你的main-&gt; start方法中你可以这样做:

primaryStage.setTitle("My GUI");
Scene scene = new Scene(arena);
scene.setOnKeyPressed(arena);
primaryStage.setScene(scene);
primaryStage.show();

答案 1 :(得分:0)

场景接收事件。你能尝试一下吗?

getScene().setOnKeyPressed(this);