Share via


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 Keyboard Shortcuts.

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 Export Settings on the Tools menu. For more information, see Visual Studio Settings.

Viewing existing key bindings

  1. Create an add-in.

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

  2. Add a reference to System.Windows.Forms, and add this namespace to the using (or Imports) statements for the Connect class.

  3. Paste the function below into the Connect class in the code.

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

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

Example

The following example displays all the 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 Keyboard Shortcuts

Concepts

Bindings Property Parameter Format

Other Resources

Binding Add-In Commands to Keys