当鼠标按下和释放事件之间没有拖动事件时,我想触发一些动作。换句话说 - 当用户在点击时保持鼠标光标不动。问题具体是关于实现它的直接方式。真的很感激 RxJava2 解决方案(我将它与 RxJavaFx 一起使用)。
问问题
134 次
2 回答
0
Observable.combineLatest(
JavaFxObservable.eventsOf(panel1, MouseEvent.DRAG_DETECTED).map(event -> System.currentTimeMillis()).startWith(System.currentTimeMillis()),
JavaFxObservable.eventsOf(panel1, MouseEvent.MOUSE_PRESSED).map(event -> System.currentTimeMillis()),
JavaFxObservable.eventsOf(panel1, MouseEvent.MOUSE_RELEASED).map(event -> System.currentTimeMillis()),
(dragDectedMillis, pressedMillis, releasedMillis) -> releasedMillis > pressedMillis && dragDectedMillis < pressedMillis ? true : false)
.filter(noDragDetected -> noDragDetected)
.subscribe(next -> System.out.println("Action"));
于 2018-03-21T12:48:32.417 回答
0
import io.reactivex.Observable;
import io.reactivex.rxjavafx.observables.JavaFxObservable;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class PressedReleasedWithoutDraggedApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
StackPane stackPane = new StackPane();
stackPane.setPrefSize(800, 600);
stage.setScene(new Scene(stackPane));
stage.show();
Observable<MouseEvent> mousePressedObservable = JavaFxObservable.eventsOf(stackPane, MouseEvent.MOUSE_PRESSED);
Observable<MouseEvent> mouseReleasedObservable = JavaFxObservable.eventsOf(stackPane, MouseEvent.MOUSE_RELEASED);
Observable<MouseEvent> mouseDraggedObservable = JavaFxObservable.eventsOf(stackPane, MouseEvent.MOUSE_DRAGGED);
mousePressedObservable
.switchMap(mousePressed -> mouseReleasedObservable.takeUntil(mouseDraggedObservable))
.subscribe(next -> System.out.println("PRESSED'N'RELEASED without DRAGGED"));
}
}
于 2018-12-07T16:17:03.133 回答