/// <summary> /// Set InputFocus to a WinForm controls using Mono winforms connection to the X11 server. /// GeckoWebBrowser.RemoveInputFocus uses the Gecko/Gtk connection to the X11 server. /// The undoes the call to _browser.SetInputFocus. /// Call this method when a winform control has gained focus, and X11 Input focus is still on the Gecko control. /// </summary> protected static void MoveInputFocusBackToAWinFormsControl() { if (Platform.IsWindows) { return; } IntPtr newTargetHandle = NativeReplacements.MonoGetFocus(); IntPtr displayHandle = NativeReplacements.MonoGetDisplayHandle(); // Remove the Focus from a Gtk window back to a mono winform X11 window. NativeX11Methods.XSetInputFocus(displayHandle, NativeReplacements.MonoGetX11Window(newTargetHandle), NativeX11Methods.RevertTo.None, IntPtr.Zero); }
protected override void OnLeave(EventArgs e) { if (_browser != null) { _browser.RemoveInputFocus(); if (_browser.WebBrowserFocus != null) { _browser.WebBrowserFocus.Deactivate(); } } _entered = false; base.OnLeave(e); if (Platform.IsWindows) { return; } Action EnsureXInputFocusIsRemovedFromReceivedWinFormsControl = () => { var control = Control.FromHandle(NativeReplacements.MonoGetFocus()); var controlName = control.GetType().FullName; if (controlName == "Gecko.GeckoWebBrowser") { return; } MoveInputFocusBackToAWinFormsControl(); // Setting the ActiveControl ensure a Focus event occurs on the control focus is moving to. // And this allows us to call RemoveinputFocus at the neccessary time. // This prevents keypress still going to the gecko controls when a winform TextBox has focus // and the mouse is over a gecko control. Form.ActiveForm.ActiveControl = control; EventHandler focusEvent = null; // Attach a execute once only focus handler to the Non GeckoWebBrowser control focus is moving too... focusEvent = (object sender, EventArgs eventArg) => { control.GotFocus -= focusEvent; MoveInputFocusBackToAWinFormsControl(); }; control.GotFocus += focusEvent; }; ProgressUtils.InvokeLaterOnUIThread( () => EnsureXInputFocusIsRemovedFromReceivedWinFormsControl()); }