How to: View Existing Key Bindings

The Bindings property enables you to view or change the key bindings associated with a specified command. Reading this property retrieves the command's current bindings as an array of objects. Each object contains a string that describes the binding.

Setting a value to the Bindings property assigns one or more new key bindings to the specified command. For more information, see How to: Bind a Command to a Single Shortcut Key and How to: Bind a Command To Multiple Combinations of Shortcut Keys.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Visual Studio Settings.

Viewing existing key bindings

  1. Use the Visual Studio Add-In Wizard to create a new add-in. Name the project and click OK to start the wizard.

    For more information about using the Visual Studio Add-In Wizard, see How to: Create an Add-In.

  2. On the Select a Programming Language page, select either Create an Add-in using Visual C# to run the Visual C# example below, or Create an Add-in Using Visual Basic to run the Visual Basic example.

  3. Paste the example function below to the Connect class in the Visual Studio Add-In Wizard generated code.

  4. Call the function from the OnConnection method as described in How to: Compile and Run the Automation Object Model Code Examples.

  5. To run the Add-in, click Add-in Manager on the Tools menu, select the add-in you created, and click OK.

    A list of all shortcut keys bound to the File.NewFile command displays.

Example

The following example illustrates the use of Bindings by displaying all shortcut keys bound to the File.NewFile command.

Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
  custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    ' Pass the applicationObject member variable to the code example.
    ListKeyBindings(_applicationObject)
End Sub

Sub ListKeyBindings(ByVal dte As DTE2)
    ' Bindings() is an array of key binding string names.
    Dim bindings() As Object
    Dim binding As Object
    Dim msg As String = Nothing
    ' Populate the collection with all of the bindings
    ' for the command File.NewFile.
    bindings = dte.Commands.Item("File.NewFile").Bindings
    For Each binding In bindings
        msg += CStr(binding) & vbCr
    Next
    MsgBox(msg)
 End Sub
// Add-in code.
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst, ref
 System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    //Pass the applicationObject member variable to the code example.
    ListKeyBindings((DTE2)_applicationObject);
}
public void ListKeyBindings(DTE2 dte)
{
    object[] bindings;
    string msg = string.Empty;
    // Populate the collection with all of the bindings associated
    // with the command File.NewFile.
    // Bindings() is an array of key binding string names.
    bindings = (object[])dte.Commands.Item("File.NewFile", 0).Bindings;
    foreach (object b in bindings)
    {
        msg += ((string)b) + "\n";
    }
    System.Windows.Forms.MessageBox.Show(msg);
}

See Also

Tasks

How to: Preserve Existing Command Key Bindings

Concepts

Bindings Property Parameter Format

Other Resources

Binding Add-In Commands to Keys