How to make a shortcut to run a VSIX method?

心已入冬 提交于 2019-12-12 08:15:57

问题


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?


回答1:


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:

  • http://dotneteers.net/blogs/divedeeper/archive/2008/04/17/LearnVSXNowPart18A.aspx
  • http://msdn.microsoft.com/de-de/library/bb165138(v=vs.90).aspx
  • http://dotneteers.net/blogs/divedeeper/archive/2008/02/22/LearnVSXNowPart13.aspx


来源:https://stackoverflow.com/questions/10392622/how-to-make-a-shortcut-to-run-a-vsix-method

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