Hi again, sorry for yet another thread.

I am using the KeyDown function in my form and it is not able to perform because of the other controls. For example, there is a text box in the form. Once the form has loaded, when I press vbKeyRight, my code does not run because the cursor is at the textbox and it is not the form getting the event. The same is true if there is a WebBrowser in the form. When I press vbKeyRight, instead of my codes executing, the browser just scrolls to the right. How do I set the focus back to the form and not my controls? Thanks.

Hi, thanks for the help. The WebBrowser control has no Enabled property. I also don't want to disable it as it is in use mainly like this:

The browser has a navigation control. You can press it to control an ip cam's direction. I made an additional feature that when the arrow keys are pressed, the mouse moves and presses the corresponding button as well.

Setting the KeyPreview property of the Form will enable the Form to process the KeyDown event before any controls on the form do.

Thanks. I used this code:

Form1.KeyPreview = True

I don't know where to put it though. It seems wherever I put it, it has no effect.

Also, taken from msdn

"After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event handlers of the form the TextBox control will receive the key that was pressed."

I really don't want the textbox to process the keypress as well. If possible, I want all keypresses processed by the form. On the same page, it mentions about the KeyPressEventArgs.Handled Property however, I don't know how to use it.

From the VB Help file on the KeyPreview> property of a form ->
Note Some controls intercept keyboard events so that the form can't receive them. Examples include the ENTER key when focus is on a CommandButton control and arrow keys when focus is on a ListBox/Textbox control.

The space bar will invoke a click event on a form.

So all you need to do is to set the forms Keyview property to True and then use the Form_KeyDown or Form_KeyUp events. Again, the form will never accept enter as a click event, that will go to any command button on you form.

The below code is something that you can try, I have not tested it. so who knows...;)

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CHAR = &H102

Call SendMessage(MyFormNameHereHwnd,WM_CHAR,13,0&)

Hi,

Where do I put the KeyPreview code? All I want is for the form to capture W,S,A,D and the 4 arrow keys. Enter and space bar keys are not in use anyway. To be more accurate, the only controls in the way are 3 browser controls. No text boxes or list boxes. :)

I tried your code but I got a "Invalid Outside Procedure" error. :(

Edited 5 Years Ago by martin11ph: n/a

In your IDE, under your forms properties, select "KeyPreview". It's default value is False, change this to true.

You can also do this under one of the following events with code -

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)

End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

End Sub

Thanks. I didn't see that property. I tried it and it works fine if the control is a textbox although as mentioned earlier, it will execute the code under Form_KeyDown and then reflect what was pressed in the textbox.

As for the web browser which is the important thing, it does not work. The Form_KeyDown events do not get executed. :(

Edited 5 Years Ago by martin11ph: n/a

As for the web browser which is the important thing, it does not work. The Form_KeyDown events do not get executed. :(

Did you see my earlier post?

Hi Walt, yes i did.

The next post was my reply.

The WebBrowser control has no Enabled property. I also don't want to disable it as it is in use mainly like this:

The browser has a navigation control. You can press it to control an ip cam's direction. I made an additional feature that when the arrow keys are pressed, the mouse moves and presses the corresponding button as well.

I quoted the wrong part of your post. Sorry. I meant

I tried it and it works fine if the control is a textbox although as mentioned earlier, it will execute the code under Form_KeyDown and then reflect what was pressed in the textbox.

If you don't want to reflect the character int the textbox set the KeyCode variable to 0 in the form_keydown event

Thanks both for your help but I only used the textbox as an example. What I really need is for it to work on browsers.

Comments
Then don't ask question you don't want an answer to. It wastes everyone's time.

@Walt,
Then don't ask question you don't want an answer to. It wastes everyone's time.

I don't know why you think I don't want this answered. Ever since my first post, I already mentioned the browser and although the solutions provided worked for textboxes, it does not for browsers.

You said:
Set the textbox ENABLED property to FALSE. The WebBrowser probably has the same value.

I said:
The WebBrowser control has no Enabled property.

Why would you be mad at me for that?

Sorry, I forgot about this thread it seems. The solution is actually quite simple...;)

Set your form KeyPreview to true. The trick lies in the calling of the key used by setting the keycode to 0 after the call of the key.

Enjoy, I'm sure this is worth some rep points....:)

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode
Case vbKeyW:
    MsgBox "W"
    KeyCode = 0
Case vbKeyS:
    MsgBox "S"
    KeyCode = 0
Case vbKeyA:
    MsgBox "A"
    KeyCode = 0
Case vbKeyD:
    MsgBox "D"
    KeyCode = 0
End Select
End Sub

Hi Andre,

It still does not work with Browsers though. You and ChrisPadgham are right with the KeyCode=0 though. This would negate anything on the textboxes.

As for the browser solution here is what I did. I merely placed a picture box with a background color the same as the form. After all the modifications have finished on the browser, I called:

Picture1.SetFocus

This took the focus out of the browser and allowed me to continue.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode
Case vbKeyW:
    MsgBox "W"
Case vbKeyS:
    MsgBox "S"
Case vbKeyA:
    MsgBox "A"
Case vbKeyD:
    MsgBox "D"
End Select
KeyCode = 0
End Sub

I also found out that this accomplishes the same. Thanks for all the help and will close now.

Thats quite funny because I have tested the code with just a webbrowser on my form, no other controls at all and it was working perfect...:)

Did you add the "Keycode = 0" after every call as per my code, or only at the end?

Thanks for marking the post.:)

Now that is weird. Could it be my VB again that is the culprit? Yes, I tried your code as well. The KeyPreview just doesn't seem to affect the Browser for me. In relation to the other thread, I think I may not have installed SP6.

It shouldn't be. Maybe a wrong setting somewhere, but if it works, all cool. I believe in the saying "if it's not broken, don't repair it!";)

This question has already been answered. Start a new discussion instead.