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 に答える