0

I've been playing quakelive.com and have been getting frustrated by my keyboard bindings and want to bind using voice commands instead.

I thought I'd create a c# console app to run in the background and use the inbuilt speech recognition engine of SAPI for windows 7 64 bit to do all the heavy speech stuff. My program would listen for SpeechRecognized events and respond accordingly. However I'm not sure how to run my console app in the background in-conjuction with ms speech recognition whilst I'm playing the game?

This is what I have written so far:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using AutoItX3Lib;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            AutoItX3 autoit = new AutoItX3();

            // Create a default dictation grammar.
            DictationGrammar defaultDictationGrammar = new DictationGrammar();
            defaultDictationGrammar.Name = "default dictation";
            defaultDictationGrammar.Enabled = true;

            // Create our process
            autoit.Run("notepad.exe", "", autoit.SW_MAXIMIZE);
            autoit.WinWaitActive("Unbenannt - Editor");
            Console.WriteLine("its active");

            SpeechRecognizer sr = new SpeechRecognizer();
            sr.SpeechRecognized += (s, e) =>
            {
                foreach (RecognizedWordUnit word in e.Result.Words)
                {
                    Console.WriteLine(word.Text);
                    if (word.Text.Trim().ToLower() == "one")
                        autoit.Send(word.Text.ToLower() + "{LCTRL}+{LSHIFT}+a", 0);
                    else
                        autoit.Send(word.Text.ToLower() + " ", 0);
                }
            };
            sr.LoadGrammar(defaultDictationGrammar);
        }
    }
}

Basically I'd like ms speech recognition to be running while my game is running and for my console app to listen for specific words I say. As you can see in my example code, I am listening for the phrase "one" to which I send to notepad using autoIt the text as well as some control characters to select all the text when it's been written.

So far it's not working. It seems like my console app has to have "focus" or be the foreground app and even then when I say words like "one" or "two" ms speech recognition tries to do "console comand" stuff with my app rather than just pass dictation text to it. For example when I say the word "one" it keeps saying "moving" because I think it thinks that console aren't documents so it must be a command and not a dictation.

Can anyone see what I am doing wrong and how to get this working as I want?

The final solution was to send control characters to the running "chrome.exe" process rather than "notepad" because quakelive is run in the browser. So I presume sending keyboard commands via autoIt would be enough for the chrome process to then pass those onto the quakelive plugin as game keyboard game inputs (i.e. keyboard input/keystrokes).

Anyone with any help or advice appreciated.

4

1 回答 1

0

创建 SpeechRecognizer 时,您正在创建一个使用 Windows 桌面识别的共享识别器。当你说'(它)试图做“控制台命令”的东西'时,我怀疑这是因为你正在使用用于从桌面控制应用程序的共享识别器。如果您想使用专用于您的应用程序的语音,请改为创建 SpeechRecognitionEngine。共享的 recongizer 可能适用于您想要的,但我认为您需要有一个专用的语法才能正确控制您的应用程序。

由于您为用户提供了一组有限的语音命令,因此如果您提供支持此词汇表的语法而不是使用听写语法,您将获得更好的成功。

我不知道控制台应用程序是否需要在前台捕获声卡。我怀疑一旦您更改为 inproc 识别器,即使在后台,该应用程序也将继续运行。

有关更多背景信息,请参阅http://msdn.microsoft.com/en-us/magazine/cc163663.aspx。这可能是迄今为止我发现的最好的介绍性文章。它有点过时了,但很有帮助。(AppendResultKeyValue 方法在 beta 之后被删除。)http://msdn.microsoft.com/en-us/library/hh361625.aspx是一个很好的起点。

于 2012-10-12T15:09:46.600 回答