我使用触摸屏幕来控制应用程序中的节点。但是如果应用程序没有运行触摸屏幕时,我也希望使用鼠标事件来控制这些节点,以便可以处理两种事件。我尝试了以下方法:
private void initDraggable() {
touchFilter = new EventHandler<TouchEvent>() {
@Override public void handle(TouchEvent event) {
System.out.println("Touch dragged");
touchTaskViewModel.actualClickPointProperty().set(new ClickPoint(event.getTouchPoint().getX(), event.getTouchPoint().getY()));
touchTaskViewModel.sceneXProperty().set(event.getTouchPoint().getSceneX());
touchTaskViewModel.handleTaskMoved();
event.consume();
}
};
addEventFilter(TouchEvent.TOUCH_MOVED, touchFilter);
setOnTouchPressed(new EventHandler<TouchEvent>() {
@Override
public void handle(TouchEvent event) {
touchTaskViewModel.entryClickPointProperty().set(new ClickPoint(event.getTouchPoint().getX(), event.getTouchPoint().getY()));
touchTaskViewModel.taskSelectedProperty().set(true);
event.consume();
}
});
setOnTouchReleased(new EventHandler<TouchEvent>() {
@Override
public void handle(TouchEvent event) {
touchTaskViewModel.taskSelectedProperty().set(false);
event.consume();
}
});
clickFilter = new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent event) {
if(!event.isSynthesized()){
System.out.println("Mouse dragged!");
touchTaskViewModel.actualClickPointProperty().set(new ClickPoint(event.getX(), event.getY()));
touchTaskViewModel.sceneXProperty().set(event.getSceneX());
touchTaskViewModel.handleTaskMoved();
event.consume();
}
}
};
addEventFilter(MouseEvent.MOUSE_DRAGGED, clickFilter);
setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(!event.isSynthesized()){
touchTaskViewModel.entryClickPointProperty().set(new ClickPoint(event.getX(), event.getY()));
touchTaskViewModel.taskSelectedProperty().set(true);
event.consume();
}
}
});
setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(!event.isSynthesized()){
touchTaskViewModel.taskSelectedProperty().set(false);
event.consume();
}
}
});
}
所以,如果事件已经由触摸事件处理,则函数isSynthesized()应返回true。但是,无论我使用触摸监视器还是鼠标拖动节点,它始终返回false。
触摸事件已经被正确处理,我看到它打印了“触摸拖动”和“鼠标拖动”。