This documentation is archived and is not being maintained.

How to: Preserve Existing Command Key Bindings 

Normally, when you change the key binding for a command, existing key bindings are lost. The following example, however, demonstrates how to bind two new key combinations to a command and still preserve its existing ones.

If you would like to see a current list of the commands, run the ListKeyBindings example as presented in the topic How to: View Existing Key Bindings.

NoteNote

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 Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To add new shortcut keys and preserve existing command key bindings

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

  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 below in the Connect class of the code generated by the Visual Studio Add-In Wizard.

  4. To create a copy of the default keyboard settings, navigate to C:\Program Files\Microsoft Visual Studio 8\Common7\IDE.

  5. Right-click one of the vsk files and select Copy on the shortcut menu.

  6. Paste the copy in the same folder.

    The copy is named "Copy of <vsk-file name>".

  7. Rename the copied file.

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

  9. On 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 in step 7 appears in the Apply the following additional keyboard mapping scheme drop-down menu.

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

  11. Replace the <Filename.vsk> with the new keyboard scheme name you specified in step 7 in the prop.Value = "< Filename.vsk>" step of the Add-in example.

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

  13. Build the Add-in.

  14. 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 new key bindings (CTRL+ALT+SHIFT+Y and CTRL+ALT+SHIFT+U) as well as the original key bindings.

Example

The following Add-in example demonstrates how to bind two new key combinations to a command while preserving its existing ones.

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

Show: