Stupid Visual Studio Tricks: Speak ‘N Code

by Jon Davis 7. March 2009 01:58

I got bored tonight and created a Visual Studio macro that reads my code to me as I type it. First I had it read the keys (“a”, “b”) like a Speak ‘N Spell, but then I realized it’s better if it actually just waits for me to spell out the words instead. It also correctly pronounces a few special characters.

I have absolutely no use for this but for “duhh huh huh huh” giggles.

If you care to play with it, just open Visual Studio, choose Tools –> Macros –> Macros IDE from the menu, double-click MyMacros on the left, double-click EnvironmentEvents, then paste the following code inside the module body.

Private Speech As Object

Private Sub TextDocumentKeyPressEvents_BeforeKeyPress(ByVal Keypress _
As String, ByVal Selection As EnvDTE.TextSelection, _
ByVal InStatementCompletion As Boolean, ByRef CancelKeyPress As Boolean) _
Handles TextDocumentKeyPressEvents.BeforeKeyPress
    If Speech Is Nothing Then
        Speech = Activator.CreateInstance(Type.GetTypeFromProgID("sapi.spvoice"))
        Speech.Voice = Speech.GetVoices("gender=male")(0)
    End If
    Dim Key As String
    Key = Keypress
    If Key = vbBack Then Return
    Select Case Keypress
        Case "!"
            Key = "not"
        Case "%"
            Key = "mod"
        Case "@"
            Key = "at"
        Case "#"
            Key = "pound"
        Case "^"
            Key = "caret"
        Case "&"
            Key = "and"
        Case "*"
            Key = "star"
        Case "/"
            Key = "slash"
        Case "\"
            Key = "backslash"
        Case ","
            Key = "comma"
        Case "<"
            Key = "less than"
        Case ">"
            Key = "greater than"
        Case ":"
            Key = "colon"
        Case "("
            Key = "" ' "pahrens"
        Case ")"
            Key = "" ' "close pahrens"
        Case "."
            Key = "dot"
        Case "{"
            Key = "" ' "begin brace"
        Case "}"
            Key = "" ' "close brace"
        Case ";"
            Key = "" ' "semicolon"
        Case vbTab
            Key = "" ' "tab"
        Case " "
            Key = "" ' "space"
        Case "["
            Key = "" ' "left bracket"
        Case "]"
            Key = "" ' "right bracket"
        Case """"
            Key = "" ' "double quotes"
        Case "'"
            Key = "single quote"
        Case vbCr, vbLf, vbCrLf
            Key = "" ' "line break"
    End Select
    Dim Say As String
    Const a2z As String = "abcdefghijklmnopqrstuvwxyz"
    If Not a2z.Contains(Keypress.ToLower()) Then
        Say = Key
        Dim editPoint = Selection.ActivePoint().CreateEditPoint()
        editPoint.CharLeft()
        If Not IsWhitespace(editPoint.GetText(1)) Then
            editPoint.CharRight()
            editPoint.WordLeft()
            Dim Word = editPoint.GetText(Selection.ActivePoint.AbsoluteCharOffset - editPoint.AbsoluteCharOffset)
            Dim HasLetter As Boolean
            For Each letter In Word
                If a2z.Contains(letter) Then HasLetter = True
            Next
            If HasLetter Then
                If Not IsWhiteSpace(Keypress) Then
                    Say = Word & ", " & Say
                Else
                    Say = Word
                End If
            End If
        End If

    End If

    If Say <> "" Then
        Speech.Speak(Say, SpeechVoiceSpeakFlags.SVSFlagsAsync + SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak)
    End If
End Sub

Function IsWhitespace(ByVal s As String)
    Dim rgx As New System.Text.RegularExpressions.Regex("\s*")
    Return rgx.Match(s).Length > 0
End Function

Enum SpeechVoiceSpeakFlags
    'SpVoice Flags
    SVSFDefault = 0
    SVSFlagsAsync = 1
    SVSFPurgeBeforeSpeak = 2
    SVSFIsFilename = 4
    SVSFIsXML = 8
    SVSFIsNotXML = 16
    SVSFPersistXML = 32

    'Normalizer Flags
    SVSFNLPSpeakPunc = 64

    'Masks
    SVSFNLPMask = 64
    SVSFVoiceMask = 127
    SVSFUnusedFlags = -128
End Enum

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

VB.NET | Pet Projects

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading




 

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Jon Davis Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
 
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon is presently employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of jondavis.net have no affiliation with, and are not representative of, his employer in any way.


Amazon Collection

Most Recent of Many Library Investments

Tag cloud

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar