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