[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!!
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.
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
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.
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.
Re: Trap Ctrl +N in VB Web Browser Control
I set the keyup event to be
VB Code:
If Me.ActiveControl = webBrowser Then
MsgBox KeyCode
End If
which produces the msgbox for normal keypresses but not Ctrl+N...any ideas?
No joy on the second method either.
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.
Re: Trap Ctrl +N in VB Web Browser Control
Sorry It will be Form_KeyDown event.
Re: Trap Ctrl +N in VB Web Browser Control
Try this..
VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim lc As Integer
Dim ln As Integer
lc = GetAsyncKeyState(vbKeyControl)
ln = GetAsyncKeyState(vbKeyN)
If Me.ActiveControl = webBrowser Then
If lc And ln Then
MsgBox "Control-N!"
End If
End If
End Sub
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:
' Set the form's [b]KeyPreview = True[/b] at design time
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim CtrlDown
CtrlDown = (Shift And vbCtrlMask) > 0
If Me.ActiveControl = WebBrowser1 Then
If CtrlDown And KeyCode = vbKeyN Then
KeyCode = 0 'Discard the key
End If
End If
End Sub
Re: Trap Ctrl +N in VB Web Browser Control
Cool, thanks iPrank, that worked :)
Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control
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.
Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control
Yes, download the WBCustomizer.dll I posted a link to in post #7 above.
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.
Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control
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.
Re: [RESOLVED]Trap Ctrl +N in VB Web Browser Control
??? I have no problems selecting text while using it.
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.