locked
Key Bindings in Settings? RRS feed

  • General discussion

  • Hi all,

    I'm in the process of converting my macros into Add-ins.

    Nearly all of my macros are bound to keystroke combinations and I want to bind my Add-in methods to the same keys. I'm hoping that I can do this via my setting XML file. The bindings are maintained within the "UserShortcuts" element like thus:


    <Category name="Environment_KeyBindings" Category="{F09035F1-80D2-4312-8EC4-4D354A4BCB4C}" Package="{DA9FB551-C724-11d0-AE1F-00A0C90FFFC3}" RegisteredName="Environment_KeyBindings" PackageName="Visual Studio Environment Package">
      <Version>10.0.0.0</Version>
      <KeyboardShortcuts>
        <UserShortcuts>
          <Shortcut Command="Macros.MyMacros.Main_Module.go_to_symbol_def" Scope="Text Editor">Ctrl+Alt+D</Shortcut>


    How could I do this for Add-in methods?

    How could I do this programmatically?


    I've seen this example:

    Public Sub OnConnection(ByVal application As Object, ByVal  _
      connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef  _
      custom As Array) Implements IDTExtensibility2.OnConnection
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)
        BindingsExample(_applicationObject)
                
    End Sub
    
    Sub BindingsExample(ByVal dte As DTE2)
        Dim cmds As Commands
        Dim cmd As Command
        Try
            ' Set references to the Commands collection and the 
            ' File.NewFile command.
            cmds = DTE.Commands
            cmd = cmds.Item("File.NewFile")
    
            ' Assigns the command (File.NewFile) globally to the F2 key.
            cmd.Bindings = "Global::F2"
            MsgBox("key remapped")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    
    http://msdn.microsoft.com/en-us/library/vstudio/ms228753.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    I need a tip as to how I would get a Command reference to a method within my add-in.

    Any assistance with this would be greatly appreciated.

          

    Wally

    Monday, July 1, 2013 1:20 AM

All replies

  • When converting macros to add-ins, you need to create a different command for each macro. When the command is invoked (Exec method) you execute the code inside the macro.

    See how to create commands, get an existing command and execute commands in this article (you can omit all the UI stuff):

    HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in

    http://www.mztools.com/articles/2005/MZ2005003.aspx


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/

    Monday, July 1, 2013 4:50 AM
  • Carlos, thank you for your amazing tools/docs.

    Question:

    Is it possible to create a temporary commandbar without using the DTE?
    Maybe using a service (http://msdn.microsoft.com/en-us/library/bb165789%28v=vs.110%29.aspx)
    or a MEF import?

    Monday, July 1, 2013 3:19 PM
  • Are you creating an add-in or a package? Using an add-in it's only natural to use DTE.

    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/

    Monday, July 1, 2013 4:56 PM
  • Oh, I mean a package...

    I'm still struggling to distinguish the many ways of extending VS...

    I've created a new question.


    • Edited by 3dGrabber Tuesday, July 2, 2013 6:54 AM added hyperlink
    Tuesday, July 2, 2013 6:53 AM