ArcObjects Library Reference

Assign Shortcut Key to Command Snippet

Assign a shortcut key to a specific tool.

[C#]

///<summary>Assign a shortcut key to a specific tool.</summary>
///
///<param name="application">An IApplication interface.</param>
///<param name="shortcutKey">A System.Windows.Forms.Keys class that is the key on the keyboard to be the shortcut. Example: System.Windows.Forms.Keys.Z</param>
///<param name="commandName">A System.String that is the name of the command to return. Example: "esriArcMapUI.ZoomInTool"</param>
/// 
///<returns>A System.Boolean that gives the result of adding the shortcut key to the tool, where true = success or false = failure</returns>
/// 
///<remarks>Refer to the EDN document http://edndoc.esri.com/arcobjects/9.1/default.asp?URL=/arcobjects/9.1/ArcGISDevHelp/TechnicalDocuments/Guids/ArcMapIds.htm for a listing of available CLSID's and ProgID's that can be used as the commandName parameter.</remarks>
public System.Boolean AssignShortcutKeyToCommand(ESRI.ArcGIS.Framework.IApplication application, System.Windows.Forms.Keys shortcutKey, System.String commandName)
{
  
  // Define key combination to be assigned
  System.Windows.Forms.Keys key = shortcutKey; // example: System.Windows.Forms.Keys.Z;
  System.Boolean keyCtrl = true;
  System.Boolean keyAlt = true;
  System.Boolean keyShift = false;

  //You should test if key combination has already been used by another command
  //before assigning. Otherwise, it has to be removed first.
  ESRI.ArcGIS.Framework.IAcceleratorTable acceleratorTable = application.Document.Accelerators;
  ESRI.ArcGIS.Framework.IAccelerator accelerator = acceleratorTable.FindByKey((System.Int32)key, keyCtrl, keyAlt, keyShift);
  if (accelerator == null)
  {
    ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();

    //Command to be assigned to
    uid.Value = commandName; // example: "esriArcMapUI.ZoomInTool"; 
    System.Boolean isAssigned = acceleratorTable.Add(uid, (System.Int32)key, keyCtrl, keyAlt, keyShift);
    return isAssigned;
  }
  return false;
}
[Visual Basic .NET]

'''<summary>Assign a shortcut key to a specific tool.</summary>
'''
'''<param name="application">An IApplication interface.</param>
'''<param name="shortcutKey">A System.Windows.Forms.Keys class that is the key on the keyboard to be the shortcut. Example: System.Windows.Forms.Keys.Z</param>
'''<param name="commandName">A System.String that is the name of the command to return. Example: "esriArcMapUI.ZoomInTool"</param>
''' 
'''<returns>A System.Boolean that gives the result of adding the shortcut key to the tool, where true = success or false = failure</returns>
''' 
'''<remarks>Refer to the EDN document http://edndoc.esri.com/arcobjects/9.1/default.asp?URL=/arcobjects/9.1/ArcGISDevHelp/TechnicalDocuments/Guids/ArcMapIds.htm for a listing of available CLSID's and ProgID's that can be used as the commandName parameter.</remarks>
Public Function AssignShortcutKeyToCommand(ByVal application As ESRI.ArcGIS.Framework.IApplication, ByVal shortcutKey As System.Windows.Forms.Keys, ByVal commandName As System.String) As System.Boolean

  ' Define key combination to be assigned
  Dim key As System.Windows.Forms.Keys = shortcutKey ' example: System.Windows.Forms.Keys.Z;
  Dim keyCtrl As System.Boolean = True
  Dim keyAlt As System.Boolean = True
  Dim keyShift As System.Boolean = False

  ' You should test if key combination has already been used by another command
  ' before assigning. Otherwise, it has to be removed first.
  Dim acceleratorTable As ESRI.ArcGIS.Framework.IAcceleratorTable = application.Document.Accelerators
  Dim accelerator As ESRI.ArcGIS.Framework.IAccelerator = acceleratorTable.FindByKey(CType(key, System.Int32), keyCtrl, keyAlt, keyShift)

  If accelerator Is Nothing Then

    Dim uid As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass

    ' Command to be assigned to
    uid.Value = commandName ' example: "esriArcMapUI.ZoomInTool";
    Dim isAssigned As System.Boolean = acceleratorTable.Add(uid, CType(key, System.Int32), keyCtrl, keyAlt, keyShift)

    Return isAssigned

  End If

  Return False

End Function


Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Framework
  • ESRI.ArcGIS.System
  • System
  • System.Windows.Forms