如果在鼠标按下和释放的事件之间没有发生拖动事件,如何反应性地处理鼠标点击事件?

时间:2018-03-21 10:32:41

标签: rx-java2 reactivex rx-javafx

我想在鼠标按下和释放事件之间没有拖动事件时触发一些动作。换句话说 - 用户在点击时保持鼠标光标不动。 问题具体是关于recive方式实现它。真的很感激RxJava2解决方案(我正在使用RxJavaFx)。

2 个答案:

答案 0 :(得分:0)

model.initial_state

答案 1 :(得分: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"));
    }
}