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;
}