更新:2007 年 11 月
可以将多个快捷键绑定到命令,而不是将单个快捷组合键绑定到命令。例如,如果使用同一个项目的两个用户倾向于用不同的快捷键发送相同的命令,这将非常有用。这通过将快捷键作为类型为 Object 的数组中的字符串元素传递来实现。
![]() |
---|
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是使用现用的常规开发设置开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置。 |
将命令绑定到多键快捷键
使用“Visual Studio 外接程序向导”创建一个新的外接程序。为项目命名,然后单击“确定”启动向导。
有关使用“Visual Studio 外接程序向导”的更多信息,请参见 如何:创建外接程序。
在“选择编程语言”页上选择“使用 Visual C# 创建外接程序”,以运行下面的 Visual C# 示例,或选择“使用 Visual Basic 创建外接程序”运行 Visual Basic 示例。
将下面的示例函数粘贴到“Visual Studio 外接程序向导”生成的代码的 Connect 类中。
若要创建默认键盘设置的一个副本,请转到 C:\Program Files\Microsoft Visual Studio 8\Common7\IDE。
右击一个 vsk 文件并在快捷菜单上选择“复制”。
将副本粘贴在同一个文件夹中。
该副本名为“<vsk-file name> 的副本”。
重命名复制的文件。
若要验证新的 vsk 文件是否出现在 Visual Studio 的键盘绑定列表中,请在“工具”菜单上单击“选项”。
在“选项”对话框的左窗格中,展开“环境”文件夹,然后选择“键盘”。
确保在步骤 7 中重命名的 vsk-file 的名称出现在“应用以下其他键盘映射方案”下拉菜单中。
运行外接程序示例之前,请确保键盘绑定设置为“(默认)”。可以通过单击“选项”对话框的“键盘”窗格中的“重置”来执行此操作。
在外接程序示例的 prop.Value = "< Filename.vsk>" 步骤中,用在步骤 7 中指定的新键盘方案名称替换 <Filename.vsk>。
按照 如何:编译和运行自动化对象模型代码示例 中的描述从 OnConnection 方法调用函数。
生成外接程序。
若要运行外接程序,请单击“工具”菜单上的“外接程序管理器”,选择您创建的外接程序,再单击“确定”。
该命令绑定到两个不同的快捷键。按 Ctrl+Shift+Alt+Y 或 Ctrl+Shift+Alt+X 以显示“新建文件”对话框。
示例
下面的示例用两个新的键绑定替换现有的键绑定。
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;
}