Solved

Using the F keys?

Posted on 2000-05-01
3
154 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month5 days, 3 hours left to enroll

636 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