简体   繁体   English

C#Webbrowser控件键盘按下不触发

[英]C# Webbrowser control keydown not firing

I have a System.Windows.Forms webbrowser control that is not firing the keydown events. 我有一个System.Windows.Forms Webbrowser控件,它不触发按键事件。

Here is the code: the mouse down event fires, but the keydown event does not. 这是代码:触发鼠标按下事件,但不会触发keydown事件。 Are there any criteria for the keydown event triggering? 是否有任何触发按键事件的条件?

IWebBrowser2 browser;
        mshtml.HTMLDocument doc;
        mshtml.HTMLDocumentEvents2_Event iEvent;

        browser = (IWebBrowser2)_browser.ActiveXInstance;

        if (browser == null)
            return;

        doc = (HTMLDocument)browser.Document;

        if (doc == null)
            return;

        iEvent = (HTMLDocumentEvents2_Event)doc;

        if (iEvent != null)
        {
            iEvent.onkeydown += new mshtml.HTMLDocumentEvents2_onkeydownEventHandler(keyDown);
            iEvent.onmousedown += new HTMLDocumentEvents2_onmousedownEventHandler(iEvent_onmousedown);
        }
    }

Try using PreviewKeyDown event instead of KeyDown . 尝试使用PreviewKeyDown事件代替KeyDown Since PreviewKeyDown fires before KeyDown event. 由于PreviewKeyDownKeyDown事件之前触发。

Check this link for more information on how to bind this event. 检查此链接以获取有关如何绑定此事件的更多信息。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM