Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using the F keys?

Posted on 2000-05-01
3
Medium Priority
?
159 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 300 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 19 hours left to enroll

916 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