SpeechRecoContext State Property (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3

Interface: ISpeechRecoContext

State Property

The State property gets or sets the active state of the recognition context.

The entire grammar associated with the recognition context can be disabled or enabled. The individual rule states of the grammar are unaffected otherwise. These conditions allow the application to control the state of the grammars at a high level. For example, if the window loses the current focus, the recognition context (s) associated with that window may be disabled if recognitions are not needed. Likewise, when the window regains the focus, all the recognition contexts may be enabled.


Set: SpeechRecoContext.State = SpeechRecoContextState
Get: SpeechRecoContextState = SpeechRecoContext.State


The owning object.
Set: A SpeechRecoContextState variable that sets the property.
Get: A SpeechRecoContextState variable that gets the property.


The following Visual Basic form code demonstrates how disabling and enabling the state of a recognition context does not affect the state of the context's command and control grammar. To run this code, paste it into the Declarations section of a form that contains no controls. Note that the code requires the grammar file C:\sol.xml to run correctly.

Option Explicit

Private Sub Form_Load()

    Dim Grammar As ISpeechRecoGrammar
    Dim RecoContext As SpSharedRecoContext
    Dim Recognizer As SpSharedRecognizer
    Dim T As String

    On Error GoTo EH

    ' Set up speech recognition and grammar:
    Set Recognizer = New SpSharedRecognizer
    Set RecoContext = Recognizer.CreateRecoContext
    Set Grammar = RecoContext.CreateGrammar

    With Grammar

        ' Load command and control grammar
        ' and disable that grammar:
        .CmdLoadFromFile "C:\sol.xml", SLODynamic
        MsgBox "Grammar state before disabling: " & .State
        .State = SGSDisabled
        MsgBox "Grammar state after disabling: " & .State

        ' Disable and then reenable recognition context
        ' and demonstrate that grammar is still disabled:
        RecoContext.State = SRCS_Disabled
        RecoContext.State = SRCS_Enabled
        T = "Grammar state after disabling/reenabling "
        T = T & "the recognition context: " & .State
        MsgBox T, vbInformation

    End With


    If Err.Number Then ShowErrMsg
End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Dim T As String

    T = "Desc: " & Err.Description & vbNewLine
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"

End Sub