处理 WebBrowser 控件上的按键事件

发布于 2024-07-14 15:53:12 字数 265 浏览 8 评论 0原文

我在 C# 应用程序中使用 WebBrowser 控件,并且希望在 WebBrowser 获得焦点时处理所有关键事件,无论单个内容元素(输入字段、链接等)如何.) 是重点。 我尝试简单地向浏览器控件 KeyDown 事件添加一个事件处理程序,但这不起作用。 我不想将处理程序显式挂接到每个可聚焦的 HtmlElement

在将所有按键事件传递到浏览器或其内容元素之前,如何接收它们?

I am using the WebBrowser control in a C# application and want to handle all key events while the WebBrowser has the focus, regardless what individual content element (input field, link, etc.) is focused. I tried to simply add an event handler to browser controls KeyDown event, but this does not work. I don't want to explicitly hook a handler to each focusable HtmlElement.

How can I receive all key events before they are passed to the browser or its content elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独守阴晴ぅ圆缺 2024-07-21 15:53:12

您有 PreviewKeyDown 事件只需挂钩起来吧。

private void wb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    // your code handling the keys here, like:
    if (e.Control && e.KeyCode == Keys.C)
    {
        // Do something funny!
    }
}

you have the PreviewKeyDown event just hook it up.

private void wb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    // your code handling the keys here, like:
    if (e.Control && e.KeyCode == Keys.C)
    {
        // Do something funny!
    }
}
节枝 2024-07-21 15:53:12

如果您想在 WebBrowser 控件中执行诸如绕过 Enter 键之类的操作,那么您就不走运了,因为该控件没有 KeyPress 或 KeyDown 事件。 KeyPreviewDownEventArgs 不提供任何规避按键的方法。 执行此操作的唯一方法是覆盖承载控件的表单的 ProcessCmdKey 函数。 例如:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean

    If keyData <> Keys.Enter Then Return MyBase.ProcessCmdKey(msg, keyData)
    Return True

End Function

If you want to do something like circumventing the Enter key in the WebBrowser control you are out of luck because there is no KeyPress or KeyDown events for the control. KeyPreviewDownEventArgs does not provide any way to circumvent a key press. The only way to do that is to overide the ProcessCmdKey function of the form that hosts the control. For Example:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean

    If keyData <> Keys.Enter Then Return MyBase.ProcessCmdKey(msg, keyData)
    Return True

End Function
蹲墙角沉默 2024-07-21 15:53:12

您可以将键处理程序添加到已加载文档的 Body 元素。 默认情况下,这会捕获 body 元素的任何子元素中发生的相同事件。

webBrowser.Document.Body.KeyDown += MyKeyDownHandler;
...
private void MyKeyDownHandler(object sender, HtmlElementEventArgs e)
{
    // Set e.ReturnValue false if you want to cancel the key press
}

我认为必须在文档加载后添加处理程序,例如在 DocumentCompleted 事件处理程序中。

You can add key handlers to the Body element of the loaded document. By default this catches the same event occurring in any child element of the body element.

webBrowser.Document.Body.KeyDown += MyKeyDownHandler;
...
private void MyKeyDownHandler(object sender, HtmlElementEventArgs e)
{
    // Set e.ReturnValue false if you want to cancel the key press
}

I think the handler has to be added after the document has loaded, e.g. in the DocumentCompleted event handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文