2

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

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

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

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);
    }
}
}
4

1 回答 1

2

将以下代码粘贴到 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");
}

您可以在此处查找所有左、右、中间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 种语言。最近,一名澳大利亚高中学生因四肢瘫痪声控乐高风暴轮椅项目而获奖。http://www.yayalu.net/Yaya-Lu-2012/Yaya-Lu-2012.htm - 她使用基本的单词组合,例如:ma-mi、mi-ma,所以它的语言是中性的。

于 2013-06-09T05:16:25.880 回答