简体   繁体   English

来自JavaFX场景的回调事件

[英]Callback events from a javafx scene

I have developed a javafx application which is in fact a learning game. 我开发了一个javafx应用程序,它实际上是一个学习游戏。 That app contains of several javafx scenes. 该应用程序包含几个javafx场景。 In one of them which is the main game I want to store the mouse positions and the keyboards events as well. 在其中一个主要游戏中,我还想存储鼠标位置和键盘事件。 How can I have access to those events from javafx scenes? 如何从javafx场景访问那些事件?

You have to use event handler Using this for mouse_position : 您必须将事件处理程序用于mouse_position:

EventHandler<MouseEvent> handler = event -> { 
/* event.getSceneX() and event.getSceneY() to retrieve positions */
};
main.getScene().addEventFilter(MouseEvent.MOUSE_MOVED, handler);
main.getScene().addEventFilter(MouseEvent.MOUSE_DRAGGED, handler);

And this for keyboard : 而这对于键盘:

object.setOnKeyPressed(new EventHandler<KeyEvent>() {
    public void handle(KeyEvent ke) {
        System.out.println("Key Pressed: " + ke.getText());
    }
});

object.setOnKeyReleased(new EventHandler<KeyEvent>() {
    public void handle(KeyEvent ke) {
        System.out.println("Key Released: " + ke.getText());
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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