Form_KeyDown not working

rex64
08-16-2004, 10:46 AM
No matter what key I press, it will not even display the msgbox inside my KeyDown function. I tried clicking on diffrent parts of the form, to make sure it was in focus, but I still can not get it to work. Letter keys, arrow keys, nothing will display the msgbox keycode. It is like the KeyDown function is not fireing for some reason.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
MsgBox KeyCode
If KeyCode = vbKeyRight Then
rightButton.Click
End If
If KeyCode = vbKeyLeft Then
leftButton.Click
End If
If KeyCode = vbKeyUp Then
upButton.Click
End If
If KeyCode = vbKeyDown Then
downButton.Click
End If
End Sub

00100b
08-16-2004, 10:53 AM
Have you set the form's KeyPreview property to True. If there are controls on the form that have the focus and KeyPreview is not set to True, then the KeyDown events are handled by the control. By specifying that the form will "Preview" keyboard events, then both the form and the control that has the focus will have the event triggered. Note that the form's KeyDown event will fire before the control's KeyDown event.

rex64
08-16-2004, 11:02 AM
Awesome, that works great! Except one problem. It seems like it takes a few button presses to get it to work. Like the first few times you push a button it just moves the focus on the controls, but then it starts working. It seesm like your cursor has to be in a text box or something where it can stay focused on that item. Any ideas? Thanks.

00100b
08-16-2004, 11:12 AM
What are the first few controls in the form?

rex64
08-16-2004, 11:42 AM
They are buttons.
What are the first few controls in the form?

00100b
08-16-2004, 11:52 AM
Let me guess, you're using arrows keys as navigation keys. Am I right?

An arrow key by itself will not be trapped by a command button as a KeyDown event. If you couple the arrow key with a shift mask (Shift, Ctl, Alt), then it will trigger the KeyDown event. KeyPreview is just allowing the form to pre-process the key state for the control that has the focus.

rex64
08-16-2004, 12:03 PM
Good guess :) Are you saying that the user would have to hold down Shift when pressing an arrow key for it to not be confused by a command button?

00100b
08-16-2004, 12:18 PM
The arrow keys are not trapped for by the key state events of a CommandButton control at all. Since the CommandButton isn't trapping it, then the previewing form won't trap it (while that control type has the focus). The same applies for many of the other navigation keys (like Tab).

If you do combine a shift mask with the navigation keypress, then you will need to include the code in the KeyDown (or KeyUp event) of the CommandButton control to tell it how to process the keycode combination (just pressing Shift+Down will not by default function as if just the down arrow key was pressed).

Short of subclassing the form or control, I don't know of an easy method in which to trap navigation key events on a CommandButton.

rex64
08-16-2004, 01:22 PM
Thanks for the help. Maybe I will just delete the buttons. For right now, I just changed the tab order :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum