如何:保留现有键盘快捷键

通常,当您更改某个命令的键盘快捷键时,现有的键盘快捷键会丢失。 下面的示例演示如何将两个新的键盘快捷键绑定到一个命令,同时仍然保留其现有的键盘快捷键。

如果要查看命令及其当前键盘快捷键的列表,请按照如何:查看现有键绑定 中的介绍运行 ListKeyBindings 示例。

提示

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。 这些过程是在“常规开发设置”处于活动状态时开发的。 若要更改设置,请在“工具”菜单上单击“导入和导出设置”。 有关更多信息,请参见 使用设置

添加新的键盘快捷键并保留现有的键盘快捷键

  1. 使用**“Visual Studio 外接程序向导”创建一个外接程序。 为项目命名,并单击“确定”**以启动该向导。

    有关如何使用**“Visual Studio 外接程序向导”**的更多信息,请参见如何:创建外接程序

  2. 在**“选择编程语言”页上选择“使用 Visual C# 创建外接程序”,以运行下面的 Visual C# 示例,或选择“使用 Visual Basic 创建外接程序”**运行 Visual Basic 示例。

  3. 将示例函数(在本主题后面显示)粘贴到**“Visual Studio 外接程序向导”**生成的代码的 Connect 类中。

  4. 若要创建默认键盘设置的副本,请定位到 C:\Program Files\Microsoft Visual Studio 10\Common7\IDE\。

  5. 右击其中的一个 .vsk 文件,再单击**“复制”**。

  6. 将该副本粘贴到同一个文件夹中,并对其进行重命名。

  7. 若要验证新的 .vsk 文件是否出现在 Visual Studio 的键盘快捷键列表中,请在**“工具”菜单上单击“选项”**。

  8. 在**“选项”对话框的左窗格中,展开“环境”文件夹,然后选择“键盘”**。

    确保先前指定的 .vsk 文件的名称出现在**“应用以下其他键盘映射方案”**列表中。

  9. 运行外接程序示例之前,请确保键盘快捷键设置为**“(默认)”。 可以通过单击“选项”对话框的“键盘”窗格中的“重置”**来执行此操作。

  10. 在外接程序示例的 prop.Value = "<Filename.vsk>" 步骤中,用先前指定的 .vsk 文件的名称替换 <Filename.vsk>。

  11. 按照如何:编译和运行自动化对象模型代码示例中的描述,从 OnConnection 方法调用该函数。

  12. 生成外接程序。

  13. 若要运行外接程序,请单击**“工具”菜单上的“外接程序管理器”,选择您创建的外接程序,再单击“确定”**。

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

请参见

任务

如何:将命令绑定到单个快捷键

如何:将一个命令绑定到多个键盘快捷键

概念

Bindings 属性的参数格式

其他资源

将外接程序命令绑定到键