Export (0) Print
Expand All

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 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 Customizing Development Settings in Visual Studio.

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

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

public void OnConnection(object application, ext_ConnectMode 
connectMode, object addInInst, ref Array custom)
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;


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

        // 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)
© 2015 Microsoft