基本来自网上,但网上版本太多,也不知道哪个是原创的
有所精简和改进- Const SLODynamic = 0
-
- Dim CommandDictionary '命令字典对象
- Dim WshShell 'WshShell对象提供对本地Windows程序的访问。
- Dim SR '语音识别(Speech Recognition)对象
- Dim Grammar '语音识别的命令语法对象
-
- '初始化命令字典对象,可根据自己的需要添加命令而来
- Set CommandDictionary = CreateObject("Scripting.Dictionary")
- CommandDictionary.Add "计算器", "calc"
- CommandDictionary.Add "记事本", "notepad"
- CommandDictionary.Add "测试", "notepad ceshi.vbs"
- CommandDictionary.Add "打开键盘", "osk"
-
- Set WshShell = CreateObject("WScript.Shell") '创建WshShell对象
-
- '创建语音识别对象,调用由"Command.XML"所定义的语法,并启动语音识别引擎
- Set SR = WScript.CreateObject("SAPI.SpSharedRecoContext", "RecoContext_")
- Set Grammar = SR.CreateGrammar
- Grammar.Reset
- Grammar.CmdLoadFromFile "COMMAND.XML", SLODynamic
- Grammar.CmdSetRuleIdState 0, 1
-
- Set objTTS = CreateObject( "SAPI.SpVoice" )
-
- MsgBox "语音识别系统测试"
- WScript.Sleep 100000000
-
- '你的语音命令被识别
- Sub RecoContext_Recognition(ByVal StreamNumber, ByVal StreamPosition, ByVal RecognitionType, ByVal Result )
- Text = Result.PhraseInfo.GetText '获取语音识别引擎所识别的命令
-
- WScript.Sleep 1000
-
- Set fso=CreateObject("scripting.filesystemobject").opentextfile("记录.log",2)
- fso.write Text & vbCrLf & "Text"
-
- If CommandDictionary.Exists(Text) Then
- objTTS.Speak "正在打开" & Text
- WshShell.Run CommandDictionary.Item(Text)
- End If
-
- If Text = "命令结束" Then
- objTTS.Speak "命令结束,再见!"
- WScript.Sleep 2000
- Set objTTS = Nothing
- WScript.Quit
- End If
- End Sub
复制代码 COMMAND.XML 的内容如下:- <?xml version="1.0" encoding="gb2312" ?><GRAMMAR LANGID="804"><RULE NAME="命令" TOPLEVEL="ACTIVE"><L>
-
- <P>计算器</P>
- <P>记事本</P>
- <P>打开键盘</P>
- <P>命令结束</P>
-
- </L></RULE></GRAMMAR>
复制代码
|