How to register key binding code on VSIX package installation

耗尽温柔 提交于 2020-01-02 07:46:10

问题


I'd like to add keyboard shortcut to Visual Studio package only once, during its installation. 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).

How to do it ?


回答1:


There is another way I wasn't aware last time. All what need to be done is adding key binding in the *.vsct file. 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>



回答2:


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. The commands are registered using the [ProvideMenuResourceAttribute] attribute which does not require any code to execute when the package is installed.




回答3:


  • Check the a registry key every time in package init() function
  • if key not present create registry key and execute your code
  • 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



来源:https://stackoverflow.com/questions/15071825/how-to-register-key-binding-code-on-vsix-package-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!