Results 1 to 19 of 19

Thread: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

  1. #1

    Thread Starter
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Resolved [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    Guys,

    I need to prevent a new IE window being opened when Ctrl +N (Equiv of selecting File -> New Window in IE) is pressed in the Web Browser control in my project. All other new window events are trapped using the NewWindow2 event, but according to MSDN a Ctrl+N keypress is not catchable using this method. Does anyone have any funkeh little code tricks that allow me to detect when Ctrl+N has been pressed and stop the web browser control from launching the new window?

    Thanks in Advance!!
    Last edited by Hack; Feb 7th, 2006 at 09:00 AM. Reason: Added green "resolved" checkmark

  2. #2
    Lively Member
    Join Date
    Sep 2005
    Posts
    105

    Re: Trap Ctrl +N in VB Web Browser Control

    Well, have you tried the keypress event?
    You should be able to detect it with that, and then as for the new window - you could try a very unstable method of attaching a timer with a 1000 interval to sendkeys alt F4 and to disable itself.
    This will close the active window in 1 second - very messy, but it's all that I know of - there may be an API call to close windows. Use this after detecting if such a window is open every 10 millisecons or so with a timer.
    I'm sorry that I can't help you with which APIs to use, I'm really bad with them.

  3. #3

    Thread Starter
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Re: Trap Ctrl +N in VB Web Browser Control

    The keypress event doesn't fire when the webbrowser control is in focus, and the control itself doesn't have one. I did try to get that to work already, but no joy. Unstable methods are no good, coz this will be used at work

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Trap Ctrl +N in VB Web Browser Control

    Set the form's KeyPreview = True and then use Form_Keyup event.
    Check that Me.ActiveControl is the webbrowser.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Trap Ctrl +N in VB Web Browser Control

    Idea #2: (Not tested)
    Add a hidden menu in the form with Ctrl+N shortcut key and with no code inside. See what happens.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6

    Thread Starter
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Re: Trap Ctrl +N in VB Web Browser Control

    I set the keyup event to be
    VB Code:
    1. If Me.ActiveControl = webBrowser Then
    2.     MsgBox KeyCode
    3. End If
    which produces the msgbox for normal keypresses but not Ctrl+N...any ideas?

    No joy on the second method either.

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Trap Ctrl +N in VB Web Browser Control

    Download the WBCustomizer.dll from MS, it allows you to turn specific accelerator keys on and off. It can also turn the popup menu off.

  8. #8
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Trap Ctrl +N in VB Web Browser Control

    Sorry It will be Form_KeyDown event.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Trap Ctrl +N in VB Web Browser Control

    Try this..
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. Dim lc As Integer
    3. Dim ln As Integer
    4.  
    5.     lc = GetAsyncKeyState(vbKeyControl)
    6.     ln = GetAsyncKeyState(vbKeyN)
    7.    
    8.     If Me.ActiveControl = webBrowser Then
    9.         If lc And ln Then
    10.             MsgBox "Control-N!"
    11.         End If
    12.     End If
    13. End Sub
    Last edited by jcis; Feb 7th, 2006 at 07:15 AM.

  10. #10
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Trap Ctrl +N in VB Web Browser Control

    There is no need to use GetAsyncKeyState. The KeyDown event itself is providing us the keycode and Shift status.
    VB Code:
    1. ' Set the form's [b]KeyPreview = True[/b] at design time
    2. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    3.  
    4. Dim CtrlDown
    5. CtrlDown = (Shift And vbCtrlMask) > 0
    6.  
    7. If Me.ActiveControl = WebBrowser1 Then
    8.  If CtrlDown And KeyCode = vbKeyN Then
    9.     KeyCode = 0 'Discard the key
    10.  End If
    11. End If
    12.  
    13. End Sub
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  11. #11

    Thread Starter
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Re: Trap Ctrl +N in VB Web Browser Control

    Cool, thanks iPrank, that worked

  12. #12
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    You are welcome.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    I found an annoying problem: for some reason the control seems to prevent menu items from functioning with their associated keyboard shortcuts, if I push Ctrl + L then nothing happens. I don't really feel like duplicating the functionality into form's KeyDown. Does anyone else have this problem? And solving this would be great.


    Edit Atm I workaround by detecting non-Shift masked KeyDown's and by moving focus to another control. A more solid solution would be great though, bubblegum code is never nice.
    Last edited by Merri; Mar 1st, 2006 at 06:21 AM.

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    Yes, download the WBCustomizer.dll I posted a link to in post #7 above.

  15. #15
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    Is there a DLL file there anywhere? There is the source, but I can't compile it since I don't have a C compiler. And an extra file doesn't really sound like a nice thing to have (trying to keep files to a minimum), so any other solution would be nice.

  16. #16
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    Found the DLL from a ZIP file on this page: http://www.novell.com/coolsolutions/tools/13919.html

  17. #17
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    The DLL file prevents me from selecting text, which is a very important thing to have, so that shuts DLL out. It did fix a few issues I had, but the negatives are greater than the positives.

  18. #18
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    ??? I have no problems selecting text while using it.

  19. #19
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control

    I use a customized page, but it does it for me in every single page I open when I test them, thus eliminating it has something to do with the customized page only. Maybe it is some kind of silly "must be English OS to work properly" bug, wouldn't be the first I've seen.


    Edit Found the official source for the actual DLL file, not the source code only: Microsoft Knowledge Base

    However, this DLL is no different, I can't select text.
    Last edited by Merri; Mar 1st, 2006 at 08:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width