更新:2007 年 11 月
通常,当您更改命令的键绑定时,现有的键绑定会丢失。但是,下面的示例演示如何将两个新组合键绑定到一个命令,同时仍然保留其现有的键。
如果要查看命令的当前列表,请按照 如何:查看现有键绑定 主题中的介绍运行 ListKeyBindings 示例。
![]() |
---|
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您的当前设置或版本。这些过程是使用现用的常规开发设置开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 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 文件名出现在“应用以下附加键盘映射方案”下拉菜单中。
运行外接程序示例之前,请确保键盘绑定设置为“(默认)”。可以通过单击“选项”对话框的“键盘”窗格中的“重置”来执行此操作。
用外接程序示例的 prop.Value = "< Filename.vsk>" 步骤的第 7 步中指定的新键盘方案名称替换 <Filename.vsk>。
按照 如何:编译和运行自动化对象模型代码示例 中的描述从 OnConnection 方法调用函数。
生成外接程序。
若要运行外接程序,请单击“工具”菜单上的“外接程序管理器”,选择您创建的外接程序,再单击“确定”。
File.NewFile 命令绑定到新的键绑定(Ctrl+Alt+Shift+Y 和 Ctrl+Alt+Shift+U)和原始键绑定。
示例
下面的外接程序示例演示如何将两个新组合键绑定到一个命令,同时仍然保留其现有的键。
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;
}