Archives‎ > ‎MSAGENTS‎ > ‎

Integrating voice commands

Addig voice commands to access the functionality of your application is a nice feature. But you may also want to acess the same functionality from a menu or a toolbar button. Using separate procedures to handle voice commands makes that integration easy.


Using procedures to handle voice commands


Start a new project, place an agent control on the form then add three command buttons named cmdHellocmdDate and cmdTime. Set their caption property accordingly. Double click on the form to replace it's content with the following code.



Option Explicit
Dim miFormLeft%, miFormTop%
Dim msLocalPath$
Dim theAgent As IAgentCtlCharacter
Dim reqHB 'As IAgentCtlRequest

Private Sub Form_Load()
    On Error Resume Next
    LoadAgent "genie"
End Sub

Sub CenterForm()
    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    miFormLeft = Me.Left \ Screen.TwipsPerPixelX
    miFormTop = Me.Top \ Screen.TwipsPerPixelY
End Sub

Sub LoadAgent(sAgentName)
    msLocalPath = "c:\program files\microsoft agent\characters\"
    Agent1.Characters.Load sAgentName, msLocalPath & sAgentName & ".acs"
    If Err = 0 Then
        Set theAgent = Agent1.Characters(sAgentName)
        theAgent.MoveTo miFormLeft - theAgent.Width, miFormTop - theAgent.Height
        MsgBox "Cannot load " & sAgentName
    End If
End Sub

Sub AddCommands()
    theAgent.Commands.Caption = "Commands Demo"
    theAgent.Commands.Voice = "Commands Demo"
    theAgent.Commands.Visible = True
    theAgent.Commands.Add "hello", "Hello", "hello", True, True
    theAgent.Commands.Add "date", "Date", "(what date is it |", True, True
    theAgent.Commands.Add "time", "Time", "(what time is it | ...time...)", True, True

End Sub

Private Sub Agent1_Command(ByVal UserInput As Object)
    Select Case UserInput.Name
        Case "hello": SayHello
        Case "date": TellDate
        Case "time": TellTime
    End Select
End Sub

Sub SayHello()
    theAgent.Speak "Hello, my name is " & theAgent.Name & "."
End Sub

Sub TellDate()
    theAgent.Speak "The date is " & Format$(Date, "mmmm dd, yyyy") & "."
End Sub

Sub TellTime()
    theAgent.Speak "The time is " & Time & "."
End Sub

Private Sub cmdDate_Click()
End Sub

Private Sub cmdHello_Click()
End Sub

Private Sub cmdTime_Click()
End Sub

You will also note that the Date and Time commands are also triggered when the user says either words. You can get a complete reference of voice input definitions in theProgramming the Microsoft Agent Control documentation available from the Microsoft site.