Domanda

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 ?

È stato utile?

Soluzione

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>

Altri suggerimenti

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.

  • 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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top