以编程方式重置VisualStudio快捷方式

时间:2016-08-19 16:17:16

标签: c# visual-studio envdte

有关resetting VisualStudio keyboard schemeimporting VisualStudio settings的两个相关问题。但是,这似乎并不是很好。

我有两个包含快捷方式的设置文件:

<!-- IntelliJ.vssettings -->   
<ShortcutsScheme>Visual C# 2005</ShortcutsScheme>
<UserShortcuts>
  <Shortcut Command="ReSharper.ReSharper_GotoNextHighlight" Scope="Global">F12</Shortcut>
</UserShortcuts>

<!-- ReSharper.vssettings -->   
<ShortcutsScheme>Visual C# 2005</ShortcutsScheme>
<UserShortcuts>
  <!-- Implicitly has F12 assigned to Edit.GoToDefinition -->
</UserShortcuts>

如您所见,ReSharper.vssettings并未真正分配F12个快捷方式,因为它是VisualStudio的默认设置。导入该文件时,不会重新应用ShortcutsScheme,在这两种情况下都是Visual Studio C# 2005。这反过来导致F12继续执行GotoNextHighlight命令。仅使用导入对话框时会出现同样的问题。

使用DTE如下重置键盘方案也不起作用:

var property = dte.Properties["Environment", "Keyboard"];
property.Item("SchemeName").Value = "(Default)";

导出默认设置不会出于同样的原因。由于shown here没有导出快捷方式。

问题:如何使用DTE以编程方式重置VisualStudio键盘方案?

我真正需要的是在Options |中触发Reset按钮的命令环境|键盘对话框。

2 个答案:

答案 0 :(得分:3)

您可以按照以下方式删除特定的键绑定: Visual Studio key bindings configuration file

不幸的是,我没有IntelliJ和ReSharper来测试它是否有效。如果确实如此,那么使用DTE这样做很好,但是这个解决方案超出了DTE的范围,使用System.IO.File会很简单。

<强>更新

  

问题:如何使用DTE以编程方式重置VisualStudio键盘方案?我真正需要的是在Options |中触发Reset按钮的命令环境|键盘对话框。

不幸的是你不能这样做(AFAIK),因为重置键盘快捷键超出了DTE的范围。

如果您设置了一个名为&#34; ResetKeyBoard&#34;的VS AddIn项目。并在Exec方法上设置一个断点,您将看到当您在“工具选项”窗口中时,DTE没有捕获任何Visual Studio事件,它们不会通过DTE对象模型公开:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

这也可以通过录制宏来演示,录制的命令只会打开选项对话框(无论你在里面改变什么设置)都是如此:

Public Module RecordingModule
    Sub TemporaryMacro()
        DTE.ExecuteCommand("Tools.Options")
    End Sub
End Module

我确实学会了如何直接打开“选项”窗口的“键盘”选项卡,但由于它是一个模态对话框,你甚至无法使用SendKeys按下重置按钮:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if(commandName == "ResetKeyBoard.Connect.ResetKeyBoard")
                {
                    _applicationObject.ExecuteCommand("Tools.Options", "BAFF6A1A-0CF2-11D1-8C8D-0000F87570EE");
                    System.Windows.Forms.SendKeys.Send("%e");
                    System.Windows.Forms.SendKeys.Send("{ENTER}");

我尝试过的最后一个DTE选项(没有运气)正在使用Commands.Raise,你再也无法打开工具选项,或者至少可以无证件

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
    handled = false;
    if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if(commandName == "ResetKeyBoard.Connect.ResetKeyBoard")
        {
            Commands cmds = _applicationObject.Commands;
            Command cmdobj = cmds.Item("Tools.Options");
            object customIn = null;
            object customOut = null;
            _applicationObject.Commands.Raise(cmdobj.Guid, cmdobj.ID, ref customIn, ref customOut);

<强>解决方法:

a)我不鼓励您替换Visual C#2005.vsk文件,但是如果您想调查它的这个文件:

C:\Program Files (x86)\Microsoft Visual Studio 1X.0\Common7\IDE\Visual C# 2005.vsk

MSDN Warning

  

您无法以编程方式更改默认键盘映射方案的设置。要更改设置,请在“选项”对话框的“键盘”节点中保存默认键盘映射方案的副本。然后,您可以更改该映射方案中的设置。

我不推荐或鼓励这种方法,它编程错误,你可能会破坏别人的键盘快捷键!

b)另一种方法可以是创建自己的VSK文件并在currentSettings.vssettings中设置它:

    </ScopeDefinitions>
     <ShortcutsScheme>Visual C# JT</ShortcutsScheme>
    </KeyboardShortcuts>

enter image description here

确保在更改之前备份currentSettings.vssettings文件。

c)这可以回到Chris Dunaway的建议,你可以在其中创建一个vssettings文件(纯粹包含键盘快捷键)并将其导入以重置键盘快捷键。我意识到默认快捷方式没有保存,但是这里有一些代码可以用DTE导出命令,插入新的vssettings文件然后导入:

//note, this is untested code!
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
    handled = false;
    if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if(commandName == "ResetKeyBoard.Connect.ResetKeyBoard")
        {
            System.Diagnostics.Debug.WriteLine("<UserShortcuts>");
            foreach (Command c in _applicationObject.Commands)
            {
                if (!string.IsNullOrEmpty(c.Name))
                {
                    System.Array bindings = default(System.Array);
                    bindings = (System.Array)c.Bindings;
                    for (int i = 0; i <= bindings.Length - 1; i++)
                    {
                        string scope = string.Empty;
                        string keyShortCut = string.Empty;
                        string[] binding = bindings.GetValue(i).ToString().Split(new string[] {  "::" },StringSplitOptions.RemoveEmptyEntries );
                        scope = binding[0];
                        keyShortCut = binding[1];
                        System.Diagnostics.Debug.WriteLine("<RemoveShortcut Command=\"...\" Scope=\"" + scope + "\">" + keyShortCut + "</RemoveShortcut>");
                        System.Diagnostics.Debug.WriteLine("<Shortcut Command=\"" + c.Name + "\" Scope=\"" + scope + "\">" + keyShortCut + "</Shortcut>");
                    }
                }
            }
            System.Diagnostics.Debug.WriteLine("</UserShortcuts>");

一旦你把它们拿出来就很容易将它们导入:

_applicationObject.ExecuteCommand("Tools.ImportandExportSettings", "/import:\"KeyboardOnly-Exported-2016-08-29.vssettings\"");

个REF:

Visual Studio 2005 IDE Tips and Tricks

How to reset visual studio settings to my saved settings with just a single shortcut?

How does one set Visual Studio 2010 keyboard shortcuts comfortably, especially when using ReSharper?

https://superuser.com/questions/914244/is-there-a-quick-way-to-delete-all-shortcuts-in-visual-studio-10

Remove a keyboard shortcut binding in Visual Studio using Macros

http://vswindowmanager.codeplex.com/

Get full list of available commands for DTE.ExecuteCommand

HOWTO: Execute a command by Guid and Id from a Visual Studio package

HOWTO: Execute a command by Guid and Id from a Visual Studio add-in

HOWTO: Pass parameters programmatically to a command from a Visual Studio add-in

最后这个是Jared Par:

https://github.com/jaredpar/VsVim/blob/master/Src/VsVimShared/Extensions.cs

/// <summary>
/// Safely reset the keyboard bindings on this Command to the provided values
/// </summary>
public static void SafeSetBindings(this DteCommand command, IEnumerable<string> commandBindings)
{
    try
    {
        var bindings = commandBindings.Cast<object>().ToArray();
        command.Bindings = bindings;

        // There are certain commands in Visual Studio which simply don't want to have their
        // keyboard bindings removed.  The only way to get them to relinquish control is to
        // ask them to remove the bindings twice.  
        //
        // One example of this is SolutionExplorer.OpenFilesFilter.  It has bindings for both
        // "Ctrl-[, O" and "Ctrl-[, Ctrl-O".  Asking it to remove all bindings will remove one
        // but not both (at least until you restart Visual Studio, then both will be gone).  If
        // we ask it to remove bindings twice though then it will behave as expected.  
        if (bindings.Length == 0 && command.GetBindings().Count() != 0)
        {
            command.Bindings = bindings;
        }
    }
    catch (Exception)
    {
        // Several implementations, Transact SQL in particular, return E_FAIL for this
        // operation.  Simply ignore the failure and continue
    }

答案 1 :(得分:-1)

您可以直接调用devenv.exe并传递/ ResetSettings开关。以下是Visual Studio命令行选项的链接:https://msdn.microsoft.com/en-us/library/xee0c8y7.aspx

您可以使用Systems.Diagnostics.Process类执行devenv.exe来重置设置:

  

Devenv.exe / ResetSettings

您可以选择传递包含要恢复的设置的设置文件:

  

Devenv.exe / ResetSettings&#34; C:\ My Files \ MySettings.vssettings&#34;

在Visual Studio中,您可以转到工具&gt;导出所选设置。导入和导出设置...并选择&#34;导出所选环境设置&#34;。然后,只选择所有设置&gt;下的键盘复选框。选项&gt;环境子树。