您可以按以下方式删除特定的键绑定:
Visual Studio键绑定配置文件
不幸的是,我没有IntelliJ和ReSharper来测试它是否有效。如果有效,最好使用DTE执行此操作,但是这种解决方案超出了DTE的范围,并且可以使用System.IO.File轻松解决。
更新:
问题:如何使用DTE以编程方式重置VisualStudio键盘方案?实际上,我需要的是在选项|环境|键盘对话框中触发重置按钮的命令。
不幸的是,您无法(据我所知)这样做,因为重置键盘快捷键超出了DTE的范围。
如果您设置一个名为“ResetKeyBoard”的VS AddIn项目,并在
Exec
方法上放置断点,则会看到DTE在您处于Tools Options窗口内时没有捕获到任何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警告
您无法以编程方式更改默认键盘映射方案的设置。要更改设置,请将默认键盘映射方案另存为“选项”对话框中的“键盘”节点中。然后,您可以更改该映射方案中的设置。
我不推荐或鼓励此方法,这是糟糕的编程实践,可能会破坏某人的键盘快捷键!
b) 另一种方法是创建自己的VSK文件并将其设置为currentSettings.vssettings:
</ScopeDefinitions>
<ShortcutsScheme>Visual C# JT</ShortcutsScheme>
</KeyboardShortcuts>
![enter image description here](https://istack.dev59.com/8BPQt.webp)
在修改之前,请确保备份 currentSettings.vssettings 文件。
c) 这与 Chris Dunaway 的建议相一致,您可以创建一个纯包含键盘快捷方式的 vssettings 文件,并将其导入以重置键盘快捷方式。我知道默认快捷方式不会被保存,但以下是一些您可以使用 DTE 代码导出命令并插入到新的 vssettings 文件中以导入的代码:
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\"");
REFs:
Visual Studio 2005 IDE技巧和窍门
如何使用单个快捷方式将Visual Studio设置重置为已保存的设置?
如何舒适地设置Visual Studio 2010键盘快捷键,特别是在使用ReSharper时?
是否有一种快速删除Visual Studio 10中所有快捷方式的方法?
使用宏在Visual Studio中删除键盘快捷键绑定
http://vswindowmanager.codeplex.com/
获取DTE.ExecuteCommand可用命令的完整列表
HOWTO:从Visual Studio包中通过Guid和Id执行命令
HOWTO:从Visual Studio插件中通过Guid和Id执行命令
HOWTO:从Visual Studio插件中以编程方式向命令传递参数
最后这个是Jared Par写的:
https://github.com/jaredpar/VsVim/blob/master/Src/VsVimShared/Extensions.cs
public static void SafeSetBindings(this DteCommand command, IEnumerable<string> commandBindings)
{
try
{
var bindings = commandBindings.Cast<object>().ToArray();
command.Bindings = bindings;
if (bindings.Length == 0 && command.GetBindings().Count() != 0)
{
command.Bindings = bindings;
}
}
catch (Exception)
{
}