如何:查看现有键绑定
Bindings 属性可以使您查看或更改与指定的命令相关联的键绑定。 读取该属性时将以对象数组的形式检索命令的当前绑定。 每个对象包含一个描述绑定的字符串。
将值设置为 Bindings 属性,把一个或多个新的键绑定分配给指定的命令。 有关更多信息,请参见 如何:将命令绑定到单个快捷键 和 如何:将一个命令绑定到多个键盘快捷键。
提示
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。 这些过程是在“常规开发设置”处于活动状态时开发的。 若要更改设置,请在“工具”菜单上选择“导入和导出设置”。 有关更多信息,请参见 使用设置。
查看现有键绑定
使用**“Visual Studio 外接程序向导”创建新的外接程序。 为项目命名,并单击“确定”**以启动该向导。
有关使用**“Visual Studio 外接程序向导”**的更多信息,请参见 如何:创建外接程序。
在**“选择编程语言”页上选择“使用 Visual C# 创建外接程序”,以运行下面的 Visual C# 示例,或选择“使用 Visual Basic 创建外接程序”**运行 Visual Basic 示例。
将下面的示例函数粘贴到**“Visual Studio 外接程序向导”**生成的代码的 Connect 类中。
按照 如何:编译和运行自动化对象模型代码示例 中的描述从 OnConnection 方法调用函数。
若要运行外接程序,请单击**“工具”菜单上的“外接程序管理器”,选择您创建的外接程序,再单击“确定”**。
显示绑定到 File.NewFile 命令的所有快捷键列表。
示例
下面的示例通过显示绑定到 File.NewFile 命令的所有快捷键来演示 Bindings 的用法。
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)
' Pass the applicationObject member variable to the code example.
ListKeyBindings(_applicationObject)
End Sub
Sub ListKeyBindings(ByVal dte As DTE2)
' Bindings() is an array of key binding string names.
Dim bindings() As Object
Dim binding As Object
Dim msg As String = Nothing
' Populate the collection with all of the bindings
' for the command File.NewFile.
bindings = dte.Commands.Item("File.NewFile").Bindings
For Each binding In bindings
msg += CStr(binding) & vbCr
Next
MsgBox(msg)
End Sub
// Add-in code.
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.
ListKeyBindings((DTE2)_applicationObject);
}
public void ListKeyBindings(DTE2 dte)
{
object[] bindings;
string msg = string.Empty;
// Populate the collection with all of the bindings associated
// with the command File.NewFile.
// Bindings() is an array of key binding string names.
bindings = (object[])dte.Commands.Item("File.NewFile", 0).Bindings;
foreach (object b in bindings)
{
msg += ((string)b) + "\n";
}
System.Windows.Forms.MessageBox.Show(msg);
}