Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module KeyPressEvents

    <System.ContextStaticAttribute()> Public WithEvents TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents

    Private output As OutputWindowPane = GetOutputWindowPane("KeyPress Events")

    Public Sub EnableKeyPressEventMacros()
        ' Description: Enables keypress events

        TextDocumentKeyPressEvents = CType(DTE, EnvDTE80.DTE2).Events.GetObject("TextDocumentKeyPressEvents")
    End Sub

    Public Sub DisableKeyPressEventMacros()
        ' Description: Disables keypress events

        TextDocumentKeyPressEvents = Nothing

        If Not IsNothing(output) Then
            output.Clear()
            output = Nothing
        End If
    End Sub

    Private Sub TextDocumentKeyPressEvents_BeforeKeyPress(ByVal Keypress As String, ByVal Selection As EnvDTE.TextSelection, ByVal InStatementCompletion As Boolean, ByRef CancelKeypress As Boolean) Handles TextDocumentKeyPressEvents.BeforeKeyPress
        ' Description: Displays information about the key just pressed and cancels the event
        '              if the key is the 'W' key

        output.OutputString("BeforeKeyPress: ")
        output.OutputString("KeyPress =")
        Dim c As Char
        For Each c In Keypress
            output.OutputString(" " & System.Convert.ToInt32(c).ToString())
        Next
        output.OutputString(", ")
        output.OutputString("Selection = '" & Selection.Text & "', ")
        output.OutputString("InStatementCompletion = " & InStatementCompletion.ToString)
        output.OutputString(vbCrLf)

        If Keypress.ToUpper() = "W" Then
            CancelKeypress = True
            output.OutputString(vbCrLf)
        End If
    End Sub

    Private Sub TextDocumentKeyPressEvents_AfterKeyPress(ByVal Keypress As String, ByVal Selection As EnvDTE.TextSelection, ByVal InStatementCompletion As Boolean) Handles TextDocumentKeyPressEvents.AfterKeyPress
        ' Description: Displays information about the key just pressed

        output.OutputString("AfterKeyPress: ")
        output.OutputString("KeyPress = " & Keypress & ", ")
        output.OutputString("Selection = '" & Selection.Text & "', ")
        output.OutputString("InStatementCompletion = " & InStatementCompletion.ToString)
        output.OutputString(vbCrLf & vbCrLf)
    End Sub


End Module

