How can I prevent mousemove events on mouse click?

I have a grid that I want to move. While I am just hanging and moving the mouse, I want the events to work, but when I click on the muscles, they should pause the shooting. As soon as I run mouseup, they should continue.

If it sounds super simple, it probably isn't. After a while, we came up with a not-so-elegant solution, but I wonder if something is better. I will not influence your approach with the help of our hack.

So, the initial code that does not work

mouseMove.TakeUntil(mouseDown).Repeat() 

Adding .SkipUntil(mouseUp) left or right on TakeUntil completely stops the work on the code.

+4
source share
4 answers

How about this:

 bool mouseIsDown = false; Observable.Merge( mouseDown.Select(_ => true), mouseUp.Select(_ => false) ).Subscribe(x => mouseIsDown = x); mouseMove.Where(_ => !mouseIsDown); 

The technically correct answer includes the Window statement, but it's just as good and easy to bang (and it’s easier for me to write)

+2
source

this might be a possible solution

 // create the observables IObservable<Point> mouseMove = Observable.FromEventPattern<MouseEventArgs>(this, "MouseDown") .Select(e=>e.EventArgs.GetPosition(this)); IObservable<bool> mouseDown = Observable.FromEventPattern(this, "MouseDown").Select(_ => false); IObservable<bool> mouseUp = Observable.FromEventPattern(this, "MouseUp").Select(_ => true); var merged = mouseUp.Merge(mouseDown).StartWith(true); // sends the moves with the current state of the mouse button var all = mouseMove.CombineLatest(merged, (move, take) => new {Take = take, Move = move}); // the result is all the points from mouse move where the mouse button isn't pressed var result = all.Where(t => t.Take).Select(t => t.Move); 
0
source

Below are 2 possible solutions.

 var beh = new BehaviorSubject<bool>(true); mousedown.Select(_ => false).Subscribe(beh); mouseup.Select(_ => true).Subscribe(beh); mousemove.SelectMany(e => { return mousemove.TakeUntil(beh.Where(b => !b)); }).Subscribe( .... ); 

OR

 var du = mousedown.Select(_ => false).Merge(mouseup.Select(_ => true)).Merge(Observable.Return(true)); mousemove.CombineLatest(du, (ev, b) => new Tuple<MouseEventArgs, bool>(ev.EventArgs, b)) .Where(t => t.Item2) .Select(t => t.Item1) .Subscribe(....); 
0
source

It works:

  var mouseMoveWhileUpOnly = mouseUp .Select(mu => mouseMove .TakeUntil(mouseDown)) .Switch(); 

The only trick you need to do to start monitoring without having to manually make a mouse is:

  var mouseUp = Observable .FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>( h => this.MouseLeftButtonUp += h, h => this.MouseLeftButtonUp -= h) .Select(ep => Unit.Default) .StartWith(Unit.Default); 

Pay attention to StartWith .

Otherwise, the observed values ​​of mouseDown and mouseMove determined as usual.

0
source

All Articles