public static IObservable <MouseButtonEventArgs> MouseClick(this DependencyObject that) { UIElement ui = (UIElement)that; bool isIn = false; return(Observable.Merge( that.MouseDown() .Where(x => x.ChangedButton == MouseButton.Left) .Do(x => { isIn = true; ui.CaptureMouse(); }) .Where(x => false) // don't select mouse down , that.LostMouseCapture() .Do(x => { isIn = false; }) .Where(x => false) // don't select that! // event to return , that.MouseUp() .Where(x => x.ChangedButton == MouseButton.Left && isIn) .Do(x => ui.ReleaseMouseCapture()) .Where(x => that.Contains(x.Source as DependencyObject)) ) .Select(x => (MouseButtonEventArgs)x)); }
public static IObservable <Tuple <MouseButtonEventArgs, int> > MouseClicks(this DependencyObject that, TimeSpan interval) { // trying to avoid "new (ReferenceType)" as much as possible UIElement ui = (UIElement)that; int count = 0; long tInterval = interval.Ticks / 2; // time between clicks is half a double click! long t0 = 0, tLast = 0; bool isIn = false; return(Observable.Merge( that.MouseDown() .Where(x => x.ChangedButton == MouseButton.Left) .Do(x => { isIn = true; tLast = DateTime.Now.Ticks; ui.CaptureMouse(); }) .Where(x => false) // don't select mouse down , LostMouseCapture(ui) .Do(x => { isIn = false; }) .Where(x => false) // don't select that! // event to return , that.MouseUp() .Where(x => x.ChangedButton == MouseButton.Left && isIn) .Do(x => ui.ReleaseMouseCapture()) .Where(x => that.Contains(x.Source as DependencyObject)) ) // no transform mouse up events .Select(x => { var e = (MouseButtonEventArgs)x; count++; var tNow = DateTime.Now.Ticks; // multi click test if (count > 1 && tNow > t0 + count * tInterval) { t0 = tLast; count = 1; } return Tuple.Create(e, count); })); }