简体   繁体   English

如何在VSIX包安装上注册密钥绑定代码

[英]How to register key binding code on VSIX package installation

I'd like to add keyboard shortcut to Visual Studio package only once, during its installation. 我想在安装过程中只为Visual Studio包添加一次键盘快捷键。 Basically I know how to do it from the package code, for example: 基本上我知道如何从包代码中做到这一点,例如:

var dte = GetGlobalService(typeof(DTE)) as DTE2;
if (dte != null)
{                
    dte.Commands.Item("Tools.Something").Bindings = "Global::Ctrl+T, Ctrl+S";
}

The problem is, I'd like to invoke this code only once (I don't want to execute this code in package class constructor, because it will execute each time the package will be used first time after VS restart). 问题是,我只想调用一次这个代码(我不想在包类构造函数中执行这个代码,因为每次在VS重启后第一次使用包时它都会执行)。

How to do it ? 怎么做 ?

There is another way I wasn't aware last time. 还有另一种我上次不知道的方式。 All what need to be done is adding key binding in the *.vsct file. 所有需要做的是在* .vsct文件中添加键绑定。 This will register your key shortcut and bind it to the selected command. 这将注册您的密钥快捷方式并将其绑定到所选命令。

<KeyBindings>
    <KeyBinding guid="guidSomehingCmdSet" id="cmdidSomehing" editor="guidVSStd97" mod1="Control" mod2="Control" key1="T" key2="S" />    
</KeyBindings>

First of all, if you plan to publicly distribute the extension then you should probably remove the binding due to an extremely high likelihood it will interfere with existing bindings of some of your users. 首先,如果您计划公开分发扩展,那么您应该删除绑定,因为它极有可能会干扰您的某些用户的现有绑定。

Second, provide the command and binding as part of a Visual Studio Command Table , not via the automation interfaces. 其次,将命令和绑定作为Visual Studio命令表的一部分提供,而不是通过自动化接口。 The commands are registered using the [ProvideMenuResourceAttribute] attribute which does not require any code to execute when the package is installed. 使用[ProvideMenuResourceAttribute]属性注册命令,该属性在安装软件包时不需要执行任何代码。

  • Check the a registry key every time in package init() function 每次在init()函数包中检查一个注册表项
  • if key not present create registry key and execute your code 如果key不存在则创建注册表项并执行您的代码
  • If key found, don't execute your code 如果找到密钥,请不要执行您的代码

For example: 1. Check "Test" key in your desired location 2. If "Test" key not present create that key in registry and execute your code 3. If "Test" key found, don't execute your code 例如:1。在所需位置选中“Test”键2.如果“Test”键不存在,请在注册表中创建该键并执行代码3.如果找到“Test”键,请不要执行代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM