예제 #1
0
        /// <summary>
        /// main objRecoContext event
        /// launched when engine recognized a phrase
        /// </summary>
        /// <param name="e">contained information on the phrase that been recognized</param>
        public void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType,	ISpeechRecoResult e)
        {
            //calculate accuracy
            float accuracy=(float)e.PhraseInfo.Elements.Item(0).EngineConfidence;

            //change accuracyMax dynamicly
            if (accuracyMax<accuracy)
                accuracyMax=accuracy;

            if (accuracy<0)
                accuracy=0;

            accuracy=(int)((float)accuracy/accuracyMax*100);
            label2.Text="Accuracy "+accuracy.ToString()+ "%";

            //get phrase
            string phrase=e.PhraseInfo.GetText(0,-1,true);
            //make sure it's in lower case (for safer use only)
            phrase=phrase.ToLower();

            //if recognized any ...
            if (phrase!="" && accuracy>=accuracyLimit)
            {
                //Only if agent enabled
                if (menuItem14.Checked==true)
                {
                    agent1.StopAll("");
                    agent1.Speak(phrase,"");
                }

                switch (e.PhraseInfo.Rule.Name)		//rule name (not the phrase !)
                {
                    case "Activate":
                    {
                        //Only if agent enabled
                        if (menuItem14.Checked==true)
                        {
                            //show character
                            agent1.Show(false);
                        }

                        //load grammar
                        SAPIGrammarFromFile("XMLDeactivate.xml");

                        //notify user
                        label1.Text="Activate";

                        //Only if agent enabled
                        if (menuItem14.Checked==true)
                        {
                            //animate character
                            agent1.Play("StartListening");
                            agent1.Speak("I'm listening","");
                        }
                        break;
                    }
                    case "Deactivate":
                    {
                        //load grammar
                        SAPIGrammarFromFile("XMLActivate.xml");

                        //notify user
                        label1.Text="Deactivate";

                        //Only if agent enabled
                        if (menuItem14.Checked==true)
                        {
                            //animate character
                            agent1.Play("Wave");
                            agent1.Hide(false);
                        }
                        break;
                    }
                    case "Start":
                    {
                        keybd_event((byte)Keys.LWin,0,0,0);	//key down
                        keybd_event((byte)Keys.LWin,0,2,0);	//key up

                        //load grammar
                        SAPIGrammarFromFile("XMLStart.xml");

                        //notify user
                        label1.Text="Start";
                        break;
                    }
                    case "Right":
                    {
                        keybd_event((byte)Keys.Right,0,0,0);	//key down
                        keybd_event((byte)Keys.Right,0,2,0);	//key up
                        break;
                    }
                    case "Left":
                    {
                        keybd_event((byte)Keys.Left,0,0,0);	//key down
                        keybd_event((byte)Keys.Left,0,2,0);	//key up
                        break;
                    }
                    case "Up":
                    {
                        keybd_event((byte)Keys.Up,0,0,0);		//key down
                        keybd_event((byte)Keys.Up,0,2,0);		//key up
                        break;
                    }
                    case "Down":
                    {
                        keybd_event((byte)Keys.Down,0,0,0);	//key down
                        keybd_event((byte)Keys.Down,0,2,0);	//key up
                        break;
                    }
                    case "Enter":
                    {
                        keybd_event((byte)Keys.Enter,0,0,0);	//key down
                        keybd_event((byte)Keys.Enter,0,2,0);	//key up
                        break;
                    }
                    case "Escape":
                    {
                        keybd_event((byte)Keys.Escape,0,0,0);	//key down
                        keybd_event((byte)Keys.Escape,0,2,0);	//key up
                        keybd_event((byte)Keys.LMenu,0,2,0);	//key up

                        //load grammar (used to reset grammar in case it contains menu stuff ...)
                        SAPIGrammarFromFile("XMLDeactivate.xml");

                        //notify user
                        label1.Text="Activate";
                        break;
                    }
                    case "PureEscape":
                    {
                        keybd_event((byte)Keys.Escape,0,0,0);	//key down
                        keybd_event((byte)Keys.Escape,0,2,0);	//key up
                        break;
                    }
                    case "Alt":
                    {
                        keybd_event((byte)Keys.LMenu,0,0,0);	//key down
                        keybd_event((byte)Keys.LMenu,0,2,0);	//key up

                        //check if there is any menu and hook it
                        IntPtr hWnd=GetForegroundWindow();
                        IntPtr hMnu=GetMenu(hWnd);
                        int mnuCnt=GetMenuItemCount(hMnu);

                        if (mnuCnt!=0)
                        {
                            //Only if agent enabled
                            if (menuItem14.Checked==true)
                            {
                                //animate character
                                agent1.Play("DoMagic1");
                                agent1.Think("Hooking menu ...");
                            }

                            //add menu to grammar
                            hookMenu(hMnu);

                            //Only if agent enabled
                            if (menuItem14.Checked==true)
                            {
                                //animate character
                                agent1.Play("Idle1_1");
                            }
                        }
                        else
                        {
                            //load grammar
                            SAPIGrammarFromFile("XMLDeactivate.xml");

                            //notify user
                            label1.Text="Activate";
                        }
                        break;
                    }
                    case "Tab":
                    {
                        keybd_event((byte)Keys.Tab,0,0,0);	//key down
                        keybd_event((byte)Keys.Tab,0,2,0);	//key up
                        break;
                    }
                    case "ShiftTab":
                    {
                        keybd_event((byte)Keys.LShiftKey,0,0,0);	//key down
                        keybd_event((byte)Keys.Tab,0,0,0);		//key down
                        keybd_event((byte)Keys.Tab,0,2,0);		//key up
                        keybd_event((byte)Keys.LShiftKey,0,2,0);	//key up
                        break;
                    }
                    case "CloseProgram":
                    {
                        Close();
                        break;
                    }
                    case "ShowAbout":
                    {
                        if (frmAbout1==null)
                        {
                            //show frmAbout
                            frmAbout1=new frmAbout();
                            frmAbout1.Closed+=new EventHandler(frmAbout1_Closed);
                            //send user profile
                            frmAbout1.Tag=(string)objRecoContext.Recognizer.Profile.GetDescription(0);
                            frmAbout1.Show();
                        }

                        //load grammar
                        SAPIGrammarFromFile("XMLAbout.xml");

                        //notify user
                        label1.Text="About Speech Recognition";
                        break;
                    }
                    case "CloseAbout":
                    {
                        //close frmAbout
                        if (frmAbout1!=null)
                        {
                            frmAbout1.Close();
                            frmAbout1=null;
                        }
                        break;
                    }
                    case "ShowCommands":
                    {
                        if (frmCommands1==null)
                        {
                            //show frmAbout
                            frmCommands1=new frmCommands();
                            frmCommands1.Closed+=new EventHandler(frmCommands1_Closed);
                            //send grammar
                            frmCommands1.Tag=label1.Text;
                            frmCommands1.Show();
                        }

                        //load grammar
                        SAPIGrammarFromFile("XMLCommands.xml");
                        break;
                    }
                    case "CloseCommands":
                    {
                        //close frmCommands
                        if (frmCommands1!=null)
                        {
                            frmCommands1.Close();
                            frmCommands1=null;
                        }
                        break;
                    }
                    case "ShowFavorites":
                    {
                        if (frmFavorites1==null)
                        {
                            //show frmFavorites
                            frmFavorites1=new frmFavorites();
                            frmFavorites1.Closed+=new EventHandler(frmFavorites1_Closed);
                            //send file name
                            frmFavorites1.Tag=appPath+"XMLFavorites.xml";
                            frmFavorites1.Show();
                        }

                        //load grammar
                        SAPIGrammarFromFile("XMLFavorites.xml");

                        //notify user
                        label1.Text="Favorites";
                        break;
                    }
                    case "CloseFavorites":
                    {
                        //show frmAbout
                        if (frmFavorites1!=null)
                        {
                            frmFavorites1.Close();
                            frmFavorites1=null;
                        }
                        break;
                    }
                    case "CloseForm":
                    {
                        IntPtr hWnd=GetForegroundWindow();

                        //make sure we are not closing our program ...
                        if (hWnd!=this.Handle)
                        {
                            keybd_event((byte)Keys.LMenu,0,0,0);	//key down
                            keybd_event((byte)Keys.F4,0,0,0);		//key down
                            keybd_event((byte)Keys.LMenu,0,2,0);	//key up
                            keybd_event((byte)Keys.F4,0,2,0);		//key up
                        }
                        break;
                    }
                    case "Programs":
                    case "Documents":
                    case "Settings":
                    case "Search":
                    case "Help":
                    case "Run":
                    {
                        keybd_event((byte)(e.PhraseInfo.Rule.Name[0]),0,0,0);	//key down
                        keybd_event((byte)(e.PhraseInfo.Rule.Name[0]),0,2,0);	//key up

                        //load grammar
                        SAPIGrammarFromFile("XMLDeactivate.xml");

                        //notify user
                        label1.Text="Activate";
                        break;
                    }
                    case "RunProgram":
                    {
                        //show frmAbout
                        if (frmFavorites1!=null)
                        {
                            frmFavorites1.Close();
                            frmFavorites1=null;
                        }

                        try
                        {
                            System.Diagnostics.Process.Start(phrase);
                        }
                        catch
                        {
                            //Only if agent enabled
                            if (menuItem14.Checked==true)
                            {
                                agent1.Speak("Could not run : "+phrase,"");
                            }
                        }

                        //load grammar
                        SAPIGrammarFromFile("XMLDeactivate.xml");

                        //notify user
                        label1.Text="Activate";
                        break;
                    }
                    case "SwitchProgram":
                    {
                        keybd_event((byte)Keys.LMenu,0,0,0);	//key down
                        keybd_event((byte)Keys.Tab,0,0,0);	//key down
                        keybd_event((byte)Keys.Tab,0,2,0);	//key up

                        //load grammar
                        SAPIGrammarFromFile("XMLSwitchProgram.xml");

                        //notify user
                        label1.Text="Switch Program";
                        break;
                    }
                    case "SwitchEnter":
                    {
                        keybd_event((byte)Keys.LMenu,0,2,0);	//key up

                        //load grammar
                        SAPIGrammarFromFile("XMLDeactivate.xml");

                        //notify user
                        label1.Text="Activate";
                        break;
                    }

                    case "HoldKey":
                    {
                        //load grammar
                        SAPIGrammarFromFile("XMLStickyKeys.xml");

                        //notify user
                        label1.Text="Press key";
                        break;
                    }

                    case "ReleaseKey":
                    {
                        timer2.Enabled=false;

                        //load grammar
                        SAPIGrammarFromFile("XMLDeactivate.xml");

                        //notify user
                        label1.Text="Activate";
                        break;
                    }

                    case "HoldRight":
                    {
                        keyHolding=(byte)Keys.Right;
                        timer2.Enabled=true;
                        break;
                    }
                    case "HoldLeft":
                    {
                        keyHolding=(byte)Keys.Left;
                        timer2.Enabled=true;
                        break;
                    }
                    case "HoldUp":
                    {
                        keyHolding=(byte)Keys.Up;
                        timer2.Enabled=true;
                        break;
                    }
                    case "HoldDown":
                    {
                        keyHolding=(byte)Keys.Down;
                        timer2.Enabled=true;
                        break;
                    }
                    case "PageUp":
                    {
                        keybd_event((byte)Keys.PageUp,0,0,0);	//key down
                        keybd_event((byte)Keys.PageUp,0,2,0);	//key up
                        break;
                    }
                    case "Yes":
                    {
                        keybd_event((byte)Keys.Y,0,0,0);	//key down
                        keybd_event((byte)Keys.Y,0,2,0);	//key up
                        break;
                    }
                    case "No":
                    {
                        keybd_event((byte)Keys.N,0,0,0);	//key down
                        keybd_event((byte)Keys.N,0,2,0);	//key up
                        break;
                    }
                    case "BackSpace":
                    {
                        keybd_event((byte)Keys.Back,0,0,0);	//key down
                        keybd_event((byte)Keys.Back,0,2,0);	//key up
                        break;
                    }
                    case "ShutDown":
                    {
                        Shell32.ShellClass a=new Shell32.ShellClass();
                        a.ShutdownWindows();

                        //load grammar
                        SAPIGrammarFromFile("XMLShutDown.xml");

                        //notify user
                        label1.Text="Shut Down";
                        break;
                    }
                    case "ActivateWithoutAnimation":
                    {
                        //load grammar
                        SAPIGrammarFromFile("XMLDeactivate.xml");

                        //notify user
                        label1.Text="Activate";
                        break;
                    }
                    case "EnterNumericState":
                    {
                        //load grammar
                        SAPIGrammarFromFile("XMLNumericState.xml");

                        //notify user
                        label1.Text="Numeric State...";
                        break;
                    }
                    case "Zero":
                    case "One":
                    case "Two":
                    case "Three":
                    case "Four":
                    case "Five":
                    case "Six":
                    case "Seven":
                    case "Eight":
                    case "Nine":
                    {
                        byte k=(byte)e.PhraseInfo.GetText(0,-1,false)[0];

                        keybd_event((byte)(k+'0'),0,0,0);	//key down
                        keybd_event((byte)(k+'0'),0,2,0);	//key up
                        break;
                    }
                    case "Plus":
                    {
                        keybd_event((byte)Keys.Add,0,0,0);	//key down
                        keybd_event((byte)Keys.Add,0,2,0);	//key up
                        break;
                    }
                    case "Minus":
                    {
                        keybd_event((byte)Keys.Subtract,0,0,0);	//key down
                        keybd_event((byte)Keys.Subtract,0,2,0);	//key up
                        break;
                    }
                    case "Div":
                    {
                        keybd_event((byte)Keys.Divide,0,0,0);	//key down
                        keybd_event((byte)Keys.Divide,0,2,0);	//key up
                        break;
                    }
                    case "Mul":
                    {
                        keybd_event((byte)Keys.Multiply,0,0,0);	//key down
                        keybd_event((byte)Keys.Multiply,0,2,0);	//key up
                        break;
                    }
                    case "Equal":
                    {
                        keybd_event(187,0,0,0);	//key down
                        keybd_event(187,0,2,0);	//key up
                        break;
                    }
                    case "EnterAlphabeticState":
                    {
                        //load grammar
                        SAPIGrammarFromFile("XMLAlphabeticState.xml");

                        //notify user
                        label1.Text="Alphabetic State...";
                        break;
                    }
                    case "abcA":case "abcB":case "abcC":case "abcD":case "abcE":case "abcF":case "abcG":
                    case "abcH":case "abcI":case "abcJ":case "abcK":case "abcL":case "abcM":case "abcN":
                    case "abcO":case "abcP":case "abcQ":case "abcR":case "abcS":case "abcT":case "abcU":
                    case "abcV":case "abcW":case "abcX":case "abcY":case "abcZ":
                    {
                        firstRecognition=phrase;
                        string str1=phrase;
                        str1=str1.ToUpper();
                        keybd_event((byte)(str1[0]),0,0,0);	//key down
                        keybd_event((byte)(str1[0]),0,2,0);	//key up
                        break;
                    }
                    case "At":
                    {
                        keybd_event((byte)Keys.LShiftKey,0,0,0);	//key down
                        keybd_event((byte)Keys.D2,0,0,0);			//key down
                        keybd_event((byte)Keys.D2,0,2,0);			//key up
                        keybd_event((byte)Keys.LShiftKey,0,2,0);	//key up
                        break;
                    }
                    case "UnderLine":
                    {
                        keybd_event((byte)Keys.LShiftKey,0,0,0);	//key down
                        keybd_event((byte)Keys.OemMinus,0,0,0);		//key down
                        keybd_event((byte)Keys.OemMinus,0,2,0);		//key up
                        keybd_event((byte)Keys.LShiftKey,0,2,0);	//key up
                        break;
                    }
                    case "Dash":
                    {
                        keybd_event((byte)Keys.Subtract,0,0,0);		//key down
                        keybd_event((byte)Keys.Subtract,0,2,0);		//key up
                        break;
                    }
                    case "Dot":
                    {
                        keybd_event(190,0,0,0);	//key down
                        keybd_event(190,0,2,0);	//key up
                        break;
                    }
                    case "BackSlash":
                    {
                        keybd_event((byte)Keys.Divide,0,0,0);	//key down
                        keybd_event((byte)Keys.Divide,0,2,0);	//key up
                        break;
                    }
                    case "AlphabeticStateNo":
                    {
                        //delete the first letter
                        keybd_event((byte)Keys.Back,0,0,0);	//key down
                        keybd_event((byte)Keys.Back,0,2,0);	//key up

                        //write the replacement letter
                        string str1=firstRecognition;

                        //fix miss recognition
                        switch(firstRecognition)
                        {
                            case "a": str1="h"; break;
                            case "b": str1="d"; break;
                            case "c": str1="t"; break;
                            case "d": str1="p"; break;
                            case "f": str1="x"; break;
                            case "h": str1="f"; break;
                            case "m": str1="n"; break;
                            case "n": str1="l"; break;
                            case "l": str1="m"; break;
                            case "p": str1="v"; break;
                            case "u": str1="q"; break;
                            case "v": str1="t"; break;
                            case "e": str1="b"; break;
                            case "j": str1="k"; break;
                        }

                        firstRecognition=str1;
                        str1=str1.ToUpper();

                        keybd_event((byte)(str1[0]),0,0,0);	//key down
                        keybd_event((byte)(str1[0]),0,2,0);	//key up
                        break;
                    }

                    //else press the key (probably a menu ...)
                    default:
                    {
                        string str1=e.PhraseInfo.Rule.Name;
                        str1=str1.ToUpper();

                        keybd_event((byte)(str1[0]),0,0,0);	//key down
                        keybd_event((byte)(str1[0]),0,2,0);	//key up

                        //could be submenu (hook it)
                        hookSubmenu(e.PhraseInfo.Rule.Name[0].ToString());
                        break;
                    }
                }
            }

            //if not recognized ...
            else
            {
                //Only if agent enabled
                if (menuItem14.Checked==true)
                {
                    //animate character
                    agent1.Play("Decline");
                }
            }
        }