Partager via


How to: Bind a Command to a Single Shortcut Key

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

In addition to displaying the shortcut key bindings, you can also use the Bindings property to set or change the key bindings for a Visual Studio command. Note that when you change a key binding, it replaces the previous key binding (the old binding is lost). Also, if the new key binding is used by another command, the key binding is also removed from the old command and reassigned to the new command.

There is a way, however, to preserve a key binding so that a new key binding becomes an additional shortcut key rather than replacing the old one. This method is outlined in the topic, How to: Preserve Existing 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 ExportSettings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

Procedure

To bind a command to a shortcut key

  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 in the Connect class of the code generated by the Visual Studio Add-In Wizard.

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

  5. Build and run the Add-in.

    Press F2 to run the File.Newfile command

Example

The following Add-in example demonstrates how to bind the File.NewFile command to a single shortcut key (F2).

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)
    BindingsExample(_applicationObject)
            
End Sub

Sub BindingsExample(ByVal dte As DTE2)
    Dim cmds As Commands
    Dim cmd As Command
    Try
        ' Set references to the Commands collection and the 
        ' File.NewFile command.
        cmds = DTE.Commands
        cmd = cmds.Item("File.NewFile")

        ' Assigns the command (File.NewFile) globally to the F2 key.
        cmd.Bindings = "Global::F2"
        MsgBox("key remapped")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
public void OnConnection(object application, ext_ConnectMode 
connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    BindingsExample(_applicationObject);

}

public void BindingsExample(DTE2 dte)
{
    Commands cmds;
    Command cmd;

    try
    {
        // Set references to the Commands collection and the 
        // File.NewFile command.
        cmds = dte.Commands;
        cmd = cmds.Item("File.NewFile", 1);

        // Assigns the command (File.NewFile) globally to the F2 key.
        cmd.Bindings = "Global::F2";
        System.Windows.Forms.MessageBox.Show("key remapped");
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

See Also

Tasks

How to: Bind a Command To Multiple Keyboard Shortcuts

How to: Preserve Existing Keyboard Shortcuts

Concepts

Bindings Property Parameter Format

Other Resources

Binding Add-In Commands to Keys