/// <inheritdoc />
 protected override void OnAttached()
 {
     _inputInterceptor     = IoC.Resolve <InputInterceptor>();
     _gameAreaFocusService = IoC.Resolve <GameAreaFocusService>();
     _inputInterceptor.VirtualKeyboardAction += InputInterceptorOnVirtualKeyboardAction;
 }
        private void InputInterceptorVirtualMouseAction(object sender, MousePressedEventArgs e)
        {
            if (e.DeviceId != _mouseId)
            {
                return;
            }

            MousePressedEventArgs lastKnown = null;
            Point worldCoodinates           = new Point();

            Application.Current.Dispatcher.Invoke(() =>
            {
                lastKnown       = _inputInterceptor.Mice[e.DeviceId].LastMouseData;
                var destination = new Point(Canvas.GetLeft(VirtualMouse) + lastKnown.X, Canvas.GetTop(VirtualMouse) + lastKnown.Y);

                if (CheckBounds(lastKnown.X, lastKnown.Y))
                {
                    Canvas.SetTop(VirtualMouse, destination.Y);
                    Canvas.SetLeft(VirtualMouse, destination.X);
                    lastKnown.AbsolutX = (int)destination.X;
                    lastKnown.AbsolutY = (int)destination.Y;
                }

                if (lastKnown.State == MouseState.Undefined)
                {
                    return;
                }

                worldCoodinates = AssociatedObject.TranslatePoint(destination, Application.Current.MainWindow);
            }, DispatcherPriority.Send);

            bool handeld             = false;
            var  elementsUnderCursor = new List <DependencyObject>();

            Application.Current.Dispatcher.Invoke(() =>
            {
                var positon = new Point(lastKnown.AbsolutX, lastKnown.AbsolutY);
                if (!GameAreaFocusService.GetExactHitTest(LayoutBounds, LayoutBounds, positon, elementsUnderCursor))
                {
                    return;
                }

                var findScrollProvider = elementsUnderCursor
                                         .Select(GameAreaFocusService.GetVisualParentWithKeyHandling)
                                         .FirstOrDefault(f => f != null);
                if (findScrollProvider == null)
                {
                    return;
                }

                var firstOrDefault = Interaction.GetBehaviors(findScrollProvider).FirstOrDefault(f => f is IVirtualInputReciver) as IVirtualInputReciver;
                handeld            = firstOrDefault.ReciveMouseInput(lastKnown);
            }, DispatcherPriority.Send);
            if (handeld)
            {
                return;
            }
            if (lastKnown.State == MouseState.Undefined)
            {
                return;
            }
            var btnMessages = new MSG[0];

            if (lastKnown.State == MouseState.LeftDown || lastKnown.State == MouseState.LeftUp)
            {
                btnMessages = SendMessageApi.GetControlClickMessages("left", lastKnown.State == MouseState.LeftDown,
                                                                     (int)worldCoodinates.X, (int)worldCoodinates.Y);
            }
            else if (lastKnown.State == MouseState.RightDown || lastKnown.State == MouseState.RightUp)
            {
                btnMessages = SendMessageApi.GetControlClickMessages("right", lastKnown.State == MouseState.RightDown,
                                                                     (int)worldCoodinates.X, (int)worldCoodinates.Y);
            }
            else if (lastKnown.State == MouseState.MiddleDown || lastKnown.State == MouseState.MiddleUp)
            {
                btnMessages = SendMessageApi.GetControlClickMessages("middle", lastKnown.State == MouseState.MiddleDown,
                                                                     (int)worldCoodinates.X, (int)worldCoodinates.Y);
            }
            else if (lastKnown.State == MouseState.ScrollDown)
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    var findScrollProvider = GameAreaFocusService.GetVisualParent <ScrollViewer>(elementsUnderCursor.Last());
                    if (findScrollProvider != null)
                    {
                        findScrollProvider.ScrollToVerticalOffset(lastKnown.Rolling);
                    }
                    else
                    {
                        var dependencyObject    = elementsUnderCursor.Last();
                        var mouseWheelEventArgs = new MouseWheelEventArgs(InputManager.Current.PrimaryMouseDevice, 0, lastKnown.Rolling);

                        mouseWheelEventArgs.RoutedEvent = UIElement.MouseWheelEvent;
                        mouseWheelEventArgs.Source      = this;
                        (dependencyObject as UIElement).RaiseEvent(mouseWheelEventArgs);
                    }
                }, DispatcherPriority.Send);
            }

            foreach (var msg in btnMessages)
            {
                SendMessageApi.SendMessageTo(_windowHandle, msg.message, msg.wParam, msg.lParam);
            }
        }
 public ChromiumKeyReciverBehavior()
 {
     _gameAreaFocusService = IoC.Resolve <GameAreaFocusService>();
     WindowInteropHelper   = new WindowInteropHelper(Application.Current.MainWindow);
 }