Export (0) Print
Expand All

How to: Bind a Command to a Single Shortcut Key 

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 Command Key Bindings.


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.


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. In the left pane of the Options dialog box, expand the Environment folder and select Keyboard.

    Ensure that the name of the vsk file you renamed in step 7 appears in the Apply the following additional keyboard mapping scheme drop-down menu.

  5. Before running the Add-in example, make sure that the Keyboard bindings are set to (Default). You can do this by clicking Reset in the Keyboard pane of the Options dialog box.

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

  7. Build the Add-in.

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

    The command is bound to a single global shortcut key. You can test the keyboard binding by pressing CTRL+SHIFT+ALT+X which causes the New File dialog box to appear.


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)

See Also

Community Additions

© 2014 Microsoft