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 |
| Public Shared oVS As DTE2 ' was applicationObject |
| Public WithEvents eventTextEditor2 As EnvDTE80.TextDocumentKeyPressEvents Public WithEvents eventTextEditor As EnvDTE.TextEditorEvents |
| ' 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) |
| ''' <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 |