tags:

views:

843

answers:

4

I am using the webbrowser control in visual studio. I think it is a wrapper around internet explorer. Anyway all is going well I am using it in edit mode however I can't get he document's keydown event to fire (in order to catch ctrl+v) anyone had similar problems with it?

Anyone have a solution?

+1  A: 

You should override a "WndProc()" method in derived class from WebBrowser control or in form, which contains a webbrowser. Or you can catch the keys with custom message filter ( Application.AddMessageFilter ). With this way you can also filter a mouse actions.

I had same problems years ago, but I don't remember which way i used.

TcKs
+2  A: 

Indeed the webbrowser control is just a wrapper of the IE browser control. Is your problem that the controls PreviewKeyDown not working? Seems to be working for me as long as the control has focus.

 webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);       

....

 private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
     Console.WriteLine(e.KeyCode.ToString() + "  " + e.Modifiers.ToString());
     if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V) {
         MessageBox.Show("ctrl-v pressed");
     }
 }

but perhaps I am not completely understanding?

sbeskur
+1  A: 

You mentioned the KeyDown event of the 'document'. If you are referring to the WebBrowser control's Document property (type HtmlDocument) it only has events for MouseUp, MouseDown, etc but not keyboard events. You want to register your event handler with the WebBrowser control's PreviewKeyDown delegate. You may also want to set the value of the WebBrowser control's WebBrowserShortcutsEnabled property to false if you don't want standard Internet Explorer shortcuts to have their usual effect. You should also make sure that the WebBrowser control is in focus by manually calling its Focus() method and setting the TabStop property of other controls on the form to false. If this is not possible because you have other controls on the form that need to accept focus, you also might want to add an event handler for the KeyDown event of the Form itself.

Matt V
A: 

How to trap keystrokes in controls by using Visual C#

e.g.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
{  
   const int WM_KEYDOWN = 0x100;  
   const int WM_SYSKEYDOWN = 0x104;  

   if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))  
   {  
      switch(keyData)  
      {  
         case Keys.Down:  
            this.Parent.Text="Down Arrow Captured";  
            break;  

...

      }  
   }  

   return base.ProcessCmdKey(ref msg,keyData);  
}
Jared Updike