GrammarBuilder.AppendWildcard Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Appends a recognition grammar element that matches any input to the current sequence of grammar elements.

Namespace:   System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public void AppendWildcard()

The wildcard element is added to the end of the current sequence of elements.

The wildcard element matches any spoken word. It does not match background noise or silence.

The following example creates a grammar that accepts password input as a wildcard. The example attaches a Grammar.SpeechRecognized event handler to the grammar that validates the password input.

private Grammar CreatePasswordGrammar()
  GrammarBuilder wildcardBuilder = new GrammarBuilder();
  SemanticResultKey passwordKey =
    new SemanticResultKey("Password", wildcardBuilder);

  GrammarBuilder passwordBuilder =
    new GrammarBuilder("My Password is");

  Grammar passwordGrammar = new Grammar(passwordBuilder);
  passwordGrammar.Name = "Password input";

  passwordGrammar.SpeechRecognized +=
    new EventHandler<SpeechRecognizedEventArgs>(

  return passwordGrammar;

// Handle the SpeechRecognized event for the password grammar.
private void PasswordInputHandler(object sender, SpeechRecognizedEventArgs e)
  if (e.Result == null) return;

  RecognitionResult result = e.Result;
  SemanticValue semantics = e.Result.Semantics;

  if (semantics.ContainsKey("Password"))
    RecognizedAudio passwordAudio =
        result.Words[3], result.Words[result.Words.Count - 1]);

    if (IsValidPassword(passwordAudio))
      Console.WriteLine("Password accepted.");

      // Add code to handle a valid password here.
      Console.WriteLine("Invalid password.");

      // Add code to handle an invalid password here.

// Validate the password input. 
private bool IsValidPassword(RecognizedAudio passwordAudio)
  Console.WriteLine("Validating password.");

  // Add password validation code here.

  return false;

.NET Framework
Available since 3.0
Return to top