Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keyboard shortcut to trigger event 1

Status
Not open for further replies.

Smoothcat74

Technical User
Aug 29, 2008
40
0
0
Hi everyone,

This is probably more basic than I think it is, but I would like to do is enable a form to recognize a combination of keystrokes in order to trigger an event (ie. ctrl+j fires a VBA subroutine). Is there an easy way to go about this?

Thanks in advance!
 
I think I like the option of utilizing the keydown option from the form properties. Would it be possible to get a simple code example of the syntax to use this function?
 

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim intCtrlDown As Integer

    ' Use bit masks to determine which key was pressed.
    intCtrlDown = (Shift And acCtrlMask) > 0
    ' Display message telling user which key was pressed.
    If intCtrlDown And (KeyCode = Asc("j") Or KeyCode = Asc("J")) Then
        MsgBox "You pressed CTRL+j"
    End If
End Sub

Don't forget to set KeyPreview to Yes.

 
Even simpler, is to add a button to the form, with a caption of say "&Do this", which will make Alt+D a shortcut to firing the routine you've placed in the on click of the button.

Also, some shortcut keys are assigned different actions, so you need to take care not to use some that might be used by the user (or those who might cause unexpected behaviour). I think for instance Ctrl+J might give a linefeed character into the control having focus (perhaps also carriage return?).

Setting KeyCode to 0 might help, should such be the case.

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top