views:

1274

answers:

2

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?

A: 

Is it possible in your application to handle keydowns in the parent form? We have a form containing a WebBrowser in which we hook into the Application's PreFilterMessage setup and look for keydowns there.

Dave Foster
+5  A: 

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!
    }
}
balexandre