我正在尝试检测鼠标是否在整个场景上移动,或者在 15 分钟不活动后不关闭应用程序。
在示例中,我有一个 Anchorpane 占据整个场景,并在其上有一个 OnMouseMoved 事件。
检测到所有移动都预计会经过 tabPane,为什么?
无论节点悬停在何处,如何设法检测场景中的所有运动?
这是文件:
MoveTest.java
public class MoveTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
loadIhm(primaryStage);
}
void loadIhm(Stage stage) throws URISyntaxException {
URL location = MouseTestCtrl.class.getResource("/application/mouseTest.fxml");
FXMLLoader loader = new FXMLLoader(location);
try {
AnchorPane root = loader.load();
stage.setScene(new Scene(root));
stage.show();
} catch (IOException e) {
throw new RuntimeException("Problème FXML : mouseTest.fxml");
}
}}
鼠标测试Ctrl
public class MouseTestCtrl {
@FXML
void moveDetected(MouseEvent event) {
System.out.println("moving");
}}
mouseTest.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseMoved="#moveDetected" prefHeight="644.0" prefWidth="883.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MouseTestCtrl">
<children>
<Pane layoutX="81.0" layoutY="70.0" prefHeight="499.0" prefWidth="730.0" style="-fx-border-color: black;">
<children>
<TabPane layoutX="11.0" layoutY="212.0" prefHeight="251.0" prefWidth="681.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
<Label layoutX="14.0" layoutY="4.0" text="Pane" />
<GridPane layoutX="372.0" layoutY="88.0" prefHeight="75.0" prefWidth="198.0" style="-fx-border-color: black;">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="3" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="5" GridPane.rowIndex="2" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="6" GridPane.rowIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="2" GridPane.columnIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="4" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="1" />
</children>
</GridPane>
<Label layoutX="434.0" layoutY="34.0" text="gridpane" />
<AnchorPane layoutX="58.0" layoutY="44.0" prefHeight="127.0" prefWidth="198.0" style="-fx-border-color: black;">
<children>
<Label layoutX="15.0" layoutY="6.0" text="Anchor" />
</children></AnchorPane>
</children>
</Pane>
<Label layoutX="14.0" layoutY="14.0" text="Anchor" />
</children>
</AnchorPane>
Slaw 提出的解决方案效果完美
theScene.addEventFilter(MouseEvent.MOUSE_MOVED, e -> { ... }).
尽管您可能想监听 MouseEvent.ANY 甚至 InputEvent.ANY 相反,因为用户活动通常不仅仅包含鼠标移动事件(例如,鼠标单击、鼠标拖动、按键等)。
此外,如果需要,您可以将该滤镜添加到舞台而不是场景中