Udostępnij za pośrednictwem


How to: Bind a Command To Multiple Keyboard Shortcuts

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

You can bind more than one keyboard shortcut to a command. This can be useful when, for example, two users who are working on a project each prefer a different shortcut for the same command. This binding is accomplished by passing the shortcuts as string elements in an array of type Object.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To bind a command to multiple keyboard shortcuts

  1. Use the Visual Studio Add-In Wizard to create a add-in. Name the project and click OK to start the wizard.

    For more information about how to use 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 in this topic, or Create an Add-in Using Visual Basic to run the Visual Basic example.

  3. Paste the example function in the Connect class of the code that is generated by the Visual Studio Add-In Wizard.

  4. To create a copy of the default keyboard settings, go to ..\Program Files\Microsoft Visual Studio 10\Common7\IDE\. Right-click one of the .vsk files and then click Copy. Paste the copy in the same folder. The copy is named "Copy of .vsk file name".

  5. Rename the copy of the file.

  6. To verify that the new .vsk file appears in the keyboard bindings list, in Visual Studio click Options on the Tools menu.

  7. In the left pane of the Options dialog box, expand the Environment folder and select Keyboard.

    Ensure that the name of the .vsk file that you renamed earlier appears in the Apply the following additional keyboard mapping scheme list.

  8. Before you run 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.

  9. In the prop.Value = "< Filename.vsk>" step of the Add-in example, replace <Filename.vsk> by using the new keyboard scheme name that you specified earlier.

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

  11. Build the add-in, and then run it by clicking Add-in Manager on the Tools menu, selecting the add-in you created, and then clicking OK.

    The command is bound to two different shortcut keys. Press either CTRL+SHIFT+ALT+Y or CTRL+SHIFT+ALT+X to display the New File dialog box.

Example

The following example replaces the existing keyboard shortcut by using two new ones.

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)
    BindSingle(_applicationObject)
End Sub
Sub BindSingle(ByVal dte As DTE2)
    ' Adds two new keybindings to a command.
    Dim cmds As Commands
    Dim cmd As Command
    Dim props As EnvDTE.Properties = DTE.Properties("Environment", _"Keyboard")
    Dim prop As EnvDTE.Property
    Dim bindings(1) As Object

    ' Make a writeable copy of the default keymapping scheme.
    prop = props.Item("SchemeName")
    prop.Value = "<FileName.vsk>"
    ' Assign the two shortcut key combinations, CTRL+SHIFT+ALT+Y and 
    ' CTRL+SHIFT+ALT+X, to the two bindings array elements. 
    bindings(0) = "Global:: CTRL+SHIFT+ALT+Y"
    bindings(1) = "Global:: CTRL+SHIFT+ALT+X"
    ' Set references to the Commands collection and the File.NewFile
    ' command.
    cmds = DTE.Commands
    cmd = cmds.Item("File.NewFile")
    ' Assign the contents of the bindings array to the Bindings 
    ' property.
    cmd.Bindings = bindings
End Sub
public void OnConnection(object application,
 ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    BindMultiple(_applicationObject ); 
}

public void BindMultiple( DTE2 dte ) 
{ 
    // Adds two new keybindings to a command.
    Commands cmds = null; 
    Command cmd = null; 
    EnvDTE.Properties props = dte.get_Properties( "Environment",
 "Keyboard"); 
    EnvDTE.Property prop = null; 
    Object[] bindings = new Object[ 2 ]; 

    // Make a writeable copy of the default keymapping scheme.
    prop = props.Item( "SchemeName" ); 
    prop.Value = "<FileName.vsk>"; 
    // Assign the two shortcut key combinations, CTRL+SHIFT+ALT+Y and 
    // CTRL+SHIFT+ALT+X, to the two bindings array elements. 
    bindings[ 0 ] = "Global:: CTRL+SHIFT+ALT+Y"; 
    bindings[ 1 ] = "Global:: CTRL+SHIFT+ALT+X"; 
    // Set references to the Commands collection and the File.NewFile
    // command.
    cmds = dte.Commands; 
    cmd = cmds.Item( "File.NewFile", -1 ); 
    // Assign the contents of the bindings array to the Bindings 
    // property.
    cmd.Bindings = bindings; 
} 

See Also

Tasks

How to: Bind a Command to a Single Shortcut Key

How to: Preserve Existing Keyboard Shortcuts

Concepts

Bindings Property Parameter Format

Other Resources

Binding Add-In Commands to Keys