protected override void OnLeave(EventArgs e) { if (_browser != null) { _browser.RemoveInputFocus(); if (_browser.WebBrowserFocus != null) { _browser.WebBrowserFocus.Deactivate(); } } _entered = false; base.OnLeave(e); #if __MonoCS__ 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()); #endif }
/// <summary> /// If Browser control dosesn't have X11 input focus. /// then Ensure that it does.. /// </summary> protected void EnsureFocusedGeckoControlHasInputFocus() { if ((_browser == null) || (_browser.HasInputFocus())) { return; } // Attempt to do it right away. this._browser.SetInputFocus(); // Othewise do it on the first idle event. Action setInputFocus = null; setInputFocus = () => { ProgressUtils.InvokeLaterOnIdle(() => { if (_browser == null) { return; } if (Form.ActiveForm == null || _browser.ContainsFocus == false) { MoveInputFocusBackToAWinFormsControl(); return; } if (!_browser.HasInputFocus()) { this._browser.SetInputFocus(); } if (!_browser.HasInputFocus()) { setInputFocus(); } }, null); }; if (!_browser.HasInputFocus()) { setInputFocus(); } }