Freigeben über


Gewusst wie: Zuordnen eines Befehls zu mehreren Tastenkombinationen

Aktualisiert: November 2007

Rather than binding a single shortcut key combination to a command, you can alternatively bind multiple shortcut keys to it. This can be useful, for example, if two users working on a project prefer different shortcut keys to issue the same command. This is accomplished by passing the shortcut keys as string elements in an array of type Object.

Hinweis:

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

To bind a command to a multiple-key shortcut key

  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 Gewusst wie: Erstellen von Add-Ins.

  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, go 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. 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.

  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. In the prop.Value = "< Filename.vsk>" step of the Add-in example, replace <Filename.vsk> with the new keyboard scheme name you specified in step 7.

  12. Call the function from the OnConnection method as described in Gewusst wie: Kompilieren und Ausführen der Codebeispiele für das Automatisierungsobjektmodell.

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

Beispiel

The following example replaces the existing key binding with 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; 
} 

Siehe auch

Aufgaben

Gewusst wie: Zuordnen eines Befehls zu einer einzelnen Tastenkombination

Gewusst wie: Beibehalten vorhandener Tastenzuordnungen für Befehle

Konzepte

Parameterformat der Bindings-Eigenschaft

Weitere Ressourcen

Zuordnen von Add-In-Befehlen zu Tasten