4

please see this question.

The initial question was solved, and I have a method in my extension. I want that when the user installs the extension, a keyboard-shortcut should be installed as well, and should run the method when pressed.

How do I do this?

Community
  • 1
  • 1
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

1 Answers1

4

You can add shortcuts to the .vsct file. This file is created automatically in the wizard when you generate a new extension and say it will have a menu command. To add it manually:

  • Create MyCommands.vsct
  • Set File Properties to VSCTCompile
  • Unload your project, right click and edit project:
<VSCTCompile Include="MyCommands.vsct">
    <ResourceName>Menus.ctmenu</ResourceName>
    <SubType>Designer</SubType>
</VSCTCompile>
  • Declare that your project will have menus and shortcuts:
[ProvideMenuResource("Menus.ctmenu",1)]
public sealed class MyPackage : Package
  • Add a keybindings section:
<KeyBindings>
   <KeyBinding guid="yourCmdSet" id="cmdAwesome"
    editor="guidVSStd97"
    key1="VK_F7" mod1="Control Alt"
    key2="VK_F1">
   </KeyBinding>
</KeyBindings>
  • In your Package.Initialize:
// Add our command handlers for menu/shortcut (commands must exist in the .vsct file)
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (null != mcs)
{
    //// Create the command for the menu item.
    var menuCommandID = new CommandID(GuidList.yourCmdSet,(int)PkgCmdIDList.cmdAwesome);
    var menuItem = new MenuCommand((sender, evt) =>
    {
        // Do stuff
    }
}

More resources:

MSeifert
  • 145,886
  • 38
  • 333
  • 352
gameweld
  • 1,319
  • 15
  • 21
  • 2
    Also make sure the `` section is right after the ``. I added it randomly in the file and got xml schema error. – elirandav Jul 09 '17 at 17:54