This topic has not yet been rated - Rate this topic

Walkthrough: Using a Shortcut Key with an Editor Extension

You can respond to shortcut keys in your editor extension. The following walkthrough shows how to add a view adornment to a text view by using a shortcut key. This walkthrough is based on the viewport adornment editor template, and it allows you to add the adornment by using the + character.

To follow this walkthrough, you must install the Visual Studio 2013 SDK. For more information, see Visual Studio Software Development Kit (SDK).

To create a MEF project

  1. Create an Editor Viewport Adornment project. Name the solution KeyBindingTest.

  2. Open the source.extension.vsixmanifest file in the VSIX Manifest Editor.

  3. Make sure that the Content heading contains a MEF Component content type and that the Path is set to KeyBindingTest.dll.

  4. Save and close Source.extension.vsixmanifest.

  5. Add the following references and set CopyLocal to false:





  6. Delete the KeyBindingTestFactory class file.

In the KeyBindingTest class file, change the class name to PurpleCornerBox. Change the constructor as well. Inside the constructor, change the line:

_adornmentLayer = view.GetAdornmentLayer("KeyBindingTest")


_adornmentLayer = view.GetAdornmentLayer("PurpleCornerBox")

The command filter is an implementation of IOleCommandTarget, which handles the command by instantiating the adornment.

To define the command filter

  1. Add a class file and name it KeyBindingCommandFilter.

  2. Add the following using statements.

    Imports System
    Imports System.Runtime.InteropServices
    Imports Microsoft.VisualStudio.OLE.Interop
    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Text.Editor
  3. The class named KeyBindingCommandFilter should inherit from IOleCommandTarget.

    Friend Class KeyBindingCommandFilter
        Implements IOleCommandTarget
  4. Add private fields for the text view, the next command in the command chain, and a flag to represent whether the command filter has already been added.

    Private m_textView As IWpfTextView
    Friend m_nextTarget As IOleCommandTarget
    Friend m_added As Boolean 
    Friend m_adorned As Boolean
  5. Add a constructor that sets the text view.

    Public Sub New(ByVal textView As IWpfTextView)
        m_textView = textView
        m_adorned = False 
    End Sub
  6. Implement the QueryStatus() method as follows.

    Private Function QueryStatus(ByRef pguidCmdGroup As Guid, ByVal cCmds As UInteger, ByVal prgCmds() As OLECMD, ByVal pCmdText As IntPtr) As Integer Implements IOleCommandTarget.QueryStatus
        Return m_nextTarget.QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText)
    End Function
  7. Implement the Exec() method so that it adds a purple box to the view if a + character is typed.

    Private Function Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdID As UInteger, ByVal nCmdexecopt As UInteger, ByVal pvaIn As IntPtr, ByVal pvaOut As IntPtr) As Integer Implements IOleCommandTarget.Exec
        If m_adorned = False Then 
            Dim typedChar As Char = Char.MinValue
            If pguidCmdGroup = VSConstants.VSStd2K AndAlso nCmdID = CUInt(VSConstants.VSStd2KCmdID.TYPECHAR) Then
                typedChar = CChar(ChrW(Marshal.GetObjectForNativeVariant(pvaIn)))
                If typedChar.Equals("+"c) Then 
                    Dim TempPurpleCornerBox As PurpleCornerBox = New PurpleCornerBox(m_textView)
                    m_adorned = True 
                End If 
            End If 
        End If 
        Return m_nextTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut)
    End Function

The adornment provider must add a command filter to the text view. In this example, the provider implements IVsTextViewCreationListener to listen to text view creation events. This adornment provider also exports the adornment layer, which defines the Z-order of the adornment.

To add the command filter provider

  1. Add a class file and name it KeyBindingFilterProvider.

  2. Add the following using statements.

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel.Composition
    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.OLE.Interop
    Imports Microsoft.VisualStudio.Utilities
    Imports Microsoft.VisualStudio.Editor
    Imports Microsoft.VisualStudio.Text.Editor
    Imports Microsoft.VisualStudio.TextManager.Interop
  3. Add a class named KeyBindingFilterProvider that inherits from IVsTextViewCreationListener, and export it with a content type of "text" and a TextViewRoleAttribute of Editable.

    <Export(GetType(IVsTextViewCreationListener)), ContentType("text"), TextViewRole(PredefinedTextViewRoles.Editable)>
    Friend Class KeyBindingCommandFilterProvider
        Implements IVsTextViewCreationListener
  4. Add the adornment layer definition.

    <Export(GetType(AdornmentLayerDefinition)), Name("PurpleCornerBox"), Order(), TextViewRole(PredefinedTextViewRoles.Editable)>
    Friend keybindingAdornmentLayer As AdornmentLayerDefinition
  5. To get the text view adapter, you must import the IVsEditorAdaptersFactoryService.

    Friend editorFactory As IVsEditorAdaptersFactoryService = Nothing
  6. Implement the VsTextViewCreated method so that it adds the KeyBindingCommandFilter.

    Public Sub VsTextViewCreated(ByVal textViewAdapter As IVsTextView) Implements IVsTextViewCreationListener.VsTextViewCreated
        Dim textView As IWpfTextView = editorFactory.GetWpfTextView(textViewAdapter)
        If textView Is Nothing Then 
        End If
        AddCommandFilter(textViewAdapter, New KeyBindingCommandFilter(textView))
    End Sub
  7. The AddCommandFilter handler gets the text view adapter and adds the command filter.

    Private Sub AddCommandFilter(ByVal viewAdapter As IVsTextView, ByVal commandFilter As KeyBindingCommandFilter)
        If commandFilter.m_added = False Then 
            'get the view adapter from the editor factory 
            Dim [next] As IOleCommandTarget
            Dim hr As Integer = viewAdapter.AddCommandFilter(commandFilter, [next])
            If hr = VSConstants.S_OK Then
                commandFilter.m_added = True 
                'you'll need the next target for Exec and QueryStatus 
                If [next] IsNot Nothing Then
                    commandFilter.m_nextTarget = [next]
                End If 
            End If 
        End If 
    End Sub

To test this code, build the KeyBindingTest solution and run it in the experimental instance.

To build and test the KeyBindingTest solution

  1. Build the solution.

  2. When you run this project in the debugger, a second instance of Visual Studio is instantiated.

  3. Create or open a text file, and then click anywhere in the text view.

  4. Type +.

    Resize the text view.

    A purple square should appear in the upper right corner of the text view.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.