public WpfPresentationSource(UIElement rootElement, IWpfValueConverter converter)
        {
            this.RootElement = rootElement;
            this.converter = converter;

            RootElement.IsRootElement = true;

            MouseDevice = new MouseDevice(this);
            KeyboardDevice = new KeyboardDevice(this);

            container = new wpf::System.Windows.Controls.Canvas { Background = wpf::System.Windows.Media.Brushes.Transparent };
            container.PreviewMouseMove += OnContainerMouseMove;
            container.PreviewMouseDown += OnContainerMouseDown;
            container.PreviewMouseUp += OnContainerMouseUp;
            container.PreviewMouseWheel += (sender, e) => e.Handled = MouseDevice.ProcessRawEvent(new RawMouseWheelEventArgs(e.Delta, converter.ConvertBack(e.GetPosition(container)), GetTimestamp()));

            MouseDevice.CursorChanged += (sender, e) => container.Cursor = converter.Convert(MouseDevice.Cursor);
            container.Cursor = converter.Convert(MouseDevice.Cursor);

            window = new wpf::System.Windows.Window { UseLayoutRounding = true, Content = container };
            window.Activated += (sender, e) => MouseDevice.Activate();
            window.Deactivated += (sender, e) => MouseDevice.Deactivate();
            window.SizeChanged += (sender, e) => SetRootElementSize();
            window.PreviewKeyDown += (sender, e) => e.Handled = KeyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(converter.ConvertBack(e.Key), converter.ConvertBack(e.KeyStates), e.IsRepeat, GetTimestamp()));
            window.PreviewKeyUp += (sender, e) => e.Handled = KeyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(converter.ConvertBack(e.Key), converter.ConvertBack(e.KeyStates), e.IsRepeat, GetTimestamp()));
            window.Show();

            container.Children.Add(((IWpfRenderElement)rootElement.GetRenderElement(WpfRenderElementFactory.Default)).WpfElement);
            SetRootElementSize();

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
        public PresentationSource(UIElement rootElement, IHtmlValueConverter converter)
        {
            this.RootElement = rootElement;
            this.converter = converter;

            RootElement.IsRootElement = true;

            MouseDevice = new MouseDevice(this);
            KeyboardDevice = new KeyboardDevice(this);

            window = System.Html.Window.Instance;

            MouseDevice.CursorChanged += (sender, e) => System.Html.Window.Document.Body.Style.Cursor = converter.ToCursorString(MouseDevice.Cursor);
            System.Html.Window.Document.Body.Style.Cursor = converter.ToCursorString(MouseDevice.Cursor);

            System.Html.Window.OnKeydown = OnKeyDown;
            System.Html.Window.OnKeyup = OnKeyUp;
            System.Html.Window.OnKeypress = PreventKeyboardHandled;
            System.Html.Window.OnMousemove = OnMouseMove;
            System.Html.Window.OnMousedown = OnMouseDown;
            System.Html.Window.OnMouseup = OnMouseUp;
            System.Html.Window.OnScroll = OnMouseWheel;
            System.Html.Window.OnFocus = e => MouseDevice.Activate();
            System.Html.Window.OnBlur = e => MouseDevice.Deactivate();
            System.Html.Window.OnResize = e => SetRootElementSize();
            System.Html.Window.OnClick = PreventMouseHandled;
            System.Html.Window.OnDblclick = PreventMouseHandled;
            System.Html.Window.OnContextmenu = PreventMouseHandled;
            System.Html.Window.AddEventListener("wheel", OnMouseWheel);

            SetRootElementSize();
            System.Html.Window.Document.Body.Style.Overflow = "hidden";
            System.Html.Window.Document.Body.AppendChild(((HtmlRenderElement)RootElement.GetRenderElement(HtmlRenderElementFactory.Default)).HtmlElement);

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
        public void KeyboardDeviceBasicTest()
        {
            FrameworkElement element = new FrameworkElement();

            int eventIndex = 0;

            int previewKeyDownIndex = 0;
            int previewKeyUpIndex = 0;
            int previewGotKeyboardFocusIndex = 0;
            int previewLostKeyboardFocusIndex = 0;
            int keyDownIndex = 0;
            int keyUpIndex = 0;
            int gotKeyboardFocusIndex = 0;
            int lostKeyboardFocusIndex = 0;

            element.PreviewKeyDown += (sender, e) => previewKeyDownIndex = ++eventIndex;
            element.PreviewKeyUp += (sender, e) => previewKeyUpIndex = ++eventIndex;
            element.PreviewGotKeyboardFocus += (sender, e) => previewGotKeyboardFocusIndex = ++eventIndex;
            element.PreviewLostKeyboardFocus += (sender, e) => previewLostKeyboardFocusIndex = ++eventIndex;
            element.KeyDown += (sender, e) => keyDownIndex = ++eventIndex;
            element.KeyUp += (sender, e) => keyUpIndex = ++eventIndex;
            element.GotKeyboardFocus += (sender, e) => gotKeyboardFocusIndex = ++eventIndex;
            element.LostKeyboardFocus += (sender, e) => lostKeyboardFocusIndex = ++eventIndex;

            TestPresentationSource presentationSource = new TestPresentationSource();
            KeyboardDevice keyboardDevice = new KeyboardDevice(presentationSource);

            keyboardDevice.Activate();

            IDisposable focus = keyboardDevice.Focus(element);
            Assert.AreEqual(1, previewGotKeyboardFocusIndex);
            Assert.AreEqual(2, gotKeyboardFocusIndex);
            Assert.IsTrue(element.IsKeyboardFocused);

            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.Down, false, 0));
            Assert.AreEqual(KeyStates.Down, keyboardDevice.GetKeyStates(Key.Enter));
            Assert.AreEqual(3, previewKeyDownIndex);
            Assert.AreEqual(4, keyDownIndex);

            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.None, false, 0));
            Assert.AreEqual(KeyStates.None, keyboardDevice.GetKeyStates(Key.Enter));
            Assert.AreEqual(5, previewKeyUpIndex);
            Assert.AreEqual(6, keyUpIndex);

            focus.Dispose();
            Assert.AreEqual(7, previewLostKeyboardFocusIndex);
            Assert.AreEqual(8, lostKeyboardFocusIndex);
            Assert.IsFalse(element.IsKeyboardFocused);

            focus = keyboardDevice.Focus(element);
            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.Down, false, 0));
            focus.Dispose();
            Assert.AreEqual(13, previewKeyUpIndex);
            Assert.AreEqual(14, keyUpIndex);
            Assert.AreEqual(15, previewLostKeyboardFocusIndex);
            Assert.AreEqual(16, lostKeyboardFocusIndex);

            keyboardDevice.Focus(element);
            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.Down, false, 0));
            keyboardDevice.Deactivate();
            Assert.AreEqual(23, previewKeyUpIndex);
            Assert.AreEqual(24, keyUpIndex);
        }