AutoHotkey Scan Codes, Speech, Sound, and Splash Images in Children’s Apps (Beginning Hotkeys Part 13)

Write Toddler Educational Hotkey Scripts by Combining AutoHotkey Scan Codes, the SoundPlay Command, the SplashImage Command, and ComObjectCreate() for Speech

I started digging deeper into the weeds of identifying and using AutoHotkey Scan Codes and Virtual Key Codes (introduced last time), when I decided to take a diversion into a little fun, but practical approach to using Scan Codes for educational programs. Sink your AutoHotkey scripting teeth into this educational example. It covers a simple approach for teaching little ones keyboard numbers and letters by combining Hotkey techniques from earlier blogs, plus speech, sound, and SplashImages from chapters in the AutoHotkey Applications book.

ComputorEdge AutoHotkey E-Books*         *          *

This beginning Hotkey blog builds upon the discussions in the previous parts. If you find any of the information too confusing, then reviewing earlier blogs may be worthwhile.

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*          *          *

The most obvious ways for using a computer in children’s educational programs is through multimedia mouse or keyboard interaction. This time we focus on the keyboard. Press a key and the computer reacts with images, talking, and sound. It’s an easy approach to teaching letters, numbers, and sounds…and simple to implement with AutoHotkey. Get creative and use your own photos and voice to stimulate (or irritate) the young ones. While there are many such instructive programs on the market, you can use AutoHotkey to write your own scripts tailored for your kids or grandkids.

CowSplash
Image borrowed from Containers on Wheels (COW). Click image for link.

As an example, I first use numeric keypad Scan Codes to voice the numbers on the keys. Then, I demonstrate a short snippet which, whenever the letter C is pressed, displays a SplashImage picture of a cow on roller skates (borrowed from Containers on Wheels COW), plays the downloaded sound of a laughing cow (run by the SoundPlay command), and reads the title and caption words (spelling the word COW) using the ComObjCreate(“SAPI.SpVoice”) speech synthesizing function. (This short script, NumbersSpeak.ahk, can be downloaded from the ComputorEdge AutoHotkey download site in the ZIP file NumbersSpeak.zip.)

While the Laughing Cow is just one Hotkey example of what can be done for the betterment of your children, it can easily be expanded to include the entire keyboard. In fact, I started this example by writing simple code for reading the numbers on the numeric pad out loud.

Isolate Hotkeys within an Active Window Using the #IfWinActive Directive

When remapping the keyboard with Hotkeys, I recommend that you contain those new Hotkeys within an active window. That way those Hotkeys won’t affect any other running programs. To accomplish that goal, use a specific open window and the #IfWinActive directive:

#IfWinActive, Numbers Speak
;
;  Place all Hotkeys between the two #IfWinActive statements
;
#IfWinActive

I picked the name Numbers Speak as the containing window title for all the new Hotkeys. I placed each new Hotkey between the two #IfWinActive directives so that they only work with the Numbers Speak window selected. (The #IfWinActive directive is discussed in the blog “AutoHotkey #Directives for Context-Sensitive Hotkeys—#IfWinActive (Beginning Hotkeys Part 3).”

Pressing each key on the numeric keypad should cause the computer to say that number, but I need an easy way to make the computer speak. For that. I consulted Chapter Thirty-two, “An Easier Way to Get Your Computer to Talk to You (ComObjCreate)” in my AutoHotkey Applications book. In the example from the book, the ComObjCreat(“SAPI.SpVoice”) function activates the computer speech synthesizer and speaks the value of the variable SpeakOutLoud:

ComObjCreate("SAPI.SpVoice").Speak(SpeakOutLoud)

By substituting the keypad number for the SpeakOutLoud variable, the computer speaks that number (e.g. ComObjCreate(“SAPI.SpVoice”).Speak(1)).

Simplify Numeric Keypad Hotkeys with AutoHotkey Scan Codes

While turning letters into Hotkeys for kid’s apps is relatively straightforward (c::MsgBox, This is the letter C!), doing the same for numbers on the numeric keypad gets a little more complex. The concept of using the NumLock key may be too much for young children. When children see or hit a number, they should hear that number—regardless of the position of NumLock (on or off). You could use the SetNumLockState command in the script, but that only complicates things. Implementing numeric pad keyboard Scan Codes (discussed last time) completely circumvents the need to mess with NumLock:

#IfWinActive, Numbers Speak
  sc052::ComObjCreate("SAPI.SpVoice").Speak(0)
  sc04F::ComObjCreate("SAPI.SpVoice").Speak(1)
  sc050::ComObjCreate("SAPI.SpVoice").Speak(2)
  sc051:: ComObjCreate("SAPI.SpVoice").Speak(3)
  sc04B::ComObjCreate("SAPI.SpVoice").Speak(4)
  sc04C::ComObjCreate("SAPI.SpVoice").Speak(5)
  sc04D::ComObjCreate("SAPI.SpVoice").Speak(6)
  sc047::ComObjCreate("SAPI.SpVoice").Speak(7)
  sc048::ComObjCreate("SAPI.SpVoice").Speak(8)
  sc049::ComObjCreate("SAPI.SpVoice").Speak(9)
  sc11c::ComObjCreate("SAPI.SpVoice").Speak("enter")
#IfWinActive

The Scan Codes shown correspond to the numeric keypad key of each number spoken. Since Scan Codes react to pressing a specific hardwired key, AutoHotkey ignores the position of NumLock (on or off)—easier for kids.

The Hotkey Containment Window

NumbersSpeakI needed a script to isolate the new Hotkeys from the rest of the computer via the #IfWinActive directive. It could be any script or program, but it must include an onscreen window which activates and deactivates—not merely Hotkeys or Hotstrings. I choose as the Hotkey containment window the script SayWhat.ahk (from the same chapter of the AutoHotkey Applications book),:

Gui, Add, Text,, Read to me:
Gui, Add, Edit, w375 vSpeakOutLoud 
Gui, Add, Button, default, Talk 
Gui, Show, w400 , Numbers Speak
Return

ButtonTalk:
 Gui, Submit, NoHide
 GuiControl,,SpeakOutLoud, ;Clear the text editing box
 ComObjCreate("SAPI.SpVoice").Speak(SpeakOutLoud)
 GuiControl,Focus,Edit1
Return

After pressing the Talk button, the computer reads out loud the text typed into the GUI Edit field. In this script, I only changed the Gui, Show line to the window title Numbers Speak and added the GuiControl, Focus, Edit1 line—which, after clicking the Talk button, resets the text cursor to the Edit field.

While I could have used any other GUI script, this one gave me the added benefit of the flexible type-and-hear speech capability. My granddaughter ordered me to spell words such as “Daddy” and “Mommy” which, to her great amusement, the computer spoke out loud.

Note: In the long run, this GUI is not the best choice for a Hotkey containment window. As Hotkeys are added, the keys are blocked for text input into the Edit field.

Just for fun, let’s add a laughing cow.

How to Create Children’s Hotkeys with Speech, Sound, and Images

For this portion of the children’s app, I consulted Chapter Six, “A Multimedia Greeting Card (Progress/Splash Image, ComObjCreate())” in the same AutoHotkey Applications book. The AutoHotkey commands highlighted in that chapter gave me all the commands I needed to turn the C key into a laughing cow Hotkey:

c::
  SplashImage, cow-skating.jpg, cwYellow ctBlue 
    , C IS FOR COW!, Is the cow laughing?
    , Laughing Cow 
  ComObjCreate("SAPI.SpVoice")
    .Speak("C IS FOR COW, Is the cow laughing? c , , o , , w , , cow")
  SoundPlay, cow-madcow.wav
  Sleep 15000
  SplashImage, off
Return

The letter C becomes the Hotkey which, using the SplashImage command, immediately displays the image seen at the beginning of this blog. Then, AutoHotkey reads out loud the words “C is for cow.” Is the cow laughing? C , , O , , W , , COW” (the extra commas provide slight pauses) using the ComObjCreate(“SAPI.SpVoice”) function. Finally, SoundPlay activates the mad cow laughing file (cow-madcow.wav).

Note: In this snippet for display purposes only, AutoHotkey line continuation techniques break the longer SplashImage and ComObjCreate() lines into multiple lines (each read by AutoHotkey as one continuous line).

Tip: If I were to complete this app for the entire keyboard, I would write a function which includes the commands repeated for each key press of the keyboard. That way I wouldn’t need to rewrite all the commands for every letter of the alphabet—only the data which changes (image file name, window title and captions, spoken words, and the WAV sound file name). See this blog about writing AutoHotkey functions or maybe this one.

Dancing Dogs
Dancing Dogs borrowed from www.clipartqueen.com

If you’re thinking of going all out on this type of children’s educational script, then you won’t want to use my SayWhat.ahk script as your Hotkey containment window. Each time you add another Hotkey letter to the script, it blocks itself—which defeats the SayWhat.ahk script’s original purpose. You could use the tilde prefix (~  option) Hotkey parameter to pass through the letters, but you’ll soon get tired of waiting for laughing cows or dancing dogs (D is for Dancing Dogs) each time you type a letter in the Edit field.

As originally planned for this time, next time, I will explore techniques for identifying and using Scan Codes and Virtual Keys in AutoHotkey scripts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s