Assign Shortcut Keys Programmatically

Created:01/15/1999
Description:

The AssignShortcutKey VBA macro shows how to programmatically assign shortcut keys to a command. In this example, the shortcut key CTRL+A is assigned the Add Data command in ArcMap.

Usage: ThisDocument.Accelerators.Add (command ID, Key, Ctrl, Alt, Shift)

The built in ArcID module is used to find the command ID for AddData. The VB Keycode Constants are used to specify the key. Since the shortcut key in this example will be CTRL+A, True is specified for the Ctrl argument, and False is used for both the Alt and Shift arguments.

Note: All programmatic customizations are temporary. If you programmatically customize ArcMap, these changes will only appear while the current document is open in the current ArcMap session. Programmatic changes are never saved in the document or templates. Once you close that document or shutdown ArcMap, the changes are removed. If you are customizing ArcCatalog, these changes will only appear during the current ArcCatalog session.


How to use:
  1. Paste this code into a module in the Visual Basic Editor in ArcMap.
  2. Run the AssignShortcutKey macro.
  3. Test the CTRL+A shortcut key. This should bring up the Add Data dialog.
Public Sub AssignShortcutKey()
  Application.Document.Accelerators.Add _
        ArcID.File_AddData, vbKeyA, True, False, False
End Sub