Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shortcut to command button?

Status
Not open for further replies.

demoniac

Programmer
Jun 14, 2001
63
US
Hello again :)

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.

Thanks :eek:)
demoniac
 
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

I hope this helps
 
oops...

I for got to tell you to capture the KeyCode... for F9 it is 120

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 120 Then
Call cmdNext_Click
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top