Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

Binding Functions to Keystrokes

$
0
0

Hello,

I am trying to migrate code that was once my macros to a VS package.

This is for VS 2015 Community on Win 10.

It seems that I can get the DTE object that my old macro code depends upon:

    DTE2 L_dte = (DTE2)Package.GetGlobalService(typeof(DTE));

My problem is binding keystrokes to the functions.


  <KeyBindings>
    <KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id" editor="guidVSStd97"
                key1="F" mod1="CONTROL ALT SHIFT"   />
    <KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id_01" editor="guidVSStd97"
                key1="D" mod1="CONTROL ALT SHIFT"   />
    <KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id_02" editor="guidVSStd97"
                key1="D" mod1="CONTROL ALT"   />
    <KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id_03" editor="guidVSStd97"
                key1="D" mod1="CONTROL"   />
  </KeyBindings>


The first KeyBinding works, but the rest do not.

I can activate all of the functions introduced as menu items, but they
need to be bound to keystrokes, as virtually all of my functions
perform text manipulation within the code/text editor, and using menu
items for that is next to useless.


Here is the test code for vspackage_02.cs:

//------------------------------------------------------------------------------
// <copyright file="VSPackage_02.cs" company="Company">
//     Copyright (c) Company.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.Win32;

namespace VSIX_Project_02
{
    /// <summary>
    /// This is the class that implements the package exposed by this assembly.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The minimum requirement for a class to be considered a valid package for Visual Studio
    /// is to implement the IVsPackage interface and register itself with the shell.
    /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
    /// to do it: it derives from the Package class that provides the implementation of the
    /// IVsPackage interface and uses the registration attributes defined in the framework to
    /// register itself and its components with the shell. These attributes tell the pkgdef creation
    /// utility what data to put into .pkgdef file.
    /// </para>
    /// <para>
    /// To get loaded into VS, the package must be referred by &lt;Asset Type="Microsoft.VisualStudio.VsPackage" ...&gt; in .vsixmanifest file.
    /// </para>
    /// </remarks>
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
    [Guid(VSPackage_02.PackageGuidString)]
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    public sealed class VSPackage_02 : Package
    {
        /// <summary>
        /// VSPackage_02 GUID string.
        /// </summary>
        public const string PackageGuidString = "a5aa6c96-41f6-4685-b26e-3ae3bb9f7fc6";

        /// <summary>
        /// Initializes a new instance of the <see cref="VSPackage_02"/> class.
        /// </summary>
        public VSPackage_02()
        {
            // Inside this method you can place any initialization code that does not require
            // any Visual Studio service because at this point the package object is created but
            // not sited yet inside Visual Studio environment. The place to do all the other
            // initialization is the Initialize method.
        }

        #region Package Members

        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
            Command_02.Initialize(this);
        }

        #endregion
    }
}


Here is the test code for VSPackage_02.vsct:

<?xml version="1.0" encoding="utf-8"?><CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema"><!--  This is the file that defines the actual layout and type of the commands.
        It is divided in different sections (e.g. command definition, command
        placement, ...), with each defining a specific set of properties.
        See the comment before each section for more details about how to
        use it. --><!--  The VSCT compiler (the tool that translates this file into the binary
        format that VisualStudio will consume) has the ability to run a preprocessor
        on the vsct file; this preprocessor is (usually) the C++ preprocessor, so
        it is possible to define includes and macros with the same syntax used
        in C++ files. Using this ability of the compiler here, we include some files
        defining some of the constants that we will use inside the file. --><!--This is the file that defines the IDs for all the commands exposed by VisualStudio. --><Extern href="stdidcmd.h"/><!--This header contains the command ids for the menus provided by the shell. --><Extern href="vsshlids.h"/><!--The Commands section is where commands, menus, and menu groups are defined.
      This section uses a Guid to identify the package that provides the command defined inside it. --><Commands package="guidVSPackage_02"><!-- Inside this section we have different sub-sections: one for the menus, another
    for the menu groups, one for the buttons (the actual commands), one for the combos
    and the last one for the bitmaps used. Each element is identified by a command id that
    is a unique pair of guid and numeric identifier; the guid part of the identifier is usually
    called "command set" and is used to group different command inside a logically related
    group; your package should define its own command set in order to avoid collisions
    with command ids defined by other packages. --><!-- In this section you can define new menu groups. A menu group is a container for
         other menus or buttons (commands); from a visual point of view you can see the
         group as the part of a menu contained between two lines. The parent of a group
         must be a menu. --><Groups><Group guid="guidVSPackage_02CmdSet" id="MyMenuGroup" priority="0x0600"><Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/></Group></Groups><!--Buttons section. --><!--This section defines the elements the user can interact with, like a menu command or a button
        or combo box in a toolbar. --><Buttons><!--To define a menu group you have to specify its ID, the parent menu and its display priority.
          The command is visible and enabled by default. If you need to change the visibility, status, etc, you can use
          the CommandFlag node.
          You can add more than one CommandFlag node e.g.:<CommandFlag>DefaultInvisible</CommandFlag><CommandFlag>DynamicVisibility</CommandFlag>
          If you do not want an image next to your command, remove the Icon node /> --><Button guid="guidVSPackage_02CmdSet" id="Command_02Id" priority="0x0100" type="Button"><Parent guid="guidVSPackage_02CmdSet" id="MyMenuGroup" /><Icon guid="guidImages" id="bmpPic1" /><Strings><ButtonText>Invoke Command_02</ButtonText></Strings></Button><Button guid="guidVSPackage_02CmdSet" id="Command_02Id_01" priority="0x0100" type="Button"><Parent guid="guidVSPackage_02CmdSet" id="MyMenuGroup" /><Icon guid="guidImages" id="bmpPic1" /><Strings><ButtonText>Invoke Command_02a</ButtonText></Strings></Button><Button guid="guidVSPackage_02CmdSet" id="Command_02Id_02" priority="0x0100" type="Button"><Parent guid="guidVSPackage_02CmdSet" id="MyMenuGroup" /><Icon guid="guidImages" id="bmpPic1" /><Strings><ButtonText>Invoke Command_02b</ButtonText></Strings></Button><Button guid="guidVSPackage_02CmdSet" id="Command_02Id_03" priority="0x0100" type="Button"><Parent guid="guidVSPackage_02CmdSet" id="MyMenuGroup" /><Icon guid="guidImages" id="bmpPic1" /><Strings><ButtonText>Invoke Command_02c</ButtonText></Strings></Button></Buttons><!--The bitmaps section is used to define the bitmaps that are used for the commands.--><Bitmaps><!--  The bitmap id is defined in a way that is a little bit different from the others:
            the declaration starts with a guid for the bitmap strip, then there is the resource id of the
            bitmap strip containing the bitmaps and then there are the numeric ids of the elements used
            inside a button definition. An important aspect of this declaration is that the element id
            must be the actual index (1-based) of the bitmap inside the bitmap strip. --><Bitmap guid="guidImages" href="Resources\Command_02.png"
              usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/></Bitmaps></Commands><KeyBindings><KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id" editor="guidVSStd97"
                key1="F" mod1="CONTROL ALT SHIFT"   /><KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id_01" editor="guidVSStd97"
                key1="D" mod1="CONTROL ALT SHIFT"   /><KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id_02" editor="guidVSStd97"
                key1="D" mod1="CONTROL ALT"   /><KeyBinding guid="guidVSPackage_02CmdSet" id="Command_02Id_03" editor="guidVSStd97"
                key1="D" mod1="CONTROL"   /><!--<KeyBinding guid="MyProjectCmdSet" id="idCommand1" editor="guidVSStd97" key1="A" mod1="ALT" /><KeyBinding guid="MyProjectCmdSet" id="idCommand2" editor="guidVSStd97" key1="A" mod1="CONTROL" mod2="ALT" />--></KeyBindings><Symbols><!-- This is the package guid. --><GuidSymbol name="guidVSPackage_02" value="{a5aa6c96-41f6-4685-b26e-3ae3bb9f7fc6}" /><!-- This is the guid used to group the menu commands together --><GuidSymbol name="guidVSPackage_02CmdSet" value="{6cec10ca-e151-4037-bb88-2da2e939aeae}"><IDSymbol name="MyMenuGroup" value="0x1020" /><IDSymbol name="Command_02Id" value="0x0100" /><IDSymbol name="Command_02Id_01" value="0x0101" /><IDSymbol name="Command_02Id_02" value="0x0102" /><IDSymbol name="Command_02Id_03" value="0x0103" /></GuidSymbol><GuidSymbol name="guidImages" value="{af5ba219-dbd7-4e31-95d8-86246de5c445}" ><IDSymbol name="bmpPic1" value="1" /><IDSymbol name="bmpPic2" value="2" /><IDSymbol name="bmpPicSearch" value="3" /><IDSymbol name="bmpPicX" value="4" /><IDSymbol name="bmpPicArrows" value="5" /><IDSymbol name="bmpPicStrikethrough" value="6" /></GuidSymbol></Symbols></CommandTable>


Here is the test code for command_02.cs:

//------------------------------------------------------------------------------
// <copyright file="Command_02.cs" company="Company">
//     Copyright (c) Company.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

using EnvDTE;
using EnvDTE80;

namespace VSIX_Project_02
{
    /// <summary>
    /// Command handler
    /// </summary>
    internal sealed class Command_02
    {
        /// <summary>
        /// Command ID.
        /// </summary>
        public const int CommandId = 0x0100;
        public const int CommandId_01 = 0x0101;
        public const int CommandId_02 = 0x0102;
        public const int CommandId_03 = 0x0103;

        /// <summary>
        /// Command menu group (command set GUID).
        /// </summary>
        public static readonly Guid CommandSet = new Guid("6cec10ca-e151-4037-bb88-2da2e939aeae");

        /// <summary>
        /// VS Package that provides this command, not null.
        /// </summary>
        private readonly Package package;

        /// <summary>
        /// Initializes a new instance of the <see cref="Command_02"/> class.
        /// Adds our command handlers for menu (commands must exist in the command table file)
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        private Command_02(Package package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            this.package = package;

            OleMenuCommandService
            commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService))
                           as OleMenuCommandService;
            if (commandService != null)
            {
                var menuCommandID = new CommandID(CommandSet, CommandId);
                var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
                commandService.AddCommand(menuItem);
                var menuCommandID_01 = new CommandID(CommandSet, CommandId_01);
                var menuItem_01 = new MenuCommand(this.MenuItemCallback_01, menuCommandID_01);
                commandService.AddCommand(menuItem_01);
                var menuCommandID_02 = new CommandID(CommandSet, CommandId_02);
                var menuItem_02 = new MenuCommand(this.MenuItemCallback_02, menuCommandID_02);
                commandService.AddCommand(menuItem_02);
                var menuCommandID_03 = new CommandID(CommandSet, CommandId_03);
                var menuItem_03 = new MenuCommand(this.MenuItemCallback_03, menuCommandID_03);
                commandService.AddCommand(menuItem_03);
            }
        }

        /// <summary>
        /// Gets the instance of the command.
        /// </summary>
        public static Command_02 Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the service provider from the owner package.
        /// </summary>
        private IServiceProvider ServiceProvider
        {
            get
            {
                return this.package;
            }
        }

        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static void Initialize(Package package)
        {
            Instance = new Command_02(package);
        }

        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
            string title = "Command_02";

            // Show a message box to prove we were here
            VsShellUtilities.ShowMessageBox(
                this.ServiceProvider,
                message,
                title,
                OLEMSGICON.OLEMSGICON_INFO,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
        }


        private void MenuItemCallback_01(object sender, EventArgs e)
        {
            MenuItemCallback_mssg("Command_01", "01");
        }
        private void MenuItemCallback_02(object sender, EventArgs e)
        {
            MenuItemCallback_mssg("Command_02", "02");
        }
        private void MenuItemCallback_03(object sender, EventArgs e)
        {
            MenuItemCallback_mssg("Command_03", "03");
        }

        private void MenuItemCallback_mssg(string title, string ndx)
        {
            string message = string.Format
                                (CultureInfo.CurrentCulture,
                             "Inside {0}.MenuItemCallback_{1}()", this.GetType().FullName, ndx);

            // Show a message box to prove we were here
            VsShellUtilities.ShowMessageBox(
                this.ServiceProvider,
                message,
                title,
                OLEMSGICON.OLEMSGICON_INFO,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);


            //DTE2 dte = (DTE2)Package.GetService(typeof(DTE));
            DTE2 L_dte = (DTE2)Package.GetGlobalService(typeof(DTE));

            Document doc = L_dte.ActiveDocument;
            EditPoint objEditPt;
            EnvDTE.TextDocument objTextDocument;
            EnvDTE.TextPoint objCursorTextPoint;

            //objTextDocument = CType(DTE.ActiveDocument.Object, EnvDTE.TextDocument);

            if (L_dte.ActiveDocument != null)
            {
                objTextDocument = (EnvDTE.TextDocument)L_dte.ActiveDocument.Object();
                objCursorTextPoint = objTextDocument.Selection.ActivePoint;
                TextDocument textDoc = (EnvDTE.TextDocument)doc.Object("TextDocument");
            }
        }
    }
}



I've been adding the test functions as menu items mainly for testing
purposes, and to try to understand as to what is working, and what is
not. (I need to know how to get rid of them once I've fixed my main
problem.)


Again, All I want to do is to simply bind my DTE functions to keystrokes.

Is there a simple way to do that?


As a side note ...

When Microsoft did away with macros, I was one of many that were PO'ed.

I managed to migrate them to an addin, but it took me several days to
figure it out.

Then, Microsoft did away with addins for VS 2015.

I am PO'ed X 3.

If you've used macros in the past and want them back, then PLEASE go to
uservoice and express your displeasure:

https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2650757-bring-back-macros




Wally


Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>