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.
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 ExportSettings on the Tools menu. For more information, see Visual Studio Settings.
Procedure
To bind a command to a shortcut key
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.
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.
Paste the example function below in the Connect class of the code generated by the Visual Studio Add-In Wizard.
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.
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.
Call the function from the OnConnection method as described in How to: Compile and Run the Automation Object Model Code Examples.
Build the Add-in.
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.
Example
The following Add-in example demonstrates how to bind the File.NewFile command to a single shortcut key (F2).
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
BindingsExample(_applicationObject)
End Sub
Sub BindingsExample(ByVal dte As DTE2)
Dim cmds As Commands
Dim cmd As Command
Try
' Set references to the Commands collection and the
' File.NewFile command.
cmds = DTE.Commands
cmd = cmds.Item("File.NewFile")
' Assigns the command (File.NewFile) globally to the F2 key.
cmd.Bindings = "Global::F2"
MsgBox("key remapped")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
BindingsExample(_applicationObject);
}
public void BindingsExample(DTE2 dte)
{
Commands cmds;
Command cmd;
try
{
// 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)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
See Also
Tasks
How to: Bind a Command To Multiple Combinations of Shortcut Keys
How to: Preserve Existing Command Key Bindings
Concepts
Bindings Property Parameter Format