protected override bool OnKeyDown(KeyEvent e) { if (Focused && IsEnabledInHierarchy() && browserHost != null) { browserHost.SendFocusEvent(true); try { //_logger.Debug(string.Format("KeyDown: system key {0}, key {1}", arg.SystemKey, arg.Key)); CefKeyEvent keyEvent = new CefKeyEvent() { EventType = CefKeyEventType.RawKeyDown, WindowsKeyCode = (int)e.Key /*KeyInterop.VirtualKeyFromKey(arg.Key == Key.System ? arg.SystemKey : arg.Key)*/, NativeKeyCode = (int)e.Key, /*0*/ /*IsSystemKey = e.Key == EKeys.System*/ }; keyEvent.Modifiers = GetCurrentKeyboardModifiers(); browserHost.SendKeyEvent(keyEvent); } catch (Exception ex) { Log.Error("WebBrowserControl: Caught exception in OnKeyDown(): " + ex.Message); } //arg.Handled = HandledKeys.Contains(arg.Key); return(true); } return(base.OnKeyDown(e)); }
/// <summary> /// Sends a mouse up event to the browser. /// </summary> /// <param name="x">The x-coordinate of the mouse relative to the left edge of the view.</param> /// <param name="y">The y-coordinate of the mouse relative to the top edge of the view.</param> /// <param name="button">One of the <see cref="CefMouseButtonType"/> values.</param> /// <param name="clicks">A click count.</param> /// <param name="modifiers">A bitwise combination of the <see cref="CefEventFlags"/> values.</param> public void SendMouseUpEvent(int x, int y, CefMouseButtonType button, int clicks, CefEventFlags modifiers) { CefBrowserHost browserHost = this.BrowserObject?.Host; if (browserHost == null) { return; } InitMouseEvent(x, y, modifiers); browserHost.SendFocusEvent(true); browserHost.SendMouseClickEvent(_mouseEventProxy, button, true, clicks); }
private void AttachEventHandlers(WpfCefBrowser browser) { browser.GotKeyboardFocus += (sender, arg) => { try { if (_browserHost != null) { _browserHost.SendFocusEvent(true); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in GotFocus()", ex); } }; browser.LostKeyboardFocus += (sender, arg) => { try { if (_browserHost != null) { _browserHost.SendFocusEvent(false); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in LostFocus()", ex); } }; browser.MouseLeave += (sender, arg) => { try { if (_browserHost != null) { CefMouseEvent mouseEvent = new CefMouseEvent() { X = 0, Y = 0 }; mouseEvent.Modifiers = GetMouseModifiers(); _browserHost.SendMouseMoveEvent(mouseEvent, true); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseLeave()", ex); } }; browser.MouseMove += (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(); _browserHost.SendMouseMoveEvent(mouseEvent, false); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseMove()", ex); } }; browser.MouseDown += (sender, arg) => { try { if (_browserHost != null) { Focus(); 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, arg.ClickCount); } else if (arg.ChangedButton == MouseButton.Middle) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Middle, false, arg.ClickCount); } else if (arg.ChangedButton == MouseButton.Right) { _browserHost.SendMouseClickEvent(mouseEvent, CefMouseButtonType.Right, false, arg.ClickCount); } } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseDown()", ex); } }; browser.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); } } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in MouseUp()", ex); } }; browser.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); } }; // TODO: require more intelligent processing browser.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, }; keyEvent.Modifiers = GetKeyboardModifiers(); _browserHost.SendKeyEvent(keyEvent); } } arg.Handled = true; }; // TODO: require more intelligent processing browser.PreviewKeyDown += (sender, arg) => { try { if (_browserHost != null) { 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, }; keyEvent.Modifiers = GetKeyboardModifiers(); _browserHost.SendKeyEvent(keyEvent); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in PreviewKeyDown()", ex); } arg.Handled = HandledKeys.Contains(arg.Key); }; // TODO: require more intelligent processing browser.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, }; keyEvent.Modifiers = GetKeyboardModifiers(); _browserHost.SendKeyEvent(keyEvent); } } catch (Exception ex) { _logger.ErrorException("WpfCefBrowser: Caught exception in PreviewKeyUp()", ex); } arg.Handled = true; }; }