2

PROBLEM:

I have a WebBrowser and have exposed its ActiveX methods. In my Form in which the WebBrowser is positioned I have a MainMenuStrip with shortcuts. Only when I am typing in a textbox on my Form do the shortcuts properly work. For example, when I press CTRL + N, a shortcut of my Form - instead of a new Form opening, the page the WebBrowser is currently on opens in IE.

WHAT I'VE TRIED:

  1. I have tried focusing the form every 100ms using a timer which didn't work (this would not be a viable option anyway as it is not very subtle and the program has to carefully navigate the WebBrowser using SendKeys etc).

  2. Code for WebBrowser:

    this.webBrowser1.WebBrowserShortcutsEnabled = false;
    this.webBrowser1.AllowWebBrowserDrop = false;
    this.webBrowser1.IsWebBrowserContextMenuEnabled = false;
    
  3. Inheriting from WebBrowser and overriding ProcessCmdKey:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.N))
        {
            newToolStripMenuItem.PerformClick();
            return true; // or false
        }
    }
    

WHAT I'M TRYING TO ACHIEVE:

  1. That the WebBrowser's shortcuts are disabled (I read somewhere that the ActiveX prevents this...why?).

  2. What I would like is that unless one is currently typing/navigating in the WebBrowser the Form's shortcuts work. Focus should be taken from the WebBrowser WHEREEVER there's a click outside the control, not just when other textboxes are in focus (e.g. when there's a click in the blank of the form.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Patrick Geyer
  • 1,515
  • 13
  • 30
  • Can you clarify what you mean by exposing its ActiveX methods? – Evan Harper Jul 09 '13 at 00:04
  • @EvanHarper, I'll give it a shot: private SHDocVw.WebBrowser_V1 Web_V1; Web_V1 = (SHDocVw.WebBrowser_V1)webBrowser1.ActiveXInstance; That's the code at least. Not sure I fully understand it though! – Patrick Geyer Jul 09 '13 at 00:08
  • Does the code actually *do* anything with that Web_V1 reference it's concoted? That's the important part. – Evan Harper Jul 09 '13 at 00:16
  • Yes, I later use it to get a mshtml.Document for which you need to expose ActiveXInterface because can't convert from System.Windows.Forms.WebBrowser.HtmlDocument to mshtml.HTMLDocument normally. Please say if this is unnecessary because it's quite inconvenient. – Patrick Geyer Jul 09 '13 at 00:29
  • possible duplicate of [WebBrowser keyboard shortcuts](http://stackoverflow.com/questions/1980515/webbrowser-keyboard-shortcuts) – Sheng Jiang 蒋晟 Jul 09 '13 at 00:31
  • Ah. Thanks for linking. I'll have a go with the Document.Properties tomorrow. – Patrick Geyer Jul 09 '13 at 00:35

2 Answers2

0

Try inheriting from WebBrowser control and override Control.IsInputKey. See if Ctrl+N arrives in there.

noseratio
  • 59,932
  • 34
  • 208
  • 486
0

After investigating a lot, we came to know it is browser compatibility issue.

We have added meta tag into the HTML page,then shortcuts are working fine. Below is the sample code.

<html>
<body>
<Head>
<meta http-equiv="X-UA-Compatible" content="IE=IE8" />
</head>
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form></body>
</html>

There are three different solutions for this problem.

Adding meta tag to make the Web site browser compatible.

Override "PreocessCmdKey" method and handle the shortcuts.

Emulate browser by adding the key under FEATURE_BROWSER_EMULATION.

If you don't want to set the meta tag in html code, you can assign meta tag to the Document text property of webbrowser control before navigating the URL. Below is the sample.

    //Setting compatible mode of IE.
                    this.m_oWebBrowser.DocumentText = @"<html>
                      <head><meta http-equiv=""X-UA-Compatible"" content=""IE=IE8"" /> </head>
                      <body></body>
                      </html>";
this.m_oWebBrowser.Navigate("www.google.com");