Export (0) Print
Expand All

How to: Bind Keyboard Shortcuts to Menu Items

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

To assign keyboard shortcuts to existing Visual Studio menu items, see How to: Work with Keyboard Shortcuts.

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. You should find out whether a given shortcut is already in use before you try to map it to another command.

To find out whether a shortcut is already in use

  1. On the Tools menu, click Options.

  2. In the left pane, expand the Environment node and select Keyboard.

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

  4. 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 Shorcut currently used by box will show the command that the shortcut currently calls.

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

    NoteNote

    Keyboard shortcuts that use ALT may open a menu and not directly execute a command. Therefore, the Shorcut 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.

To assign a keyboard shortcut to a command

  1. Open the .vsct file for your package.

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

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

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

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

    NoteNote

    In most situations, Shift should not be used without a second modifier, because pressing it has so many other uses.

  6. Set the key1 attribute to a letter, number, symbol, or virtual-key code.

    NoteNote

    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.

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

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

  9. 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.

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

<KeyBindings>
  <KeyBinding guid="guidMyPackageCmdSet" id="cmdidMyCommand" 
      key1="C" mod1="Control" mod2="Alt" editor="guidVSStd97" />
</KeyBindings>

This example binds the keyboard shortcut CTL+B to a command named cmdidBold in a project named TestEditor. The command is only available in the custom editor and not in the general Visual Studio integrated development environment (IDE).


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


Community Additions

ADD
Show:
© 2015 Microsoft