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()
If Not IsWhitespace(editPoint.GetText(1)) Then
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
If HasLetter Then
If Not IsWhiteSpace(Keypress) Then
Say = Word & ", " & Say
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
SVSFPersistXML = 32
'Normalizer Flags
SVSFNLPSpeakPunc = 64
SVSFNLPMask = 64
SVSFVoiceMask = 127
SVSFUnusedFlags = -128
End Enum

Add comment

(Will show your Gravatar icon)  

  Country flag

  • Comment
  • Preview


Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

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 was previously 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 have no affiliation with, and are not representative of, his former employer in any way.

Contact Me 

Tag cloud


<<  August 2020  >>

View posts in large calendar