Disable Shortcuts in WebBrowser Control and keep Copy and Paste

Aug 19, 2012 by     2 Comments    Posted under: Programming

It took me a while to figure this one out (admittedly it shouldn’t have taken as long as it did!), so hopefully this will save someone else banging their head against the desk if they run into the same issue.

The Problem

I have a WebBrowser control which navigates to about:blank and then loads HTML into the document from an embedded resource and then is altered afterwards to show incoming data. As this document is being built in real time, it causes issues when the user hits the F5 key, as it refreshes about:blank and shows them a blank screen again.

My initial solution to this was to just set the WebBrowserShortcutsEnabled property to false, gave it a test, it stopped it refreshing, all was good with the world again! Or not…

The WebBrowserShortcutsEnabled property will also disable CTRL+C and CTRL+V, although I don’t need the paste shortcut, I do need the copy shortcut.

The Solution

The solution is actually very simple, and I’m not sure why it took so long to come to the conclusion (I blame lack of Red Bull flowing through my veins!). But again – in case anyone is banging their heads against their desk, this is what you need to do:

Set the WebBrowserShortcutsEnabled property to false as previously mentioned and create an event handler for the PreviewKeyDown event of the WebBrowser control and add the code below to it

If you need to add more functionality back (i.e. cut and paste), the ExecCommand method also accepts Cut and Paste as commands, so just repeat the code above to pick up the X and V keys too and execute the appropriate command.

Additional Step

If also like me, you need to stop the user from navigating to another page from within your WebBrowser control, add an event handler for the Navigating event of your WebBrowser control and add the following:

This will force all pages to open in a new window, and if you set the Tag property to be true after you have loaded your initial content into the page, will prevent a link to about:blank sending the user back to a blank page!

2 Comments + Add Comment

  • Another solution which does not require you to invoke any copy command is to change the WebBrowserShortcutsEnabled settings for the keys you want to allow, for example :

    webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);

    // Slave the WebBrowserShortcutsEnabled settings based on keys type by user.
    // It allow fine graind control on which keys are allowed to be used without giving
    // too much shortcuts possibility (which could mess up the control usage )
    private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
    switch (e.KeyData)
    {
    case Keys.Control | Keys.A: // Select All
    case Keys.Control | Keys.C: // Copy
    case Keys.Control | Keys.V: // Paste
    case Keys.Delete: // Delete
    ((WebBrowser)sender).WebBrowserShortcutsEnabled = true;
    break;
    default:
    ((WebBrowser)sender).WebBrowserShortcutsEnabled = false;
    break;
    }
    }

    • That’s a good idea, Olivier! Much cleaner than doing the invoking.

Got anything to say? Go ahead and leave a comment!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>