private Mouse CreateCursorMouse() { var initialPosition = Control.MousePosition; var cycleMode = CycleModeIn.SliceCount > 0 ? CycleModeIn[0] : CycleMode.NoCycle; var mouseData = new MouseData() { Position = initialPosition }; var mouseInput = Observable.FromEventPattern <MouseInputEventArgs>(typeof(Device), "MouseInput") .Where(_ => EnabledIn.SliceCount > 0 && EnabledIn[0]); IObservable <MouseNotification> notifications; switch (cycleMode) { case CycleMode.NoCycle: notifications = mouseInput .SelectMany <EventPattern <MouseInputEventArgs>, MouseNotification>(ep => GenerateCursorNotifications(mouseData, ep.EventArgs)); break; case CycleMode.Cycle: notifications = mouseInput .SelectMany <EventPattern <MouseInputEventArgs>, MouseNotification>(ep => GenerateCyclicCursorNotifications(mouseData, ep.EventArgs)); break; case CycleMode.IncrementCycle: var incrementalMouseData = new IncrementalMouseData() { Position = initialPosition, IncrementalPosition = initialPosition }; notifications = mouseInput .SelectMany <EventPattern <MouseInputEventArgs>, MouseNotification>(ep => GenerateIncrementalCyclicCursorNotifications(incrementalMouseData, ep.EventArgs)); break; default: throw new NotImplementedException(); } return(new Mouse(notifications)); }
private IEnumerable <MouseNotification> GenerateIncrementalCyclicCursorNotifications(IncrementalMouseData mouseData, MouseInputEventArgs args) { var virtualScreenSize = SystemInformation.VirtualScreen.Size; var position = Control.MousePosition; var delta = new Size(position.X - mouseData.Position.X, position.Y - mouseData.Position.Y); var newPosition = position; var leftBounds = GetLeftMostMonitorBounds(position.Y); var rightBounds = GetRightMostMonitorBounds(position.Y); if (position.X <= leftBounds.Left) { newPosition.X = rightBounds.Right; delta = new Size(-1, delta.Height); } if (position.X >= rightBounds.Right - 1) { newPosition.X = leftBounds.Left; delta = new Size(1, delta.Height); } var topBounds = GetTopMostMonitorBounds(position.X); var bottomBounds = GetBottomMostMonitorBounds(position.X); if (position.Y <= topBounds.Top) { newPosition.Y = bottomBounds.Bottom; delta = new Size(delta.Width, -1); } if (position.Y >= bottomBounds.Bottom - 1) { newPosition.Y = topBounds.Top; delta = new Size(delta.Width, 1); } if (newPosition != position) { Cursor.Position = newPosition; position = newPosition; } if (mouseData.Position != position) { mouseData.Position = position; mouseData.IncrementalPosition += delta; yield return(new MouseMoveNotification(mouseData.IncrementalPosition, virtualScreenSize)); } foreach (var n in GenerateMouseButtonNotifications(args, mouseData.IncrementalPosition, virtualScreenSize)) { yield return(n); } }