3

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.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

2 Answers2

6

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.

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18
  • Thanks again, but again I have the same problem with this, the lack of an .vsct file because I'm using an empty vspackage where I add buttons to the code editor contextmenu and nothing more (I don't want to add any command under the "Tools" menu). really I should start my project from zero using a command menu template?. – ElektroStudios Jul 10 '15 at 12:48
  • We will continue the discussion in your "VS 2013 SDK: How to add a line separator in a CommandBarPopup menu?" thread – Carlos Quintero Jul 13 '15 at 10:55
  • thanks for your experience, I've updated this question, pelase if yu could help with this. – ElektroStudios Jul 13 '15 at 17:48
  • 1
    The Editor ID, despite its name, is a Guid. The guidVSStd97 value is defined in vsshlids.h as #define guidVSStd97 CMDSETID_StandardCommandSet97 and in turn #define CMDSETID_StandardCommandSet97 { 0x5efc7975, 0x14bc, 0x11cf, { 0x9b, 0x2b, 0x00, 0xaa, 0x00, 0x57, 0x38, 0x19 } }. So, if you want a specific editor, you must know its guid and MSDN says "The value of Editor ID is a GUID that corresponds to a CmdUIGUID that was assigned by the developer when the editor is created. This Editor ID GUID value may or may not be the same as the GUID for the EditorFactory for the editor." – Carlos Quintero Jul 14 '15 at 15:37
  • 1
    I don't think editor guids are documented in MSDN but they are registered in HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0Exp_Config\Editors – Carlos Quintero Jul 14 '15 at 15:38
  • I have not tested but try {8B382828-6202-11d1-8870-0000F87579D2} (Source Code (Text) Editor) or {C7747503-0E24-4FBE-BE4B-94180C3947D7} (Source Code (Text) Editor with Encoding) – Carlos Quintero Jul 14 '15 at 15:43
  • 1
    Although https://msdn.microsoft.com/en-us/library/cc138531.aspx says that you need the editor name (something like "guidEditorFactory"), not the editor guid, and that's quite difficult to get without the .vsct file of the custom editor. – Carlos Quintero Jul 14 '15 at 15:52
  • Awesome how much things you know about that. I'm not sure which editor is what I want because lack of documentation, but seeying the registry key you mentioned I think could be this key with default value contains `Source Code (Text) Editor` as yours but in my side its package value contains `{e269b994-ef71-4ce0-8bcd-581c217372e8}` if our guids are different this means it could derive in a compatibility issue in other PCs? or maybe not because you said its necessary to use the name, not the guid. – ElektroStudios Jul 14 '15 at 16:05
  • `that's quite difficult to get without the .vsct file of the custom editor.` this means I can't do anything? – ElektroStudios Jul 14 '15 at 16:06
  • 1
    Try posting in the MSDN Visual Studio Integrate forum (https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsx), Microsoft engineers of the VS team visit it and can provide an answer – Carlos Quintero Jul 14 '15 at 18:30
2

I was unable to find the relevant information for Visual Studio 2019 "C# Editor". But it relatively easy, though, not intuitive. To get the guids for the standard editors you can:

  1. Go to Tools > Options > Environment > Keyboard
  2. Select any command in the list
  3. Open Use new shortcut in dropdown, select required editor
  4. Assign any shortcut to it
  5. Save the options
  6. Go to Tools > Import and Export Settings... > Export
  7. Unselect ALL and then choose Environment > Keyboard
  8. Press Export and choose where to save it as .vssettings file
  9. Open the .vssettings file with any text editor
  10. Search for UserShortcuts. You'll find your modified command there
  11. In the Shortcut node, you'll find your scope (in my case Scope="C# Editor")
  12. Search for that scope value (e.g. C# Editor)

You'll find a Scope XML node. In my case it is:

<Scope Name="C# Editor" ID="{A6C744A8-0E4A-4FC6-886A-064283054674}"/>

Copy the id including curly braces, and set it to the value of <GuidSymbol ... /> as described in another answer in this topic. In my case it is:

 <GuidSymbol name="guidCSharpEditor" value="{A6C744A8-0E4A-4FC6-886A-064283054674}" />
Pavel Sapehin
  • 988
  • 12
  • 23