Condividi tramite


How to: Preserve Existing 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.

Typically, when you change the keyboard shortcut for a command, the existing shortcut is lost. The following example demonstrates how to bind two new shortcuts to a command and still preserve its existing ones.

If you want to see a list of commands together with their current shortcuts, run the ListKeyBindings example as presented in How to: View Existing 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, click Import and ExportSettings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

To add new keyboard shortcuts and preserve existing ones

  1. Use the Visual Studio Add-In Wizard to create an 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 below, or Create an Add-in Using Visual Basic to run the Visual Basic example.

  3. Paste the example function (shown later in this topic) 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, locate C:\Program Files\Microsoft Visual Studio 10\Common7\IDE\.

  5. Right-click one of the .vsk files and then click Copy.

  6. Paste the copy in the same folder, and then rename it.

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

  8. 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 specified earlier appears in the Apply the following additional keyboard mapping scheme list.

  9. Before you run the add-in example, make sure that the keyboard shortcuts are set to (Default). You can do this by clicking Reset in the Keyboard pane of the Options dialog box.

  10. In the prop.Value = "<Filename.vsk>" step of the add-in example, replace <Filename.vsk> by using the name of the .vsk file you specified earlier.

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

  12. Build the add-in.

  13. To run the add-in, click Add-in Manager on the Tools menu, select the add-in you created, and click OK.

    The File.NewFile command is bound to the new keyboard shortcuts, CTRL+ALT+SHIFT+Y and CTRL+ALT+SHIFT+U, and also to the original shortcuts.

Example

The following add-in example demonstrates how to bind two new keyboard shortcuts to a command, and also preserve its existing ones.

Sub PreserveBindings()
    ' Adds two new key bindings while preserving the existing ones.
    Dim cmds As Commands
    Dim cmd As Command
    Dim props As EnvDTE.Properties = DTE.Properties("Environment", _
    "Keyboard")
    Dim prop As EnvDTE.Property
    Dim bindings() As Object
    Dim bindingNumber As Integer

    ' Set references to the Commands collection and the File.NewFile
    ' command.
    cmds = DTE.Commands
    cmd = cmds.Item("File.NewFile")
    ' Make a writeable copy of the default keymapping scheme.
    prop = props.Item("SchemeName")
    prop.Value = "<FileName.vsk>"
    ' Retrieve the current bindings for the command.
    bindings = cmd.Bindings
    ' Get the number of bindings for the command.
    bindingNumber = bindings.Length
    ' Add two more elements to the array to accomodate two
    ' new commands.
    ReDim Preserve bindings(bindingNumber + 1)
    ' Add the new bindings to the existing ones in the array.
    bindings(bindingNumber) = "Global::CTRL+ALT+SHIFT+Y"
    bindings(bindingNumber + 1) = "Global::CTRL+ALT+SHIFT+U"
    ' Assign the contents of the bindings array to the Bindings 
    ' property.
    cmd.Bindings = bindings
End Sub
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst, ref
 System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    PreserveBindings((_applicationObject); 
}

// Add-in example for TextSelection.FindPattern.
// Also shows usage of these methods and properties:
//    TextSelection.SelectLine
public void PreserveBindings( DTE dte ) 
{ 
    // Adds two new key bindings while preserving the existing ones.
    Commands cmds = null; 
    Command cmd = null; 
    EnvDTE.Properties props = dte.get_Properties( "Environment",
 "Keyboard"); 
    EnvDTE.Property prop = null; 
    Object[] bindings = null; 
    int bindingNumber = 0; 

    //  Set references to the Commands collection and the File.NewFile
    //  command.
    cmds = dte.Commands; 
    cmd = cmds.Item( "File.NewFile", -1 ); 
    // Make a writeable copy of the default keymapping scheme.
    prop = props.Item( "SchemeName" ); 
    prop.Value = "<FileName.vsk>"; 
    // Retrieve the current bindings for the command.
    bindings = ( ( System.Object[] )( cmd.Bindings ) ); 
    // Get the number of bindings for the command.
    bindingNumber = bindings.Length; 
    // Add two more elements to the array to accomodate two
    // new commands.
    // Create temp variable for copying values. 
    // Arrays are zero-based in C#.
    object[] temp = new object[ bindingNumber + 2 ]; 
    System.Array.Copy( bindings, temp, Math.Min( bindings.Length,
 temp.Length ) ); 
    bindings = temp; 
    // Add the new bindings to the existing ones in the array.
    bindings[ bindingNumber ] = "Global::CTRL+ALT+SHIFT+Y"; 
    bindings[ bindingNumber+1 ] = "Global::CTRL+ALT+SHIFT+U"; 
    // 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: Bind a Command To Multiple Keyboard Shortcuts

Concepts

Bindings Property Parameter Format

Other Resources

Binding Add-In Commands to Keys