KnowDotNet Visual Organizer

Watching Keystrokes in an Add-in in VS2005

What's New in VS2005 Extensibility?

by Les Smith
Print this Article Discuss in Forums

Is is possible to watch the keystrokes in an Add-in in VS2005 (Whidbey)?  Version 1.0 and 1.1 did not provide that capability.  YES!  VS2005 does provide the much needed capability to watch keystrokes in an add-in.   Instead of replacing the old Extensibility Model, a new Namespace has been added, called EnvDTE80, which contains the new functionality in the IDE.  I will show an Import for it, even though the Wizard will have done this for me.

Imports EnvDTE80

To utilize this functionality, I will start by adding events to trap the KeyPress events in the editor.  I will also add an object to catch the TextEditor event that occurs when the Enter key is pressed in the editor.  I will also shorten the name of the applicationObject to oVS for ease of use.
   Public Shared oVS As DTE2 ' was applicationObject

   Public WithEvents eventTextEditor2 As EnvDTE80.TextDocumentKeyPressEvents
  
Public WithEvents eventTextEditor As EnvDTE.TextEditorEvents

In the OnConnection method, I will add code to sink the Text Editor events.  Notie that EnvDTE.TextEditorEvents is a carry over from previous versions of Visual Studio .NET Extensibility.

   ' sink the event handlers for events
   events = oVS.Events
   eventTextEditor =
CType(events.TextEditorEvents(Nothing), _
      EnvDTE.TextEditorEvents)

  
' set up for sinking new text editor keypress events
   eventsNew = oVS.Events
   eventTextEditor2 =
CType(eventsNew.TextDocumentKeyPressEvents(Nothing), _
      EnvDTE80.TextDocumentKeyPressEvents)

Finally, I will add event handlers for the two types of events previously declared.  The KeyPress event spawns two events, Before and After KeyPress.

   ''' <summary>
   ''' This event fires when the Enter key is pressed and also at other time
   ''' when the cursor is moved in the text editor.
   ''' </summary>
   ''' <param name="StartPoint"></param>
   ''' <param name="EndPoint"></param>
   ''' <param name="Hint">
   ''' <remarks>
   Private Sub eventTextEditor_LineChanged(ByVal StartPoint As EnvDTE.TextPoint, _
      
ByVal EndPoint As EnvDTE.TextPoint, ByVal Hint As Integer) _
      
Handles eventTextEditor.LineChanged
      Console.WriteLine(StartPoint.Line.ToString)
  
End Sub

   ''' <summary>
   ''' Fires after the key press event in the IDE.
   ''' </summary>
   ''' <param name="Keypress"></param>
   ''' <param name="Selection"></param>
   ''' <param name="InStatementCompletion">
   ''' <remarks>
   Private Sub eventTextEditor2_AfterKeyPress(ByVal Keypress As String, _
      
ByVal Selection As EnvDTE.TextSelection, _
      
ByVal InStatementCompletion As Boolean) _
      
Handles eventTextEditor2.AfterKeyPress
      
' InStatementCompletion true means Intellisense was matched on the key being pressed
      Debug.WriteLine("after key pressed: " & Keypress & ", Selection: " & _
         Selection.Text &
"InStmtCompletion: " & InStatementCompletion.ToString)
  
End Sub

   ''' <summary>
   ''' Fires before the Keypress event in the IDE.
   ''' </summary>
   ''' <param name="Keypress"></param>
   ''' <param name="Selection"></param>
   ''' <param name="InStatementCompletion"></param>
   ''' <param name="CancelKeypress">
   ''' <remarks>
   Private Sub eventTextEditor2_BeforeKeyPress(ByVal Keypress As String, _
      
ByVal Selection As EnvDTE.TextSelection, ByVal InStatementCompletion As Boolean, _
      
ByRef CancelKeypress As Boolean) Handles eventTextEditor2.BeforeKeyPress
      
' InStatementCompletion true means Intellisense was matched on the key being pressed
      Debug.WriteLine("b4 key pressed: " & Keypress & ", Selection: " & Selection.Text & _
        
"InStmtCompletion: " & InStatementCompletion.ToString)
  
End Sub

The InStatementCompetion argument is True when the keystroke causes Intellisense to show a popup menu.  In the BeforKeyPress event, a True here means that Intellisense will be activated, if not already, by the KeyPress event in the IDE for this particular keystroke.  If you choose to override standard Intellisense, you can set the CancelKeyPress to True before exiting from the event.  In the case of the AfterKeyPress event, a True in InstatementCompletion tells you that Intellisense has already displayed its popup menu for the current keystroke.  Knowing this information will allow you to add your own Intellisense when the normal Intellisense is not activated or even override standard Intellisense.  Neat, uh?

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing