Solved

Using the F keys?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now