I am writing a c# code to control mouse with speech recognition. The code is showing bellow.
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace my
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
string line1 = "";
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.5f)
line1 += word.Text + " ";
}
string command1 = line1.Trim();
System.Console.WriteLine(command1);
if (command1 == "start")
{
System.Console.WriteLine("!!!!TEST-1!!!!");
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (c, arg) =>
{
string line = "";
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.5f)
line += word.Text + " ";
}
string command = line.Trim();
System.Console.WriteLine(command);
System.Console.WriteLine("!!!!TEST-2!!!!");
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);
}
else
{
System.Console.WriteLine("!!!!TEST-3!!!!");
}
txtOutput.Text += line1;
txtOutput.Text += Environment.NewLine;
};
recognitionEngine.UnloadAllGrammars();
recognitionEngine.LoadGrammar(CreateGrammars());
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
private Grammar CreateGrammars()
{
Choices commandChoices = new Choices("left", "right", "up", "down","start");
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);
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
When user run the code, user can say the commands “up”, “down”, “left”, “right” and the mouse pointer will jump respectively in increments of 50. But first i want when user say "start" run the program and second when user say the commands “up”, “down”, “left”, “right” and the mouse pointer will jump respectively in increments of 50. When user say "stop" then stop the program. In here I said as "start" string command1 get is as "start" and it goes into System.Console.WriteLine("!!!!TEST-1!!!!"); this command. But secondly when i said as "up" string command get it as "start" not "up".So never go to the switch statement. If u can run this code u can understand the problem.I tried to solve this problem since a two week.But i still couldn't slove.If u can solve this problem it will be big help to me. Because i want to finish this project nearly 1 month .Thank u