0

This question is related to Visual Basic .NET 2010

Hey. I need to capture keys from a window handle on my form. This is what my code does:

  1. Use a webbrowser control to navigate to a website
  2. Get window handle of an object on the webbrowser
  3. user32.SetParent() to a new UserControl on my form

Now the object that initially was on the webbrowser is displayed in a control on my form, but any keys sent to that control don't register on the events .KeyDown() and .KeyUp().

So how do I capture the keys from the window handle?

Jeff Reed
  • 231
  • 1
  • 6
  • 23
  • 1
    Try setting KeyPreview on the form to true and then catch them at the form level. – Steve Aug 15 '14 at 15:10
  • What is the purpose of SetParent? – Cody Gray - on strike Aug 15 '14 at 15:58
  • The only kind of "objects" in a WebBrowser that have a window handle are ActiveX controls. Their interaction with the ActiveX host is pretty elaborate, such a control typically implements about a dozen interfaces. For such things as passing keyboard messages between the control and the host so the host can recognize shortcut keystrokes. Changing the parent is illegal, it breaks the contract. You can't make this work. – Hans Passant Aug 15 '14 at 16:17
  • @Hans I've already got it to work. You forgot about Java applets. – Jeff Reed Aug 15 '14 at 18:14
  • 1
    Odd, I could have sworn that somebody forgot to mention that in the question. Why do you think they are not ActiveX hosted? – Hans Passant Aug 15 '14 at 18:17
  • @Steve You mean that I should capture them using ParentForm.KeyUp/Down? Doesn't seem to work on the control. – Jeff Reed Aug 15 '14 at 18:17
  • Worth a try. You can try a windows API `SetWindowsHookEx`. I know it works for capturing mouse and keyboard within your own windows but for VB it will not work outside your windows. This site has a good VB tutorial: http://www.developer.com/net/vb/article.php/1502401/Managing-Low-Level-Keyboard-Hooks-with-the-Windows-API.htm – Steve Aug 15 '14 at 19:59

0 Answers0