If I have a command button called Next, and I want users to be able to hit F9 and it be a shortcut for clicking Next, how exactly do I do that? I can't seem to find an example for doing something like that.
Only the object that has the focus can receive a keyboard event. For keyboard events, a form has the focus only if it is active and no control on that form has the focus. This happens only on blank forms and forms on which all controls have been disabled. However, if you set the KeyPreview property on a form to True, the form receives all keyboard events for every control on the form before the control recognizes them. This is extremely useful when you want to perform the same action whenever a certain key is pressed, regardless of which control has the focus at the time.
The KeyDown and KeyUp events provide the lowest level of keyboard response. Use these events to detect a condition that the KeyPress event is unable to detect, for instance:
Private Sub Form_Load()
Form1.KeyPreview = True
Text1.Text = ""
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
cmdNext_Click
End Sub
Private Sub cmdNext_Click()
Text1.Text = "Success"
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.