Solved

Using the F keys?

Posted on 2000-05-01
3
150 Views
Last Modified: 2010-05-02
I would like to use for example the F2 key as a shortcut to run a function normally coded under a button. How do i do this?
0
Comment
Question by:hutchison
  • 2
3 Comments
 
LVL 18

Expert Comment

by:mdougan
ID: 2767495
Set the Form's KeyPreview property to true.

In the Form's KeyDown event you'll see that the system passes it a parm with a name something like KeyCode.  Put some code in like:

if KeyCode = vbKeyF1 then msgbox "The F1 Key was pressed"
0
 
LVL 18

Accepted Solution

by:
mdougan earned 100 total points
ID: 2767525
If you want to get fancier, you can also look at the value of the Shift parameter:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ShiftTest as Integer
Dim sMsg as String    
   
   If KeyCode = vbKeyF2 Then

' if you actually want to call the button click event
'     cmdMyButton_Click

' or if you want to call a Sub routine

'      mySub "Hello World"

      sMsg = "Key F2 was Pressed"
     
      ShiftTest = Shift And 7  
      Select Case ShiftTest      
         Case 1 ' or vbShiftMask
                 sMsg = sMsg & " and You pressed the SHIFT key."    
         Case 2 ' or vbCtrlMask
                 sMsg = sMsg &  " and You pressed the CTRL key."      
         Case 4 ' or vbAltMask
                 sMsg = sMsg & " and You pressed the ALT key."      
         Case 3
                 sMsg = sMsg & " and You pressed both SHIFT and CTRL."    
         Case 5
                 sMsg = sMsg & " and You pressed both SHIFT and ALT."      
         Case 6
                 sMsg = sMsg & " and You pressed both CTRL and ALT."    
         Case 7
                 sMsg = sMsg &  " and You pressed SHIFT, CTRL, and ALT."      
      End Select

      msgbox sMsg
   End if

End Sub


0
 
LVL 15

Expert Comment

by:StingRaY
ID: 2768412
Try this:
If you use F2 key for access menubar.
Try to set Short cut for that menu, assign it to F2.

But if you want to use F2 key for any task/routine or anything else...

Set Form.KeyPreview = True
and use Form_KeyDown to handle it.
eg.
Private Sub Form1_KeyDown(KeyCode As Integer, Shift As Integer)
    If Shift = 0 Then ' Shift is not pressed
        Select Case KeyCode
            Case vbKeyF2
                   ' Your code....
             Case Else
         End Select
    End If
End Sub
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question