How to: Bind Keyboard Shortcuts to Menu Items
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 How to: Work with Shortcut Key Combinations.
Choosing a Key Combination
Many keyboard shortcuts are already used in Visual Studio. Although you can assign the same shortcut to more than one command, we recommend that you do not do it 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
On the Tools menu, click Options.
In the left pane, expand the Environment node and select Keyboard.
Make sure that Use new shortcut in is set to Global.
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.
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 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
Open the .vsct file for your package.
Create an empty <KeyBindings> section if it is not already present.
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.
Note
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.
Set the key1 attribute to a letter, number, symbol, or virtual-key code.
Note
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.
If your keyboard shortcut requires more than two keys, set the mod2 and key2 attributes.
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 Integration Package Wizard 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
This example binds the keyboard shortcut ALT+C to a command named cmdidMyCommand in a package named MyPackage.
<KeyBindings>
<KeyBinding guid="guidMyPackageCmdSet" id="cmdidMyCommand"
key1="C" mod1="Alt" editor="guidVSStd97" />
</KeyBindings>
This example binds the keyboard shortcut CONTROL+ALT+8 to a command named cmdidColorFont 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).