Microsoft & .NETVisual BasicBeginning MS Agent: Part 3

Beginning MS Agent: Part 3

I hope everyone enjoyed the first couple of articles I wrote about Microsoft Agent. You should now be able to load and use agents in your programs. It’s been so long since my last article that you should have mastered it by now!

This article is going to deal with obtaining user input from agents. Agents, as you know, can be very effective in engaging the user, so obtaining input through the agents becomes very important.

The method I am going to over is quite simple, yet very effective. As you may have noticed, Agents have a pop-up menu, accessible by either right-mouse clicking on the character or the character’s tray icon. This menu will always have the “Hide” or “Show” option. It is quite simple to add a menu item:

Agent.Characters("CharacterID").Commands.Add Name, _                      Caption, Voice, Enabled, Visible

Placing the above line after the lines for loading your character inside the Form_Load procedure will add the command to your agent’s pop-up menu. Lets go through the above line, piece by piece.

– Agent.Characters(“CharacterID”).Commands.Add
Obviously, the first part makes reference to the character you wish to add the command to. The “.Commands” part of the line references the agents commands collection, where all of the available commands are stored. Finally, “.add” allows you to add a new command to the collection. Other methods of the Commands collection are Insert, Remove, and RemoveAll.

– Name
Required. This value is the name of the command, and is used to identify the command. This will be extremely important later on.

– Caption
Optional. This value contains the text that will be displayed on the characters pop-up menu.

– Voice
Optional. This string value corresponds to the words or phrases that the user can say to allow the Speech Engine to recognize this command.

– Enabled
Optional. This Boolean value determines whether or not the command is enabled. The default value is True.

– Visible
Optional. A Boolean value that determines whether or not the command will appear in the characters pop-up menu. The default is True.

Lets go over an example of adding a command.

Private Sub Form_Load()Agent1.Characters.Load "Peedy", _"e:winntmsagentcharspeedy.acs"Set Assistant = Agent1.Characters("Peedy")Assistant.Commands.RemoveAllAssistant.Commands.Caption = "Assistant Commands" Assistant.Commands.Voice = "Assistant Commands"Assistant.Commands.Add "ACO", _        "Advanced Character Options", _        "[Advanced Character Options][Advanced]"Assistant.ShowEnd Sub

As you can see I have loaded the Peedy character in my application. Before adding a new command however, I have added a couple of more lines. As you can see, I have called the “RemoveAll” method which clears the agent’s commands so that I can properly add new ones. Next I have changed the caption and voice properties. This is so that the commands I add will appear in the Commands Window (accessible by opening the character’s pop-up menu). Finally, I have added a command called “ACO” and called the character’s show animation. For my new command, I have entered a caption property, so that the command will appear in the pop-up menu. I have also entered a string for the voice property, so that I can access this command using voice. Lets look at this in more detail.

"[Advanced Character Options][Advanced]"

As I said before, the voice property defines the words or phrases that can be used to access the command. Here I have added a phrase and a word that can be spoken to access this command. The reason we need to use square brackets is to tell the agent control that there is more than one possible word or phrase to access the command. So the command will be accessed when the user says “Advanced Character Options” OR when the user says “Advanced”. Please note that in order for the agent to listen for commands, the user needs to press the listening hotkey. You can change this hotkey in the properties window that our command loads.

Now that we have added the command to the character, the user can click on the menu item or speak a word or phrase to call the command. Now how do we use that to do something useful? We use the Agent1_Command procedure. The following procedure corresponds to the command we added above.

Private Sub Agent1_Command(ByVal UserInput as Object)Select Case UserInput.Name    Case "ACO"        Agent1.PropertySheet.Visible = TrueEnd SelectEnd Sub

As you can see, we have added the functionality to our command, “ACO”. When the user clicks on the menu item or says one of the voice commands, the Advanced Character Options property sheet is shown. “UserInput” corresponds to the user either selecting the menu option or saying the voice command. The “.name” property is then updated with the name of the command the user called. Here is where naming your command is important. A simple Select structure allows us to easily add something for our command to do.

What if you have lots of commands with voice input commands that are very similar? Well there is a way to only call a command if the agent clearly recognizes the spoken input. Consider our code from above, modified.

Private Sub Agent1_Command(ByVal UserInput as Object) Dim BadConfidence BadConfidence = 10  If (UserInput.Confidence <= -40) Then    ' Bad Recognition    Exit Sub  ElseIf (UserInput.Alt1Name <> "") And _         Abs(Abs(UserInput.Alt1Confidence) - _           Abs(UserInput.Confidence)) _            < BadConfidence Then    ' Bad Confidence - too close to another command    Exit Sub  ElseIf (UserInput.Alt2Name <> "") And _        Abs(Abs(UserInput.Alt2Confidence) - _          Abs(UserInput.Confidence)) _           < BadConfidence Then    ' Bad Confidence - too close to another command    Exit Sub  Else ' High Confidence      Select Case UserInput.Name        Case "ACO"            Agent1.PropertySheet.Visible = True    End Select  End IfEnd Sub

First of all, many thanks to the MASH program, which is where I first learned of this code example. For a more detailed description of this code, please visit the MASH site. Basically what we have done above is define how exact the spoken input needs to be in order to call the command. You can alter the numbers to adjust how sensitive the speech must be. A BadConfidence number of about 10 is a good starting number to limit the speech recognition engine's mistakes.

For some reason, although Microsoft Office Assistants use Agent technology, they are special and have better balloons than regular agents. The balloons used by the Office Assistants allow for text input, menu selection and buttons. Regular agents are not capable of those things. However there is an excellent ActiveX control out there that can add this functionality to your agents, called BalloonDialog. The wide range of options this control allows is excellent, and even better, the control only costs $8.95 USD (I assume its in US dollars). For more information, and to see the control in action, visit the web site at:

http://www.sommytech.com.ar/balloondialog/

Microsoft Agent is an excellent option for an applications user interface. Now that you know how to obtain input from the user, agents are an even better choice for your next project. I hope you found the article informative! As always, continue reading to learn more. The method I described above is only one way, and there are probably more ways of obtaining input.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories