Solved

Using the F keys?

Posted on 2000-05-01
3
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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