private void AttachEventHandlers(UIElement uiElement) { uiElement.GotFocus += (sender, arg) => { try { if (_browserHost != null) { // this.browserHost.SetFocus(true); _browserHost.SendFocusEvent(true); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in GotFocus()", ex); } }; uiElement.LostFocus += (sender, arg) => { try { if (_browserHost != null) { // this.browserHost.SetFocus(false); _browserHost.SendFocusEvent(false); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in LostFocus()", ex); } }; uiElement.LostMouseCapture += (sender, arg) => { try { if (_browserHost != null) { _browserHost.SendCaptureLostEvent(); //_logger.Debug("Browser_LostMouseCapture"); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in LostMouseCapture()", ex); } }; uiElement.MouseLeave += (sender, arg) => { try { if (_browserHost != null) { CefMouseEvent mouseEvent = new CefMouseEvent() { X = 0, Y = 0 }; _browserHost.SendMouseMoveEvent(mouseEvent, true); //_logger.Debug("Browser_MouseLeave"); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseLeave()", ex); } }; uiElement.MouseMove += (sender, arg) => { try { if (_browserHost != null) { Point cursorPos = arg.GetPosition(this); CefMouseEvent mouseEvent = new CefMouseEvent() { X = (int)cursorPos.X, Y = (int)cursorPos.Y }; _browserHost.SendMouseMoveEvent(mouseEvent, false); //_logger.Debug(string.Format("Browser_MouseMove: ({0},{1})", cursorPos.X, cursorPos.Y)); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseMove()", ex); } arg.Handled = true; }; uiElement.MouseDown += (sender, arg) => { try { if (_browserHost != null) { CaptureMouse(); Keyboard.Focus(this); Point cursorPos = arg.GetPosition(this); CefMouseEvent mouseEvent = new CefMouseEvent() { X = (int)cursorPos.X, Y = (int)cursorPos.Y, }; mouseEvent.Modifiers = GetMouseModifiers(); if (arg.ChangedButton == MouseButton.Left) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Left, false, 1); } else if (arg.ChangedButton == MouseButton.Middle) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Middle, false, 1); } else if (arg.ChangedButton == MouseButton.Right) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Right, false, 1); } //_logger.Debug(string.Format("Browser_MouseDown: ({0},{1})", cursorPos.X, cursorPos.Y)); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseDown()", ex); } arg.Handled = true; }; uiElement.MouseUp += (sender, arg) => { try { if (_browserHost != null) { Point cursorPos = arg.GetPosition(this); CefMouseEvent mouseEvent = new CefMouseEvent() { X = (int)cursorPos.X, Y = (int)cursorPos.Y, }; mouseEvent.Modifiers = GetMouseModifiers(); if (arg.ChangedButton == MouseButton.Left) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Left, true, 1); } else if (arg.ChangedButton == MouseButton.Middle) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Middle, true, 1); } else if (arg.ChangedButton == MouseButton.Right) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Right, true, 1); } //_logger.Debug(string.Format("Browser_MouseUp: ({0},{1})", cursorPos.X, cursorPos.Y)); ReleaseMouseCapture(); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseUp()", ex); } arg.Handled = true; }; uiElement.MouseWheel += (sender, arg) => { try { if (_browserHost != null) { Point cursorPos = arg.GetPosition(this); CefMouseEvent mouseEvent = new CefMouseEvent() { X = (int)cursorPos.X, Y = (int)cursorPos.Y, }; _browserHost.SendMouseWheelEvent(mouseEvent, 0, arg.Delta); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseWheel()", ex); } arg.Handled = true; }; // TODO: require more intelligent processing uiElement.PreviewTextInput += (sender, arg) => { if (_browserHost != null) { _logger.Debug("TextInput: text {0}", arg.Text); foreach (var c in arg.Text) { CefKeyEvent keyEvent = new CefKeyEvent() { EventType = CefKeyEventType.Char, WindowsKeyCode = (int)c, // Character = c, }; _browserHost.SendKeyEvent(keyEvent); } } arg.Handled = true; }; // TODO: require more intelligent processing uiElement.PreviewKeyDown += (sender, arg) => { try { if (_browserHost != null) { //_logger.Debug(string.Format("KeyDown: system key {0}, key {1}", arg.SystemKey, arg.Key)); CefKeyEvent keyEvent = new CefKeyEvent() { EventType = CefKeyEventType.RawKeyDown, WindowsKeyCode = KeyInterop.VirtualKeyFromKey(arg.Key == Key.System ? arg.SystemKey : arg.Key), NativeKeyCode = 0, IsSystemKey = arg.Key == Key.System, }; _browserHost.SendKeyEvent(keyEvent); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in PreviewKeyDown()", ex); } arg.Handled = false; }; // TODO: require more intelligent processing uiElement.PreviewKeyUp += (sender, arg) => { try { if (_browserHost != null) { //_logger.Debug(string.Format("KeyUp: system key {0}, key {1}", arg.SystemKey, arg.Key)); CefKeyEvent keyEvent = new CefKeyEvent() { EventType = CefKeyEventType.KeyUp, WindowsKeyCode = KeyInterop.VirtualKeyFromKey(arg.Key == Key.System ? arg.SystemKey : arg.Key), NativeKeyCode = 0, IsSystemKey = arg.Key == Key.System, }; _browserHost.SendKeyEvent(keyEvent); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in PreviewKeyUp()", ex); } arg.Handled = false; }; }