Search code examples
c#.netvb.netvisual-studiovisual-studio-sdk

VS 2013 SDK: How to keybind hotkey only for Code Window Editor?


In C# or else VB.Net, using a Visual Studio Package, I would like to assign a custom keyboard shortcut to a CommandBarButton, for example Ctrl + E + R, then, when pressed, the associated CommandBarButton should call its Execute method (I imagine that).

How I could do it?.


Update

( please avoid question above, that issue was already answered. )

Actually I'm using an vsct file, then my keybinding is this:

<KeyBindings>

  <KeyBinding guid="guidMainCmdSet" id="cmdidMyCommand" editor="guidVSStd97" 
              mod1="Control" key1="E" 
              mod2="Control" key2="R"/>

</KeyBindings>

MSDN explains that the guidVSStd97 is global, it seems to affect to all parts of the IDE:

https://msdn.microsoft.com/en-us/library/bb165973%28v=vs.90%29.aspx

To define key bindings at the global scope, use an Editor ID value of guidVSStd97.

My extension works with the selected text of the Code Window, so its weird that for example in the solution explorer, or with any project loaded I'm able to press the hotkey.

Then, I would like to be able to press that hotkey (Ctrl+ E + R) only in the Code Window Editor.

My question is:

Which is the editor id of the Code Window Editor?

Additional requisite:

I need an MSDN reference to see more related editor ids, I can't find anything about that.


Solution

  • In Visual Studio, keyboard shortcuts are associated to commands, not directly to CommandBarButtons. Technically they are called Keyboard Bindings and are declared in the .vsct file where you declare commands. See KeyBindings element

    Edited: You have to use:

      <KeyBinding guid="guidVSMyPackageCmdSet" id="cmdidMyCommand" editor="guidSourceCodeTextEditor" mod1="Control" key1="X" mod2="Control" key2="X"/>
    

      <GuidSymbol name ="guidVisualBasicEditor" value="{2c015c70-c72c-11d0-88c3-00a0c9110049}" />
    
      <GuidSymbol name ="guidSourceCodeTextWithEncodingEditor" value="{c7747503-0e24-4fbe-be4b-94180c3947d7}" />
    
      <GuidSymbol name ="guidSourceCodeTextEditor" value="{8b382828-6202-11d1-8870-0000f87579d2}" />
    

    ...

    where guidSourceCodeTextEditor can be any name that you define in the <Symbols> section whose value you must get from HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0Exp_Config\Editors. Once you run the package, if you go to Tools >Options window, Environment > Keyboard section, type the name of your command in Show Commands Containing, and you should see the shortcut in the list with the editor between parenthesis, as if you have selected it from the "Use new shortcut in" list. Which yields us to the question if the guids are the same for each Visual Studio version. AFAIK, this is not guaranteed (nothing prevents Microsoft changing guids in a new version) but likely they are the same. I cannot verify right now because the computer that I am using only has VS 2013.