English

How To: Add an accelerator key using ArcObjects

Summary

An accelerator key is the mapping of a particular keyboard sequence to execute a command.

Procedure

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument then right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents, open the Normal.mxt ThisDocument code module instead.

  4. Copy the following code into the code module.

    Code:
    Public Sub myShortcut()
    Dim mAccTab As IAcceleratorTable
    Set mAccTab = ThisDocument.Accelerators
    Dim newAcc As Boolean
    newAcc = mAccTab.Add("Project.ThisDocument.mymacro", vbKeyZ, False, True, False)
    End Sub

    Public Sub mymacro()
    MsgBox "This test worked"
    End Sub

    For example purposes, 'mymacro' displays a test string when the Alt-Z keyboard combination is pressed. You can substitute the message with the command of your choice, as well as the key sequence.
  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.