Gewusst wie: Beibehalten vorhandener Tastenzuordnungen für Befehle
Aktualisiert: November 2007
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 Gewusst wie: Anzeigen vorhandener Tastaturzuordnungen.
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 add new shortcut keys and preserve existing command key bindings
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.
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.
To create a copy of the default keyboard settings, navigate to C:\Program Files\Microsoft Visual Studio 8\Common7\IDE.
Right-click one of the vsk files and select Copy on the shortcut menu.
Paste the copy in the same folder.
The copy is named "Copy of <vsk-file name>".
Rename the copied file.
To verify that the new vsk-file appears in the keyboard bindings list, in Visual Studio click Options on the Tools menu.
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.
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.
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.
Call the function from the OnConnection method as described in Gewusst wie: Kompilieren und Ausführen der Codebeispiele für das Automatisierungsobjektmodell.
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 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.
Beispiel
The following Add-in example demonstrates how to bind two new key combinations to a command while preserving 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;
}
Siehe auch
Aufgaben
Gewusst wie: Zuordnen eines Befehls zu einer einzelnen Tastenkombination
Gewusst wie: Zuordnen eines Befehls zu mehreren Tastenkombinationen
Konzepte
Parameterformat der Bindings-Eigenschaft