Bind keyboard shortcuts to menu items

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

To bind a keyboard shortcut to a custom menu command, just add an entry to the .vsct file for the package. This topic explains how to map a keyboard shortcut to a custom button, menu item, or toolbar command, and how to apply the keyboard mapping in the default editor or limit it to a custom editor.

To assign keyboard shortcuts to existing Visual Studio menu items, see Identify and customize keyboard shortcuts.

Choose a key combination

Many keyboard shortcuts are already used in Visual Studio. You should not assign the same shortcut to more than one command because duplicate bindings are hard to detect and may also cause unpredictable results. Therefore, it is a good idea to verify the availability of a shortcut before you assign it.

To verify the availability of a keyboard shortcut

  1. In the Tools > Options > Environment window, select Keyboard.

  2. Make sure that Use new shortcut in is set to Global.

  3. In the Press shortcut keys box, type the keyboard shortcut that you want to use.

    If the shortcut is already used in Visual Studio, the Shortcut currently used by box will show the command that the shortcut currently calls.

  4. Try different combinations of keys until you find one that is not mapped.

    Note

    Keyboard shortcuts that use Alt may open a menu and not directly execute a command. Therefore, the Shortcut currently used by box may be blank when you type a shortcut that includes Alt. You can verify that the shortcut does not open a menu by closing the Options dialog box and then pressing the keys.

    The following procedure assumes that you have an existing VSPackage with a menu command. If you need help doing that, take a look at Create an extension with a menu command.

To assign a keyboard shortcut to a command

  1. Open the .vsct file for your package.

  2. Create an empty <KeyBindings> section after the <Commands> if it is not already present.

    Warning

    For more information about key bindings, see Keybinding.

    In the <KeyBindings> section, create a <KeyBinding> entry.

    Set the guid and id attributes to those of the command you want to invoke.

    Set the mod1 attribute to Control, Alt, or Shift.

    The KeyBindings section should look something like this:

    <KeyBindings>
        <KeyBinding guid="<name of command set>" id="<name of command id>"
            editor="guidVSStd97" key1="1" mod1="CONTROL"/>
    </KeyBindings>
    
    

    If your keyboard shortcut requires more than two keys, set the mod2 and key2 attributes.

    In most situations, Shift should not be used without a second modifier because pressing it already causes most alphanumeric keys to type an uppercase letter or a symbol.

    Virtual-key codes let you access special keys that do not have a character associated with them, for example, function keys and the Backspace key. For more information, see Virtual-key codes.

    To make the command available in the Visual Studio editor, set the editor attribute to guidVSStd97.

    To make the command available only in a custom editor, set the editor attribute to the name of the custom editor that was generated by the Visual Studio Package Template when you created the VSPackage that includes the custom editor. To find the name value, look in the <Symbols> section for a <GuidSymbol> node whose name attribute ends in "editorfactory." This is the name of the custom editor.

Example 1

This example binds the keyboard shortcut Ctrl+Alt+C to a command named cmdidMyCommand in a package named MyPackage.

<CommandTable>
. . .
<Commands>
. . .
</Commands>
<KeyBindings>
  <KeyBinding guid="guidMyPackageCmdSet" id="cmdidMyCommand"
      key1="C" mod1="CONTROL" mod2="ALT" editor="guidVSStd97" />
</KeyBindings>
. . .
</CommandTable>

Example 2

This example binds the keyboard shortcut Ctrl+B to a command named cmdidBold in a project named TestEditor. The command is available only in the custom editor and not in other editors.

<KeyBinding guid="guidVSStd97" id="cmdidBold" editor="guidTestEditorEditorFactory" key1="B" mod1="Control" />

See also