Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Need Help Assigning Keys

Hello
I am Making a reservation program and i have buttons at the top of the program. i.e (Create Reservation, Modify, ect). I would like to be able to press a F key to open what the button links to as well as being able to click the button.
Any Suggestions would be very helpful.
[288 byte] By [Mister Purple] at [2007-11-11 10:02:35]
# 1 Re: Need Help Assigning Keys
First you need to set the forms KeyPreview to True then in the Form's KeyDown Event look for the Function Keys as well as which modifer keys(Alt,Ctrl,Shift) might have been pressed. Then perform what is needed based on which key was pressed. Here is an example that just pops open a messagebox that tells you what you hit, plus I threw in the KeyPreview property in the Form Load Event:
Private Sub Form_Load()
Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ShiftDown As Boolean
Dim AltDown As Boolean
Dim CtrlDown As Boolean
Dim msg As String

ShiftDown = (Shift And vbShiftMask) > 0
AltDown = (Shift And vbAltMask) > 0
CtrlDown = (Shift And vbCtrlMask) > 0

'look for which function key
Select Case KeyCode
Case vbKeyF1
msg = "F1"
Case vbKeyF2
msg = "F2"
Case vbKeyF3
msg = "F3"
Case vbKeyF4
msg = "F4"
Case vbKeyF5
msg = "F5"
Case vbKeyF6
msg = "F6"
Case vbKeyF7
msg = "F7"
Case vbKeyF8
msg = "F8"
Case vbKeyF9
msg = "F9"
Case vbKeyF10
msg = "F10"
Case vbKeyF11
msg = "F11"
Case vbKeyF12
msg = "F12"
End Select
'if a function key was then
If msg <> "" Then
'Find out if Alt, Ctrl, or Shift are also pressed
If AltDown Then msg = "Alt+" & msg
If CtrlDown Then msg = "Ctrl+" & msg
If ShiftDown Then msg = "Shift+" & msg
'Show What Was Pressed
MsgBox "You Pressed " & msg
End If
End Sub
Ron Weller at 2007-11-11 17:23:26 >
# 2 Re: Need Help Assigning Keys
Thanks a bunch man it works beautifly.
Mister Purple at 2007-11-11 17:24:27 >