c# - 通过语音识别控制鼠标

标签 c# speech-recognition speech speech-synthesis

我想创建一个应用程序,使用语音识别来控制鼠标位置及其左右单击。

我已经构建了一个可以控制鼠标指针的应用程序但我被困在通过语音模拟“左和右”点击的代码上。

这是我到目前为止的代码:

private void Initialize()
    {
        recognitionEngine = new SpeechRecognitionEngine();
        recognitionEngine.SetInputToDefaultAudioDevice();
        recognitionEngine.SpeechRecognized += (s, args) =>
        {
            string line = "";
            foreach (RecognizedWordUnit word in args.Result.Words)
            {
                if (word.Confidence > 0.5f)
                    line += word.Text + " ";
            }
            string command = line.Trim();
            switch (command)
            {
                case "left":
                    MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
                    break;
                case "right":
                    MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
                    break;
                case "up":
                    MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
                    break;
                case "down":
                    MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
                    break;
            }

            txtOutput.Text += line;
            txtOutput.Text += Environment.NewLine;
        };

        recognitionEngine.UnloadAllGrammars();
        recognitionEngine.LoadGrammar(CreateGrammars());
        recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
    }

    private Grammar CreateGrammars()
    {
        Choices commandChoices = new Choices("left", "right", "up", "down");
        GrammarBuilder grammarBuilder = new GrammarBuilder();
        grammarBuilder.Append(commandChoices);
        return new Grammar(grammarBuilder);
    }

    private void MoveMouse(int x, int y)
    {
        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(x, y);
        Cursor.Clip = new Rectangle(this.Location, this.Size);
    }
}
}

最佳答案

将以下代码粘贴到 Winforms 项目中并运行该项目:

public Form1()
{
    InitializeComponent();
    this.KeyPreview = true;
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

    var button1 = new Button();
    button1.Location = new Point(50,50);
    button1.Text = "Hover mouse over and press a key to simulate mouse click";
    button1.AutoSize = true;
    button1.Click +=new EventHandler(button1_Click);
    this.Controls.Add(button1);
}

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    SimulateLeftClick();
}

private void SimulateLeftClick()
{
    int xpos = Cursor.Position.X;
    int ypos = Cursor.Position.Y;
    mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Left Click simulated");
}

您可以在此处查找所有 Left、Right、Middle int 值:http://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx

将其放入您的解决方案中:

switch (command)
{
    case "leftclick":
        int xpos = Cursor.Position.X;
        int ypos = Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
        break;

还要考虑一下如何针对多语言应用程序执行此操作。世界上有超过 2500 种语言。最近,一名澳大利亚高中生因四肢瘫痪的声控乐高 Storm 轮椅项目获奖。 http://www.yayalu.net/Yaya-Lu-2012/Yaya-Lu-2012.htm - 她使用基本的单词组合,例如:ma-mi、mi-ma,因此语言中性。

关于c# - 通过语音识别控制鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17006053/

相关文章:

speech-recognition - MS Speech Platform 11 Recognizer 是否支持 ARPA 编译语法?

java - Android SpeechRecognizer设置识别引擎?

C#语音一起识别多个单词? (认识一个句子)

c# - 如何将多态对象作为派生对象传递?

java - 如何在 Android 应用程序的语音识别中传递语言?

java - 创建一个存储语音的按钮

audio - 如何使AIML机器人发出声音响应?有没有办法使这种声音成为自定义声音?

c# - 直接单击事件触发之前的表单验证

c# - 读取嵌入的文本文件

c# - 为什么我的对象不需要 ServiceKnownType?