C# - Vocal Commands with the MsAgent

Do you remember the MsAgent? These small characters which help and guide you in Microsoft office applications?

The MsAgent get several other capabilities like vocal commands, and from this point they seem to be very useful in attractive and interactive ways.

In a speech enabled application they could make the scene as a bridge between the user and the computer : you tell it something and it makes the computer reacts to the thing you said.

The MsAgent is in fact a powerful character full of essential capacities to interact with the user.

So let's get a look on how to make a MsAgent react to your voice, and compare it to the previous components integrated in the Microsoft Speech Sdk.
Speech recognition permit you to associate one or many words to a command : when one word is recognized, an event will be generated.

In a first time you will have to declare the different references which correspond to the implementation of the MsAgent and declare the objects AxAgent and IagentCtlCharacterEx.

using AgentObjects;
private AxAgentObjects.AxAgent agent;
private IAgentCtlCharacterEx myCharacter;

After that is done, you just have to initialize your agent by this way :

agent = new AxAgentObjects.AxAgent();
agent.Characters.Load("Peedy", (object)"C:/Windows/Msagent/chars/Peedy.acs");
myCharacter = agent.Characters["Peedy"];

You see that you have to begin the initialization of the agent : it is just to add the Agent control to your form or to the object you want the MsAgent to interact with.

Then, You have to load a character, the one you have installed giving its path, and giving it a name.Let's now see how to create some commands for the MsAgent.
In this section we will see the way to create a entire command which will be able to be recognize by the MsAgent with an event.

This example is a creation of a command for a menu :

myCharacter.Commands.Caption = "Test";
myCharacter.Commands.Add("Say Hello", // Command name
(object)"Hello", // Display name
(object)"Hello", // SR Name
(object)true, // Enabled
(object)true); // Visible

To add a command, you just have to define a caption, for example here "Test", and then you will have to define the caracteristics of the command.

You add a command by giving it a name, the display name in the agent configuration, the word that the agent will react to, and finally options to activate it and enable it.

Now you just have created your first command, let's see how to make it recognized.

Finally, don't forget to enable the listening on your character :


The implementation is carried out by factual method called OnCommand. Now let's see how does it work:

private void OnCommand(object sender, AxAgentObjects._AgentEvents_CommandEvent e)
IAgentCtlUserInput ui;
ui = (IAgentCtlUserInput)e.userInput;
if (ui.Name.Equals("Say Hello"))
myCharacter.Speak((object)"Hello", null);

We take the user input with the object IagentCtlUserInput and we are able to get the command name you have implemented above.

You can create whatever you want in the if, in this example we have make the agent speak.
This tutorial was originally written by SatanicPunker

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « C# - Vocal Commands with the MsAgent », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).