WebBrowser "steals" KeyDown events from my form

I have it WebBwoserinside Formand I want the Ctrl + O key combination to be used as a shortcut for a menu item. My problem is that if I click on WebBrowserand I press Ctrl + O, the Internet Explorer dialog box will appear, instead of doing what my menu item does. I have a property Form KeyPreviewset to true. In addition, I added an event handler for the event KeyDown, but it stops receiving the call after clicking the button WebBrowser. How can i fix this?

+5
source share
2 answers

This should solve your problem. He turned off the accelerator keys of the web browser.

webBrowser1.WebBrowserShortcutsEnabled = false;

You might want to know if you need one IsWebBrowserContextMenuEnabled.

The following may also solve your problem if you need some accelerator keys that will be active in the browser. However, this approach requires something to catch the focus. MessageBox.Show()and dialog.ShowDialog()can do the job

    private void DoSomething()
    {
        webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);
    }
    private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.O)
        {
            menuItem.PerformClick();
            // MessageBox.Show("Done");
        }
    }
+7
source

You can override the ProcessCmdKey method:

protected override bool ProcessCmdKey(ref Meassage msg, Keys keyDaya)
{
    //Trap for Key down 

    return true; //false if you want to suppress the key press.
}
+2
source

All Articles