I am doing speech recognition system for windows arrow keys in c#. I have done it for mouse click events but now i want to do it for arrow keys. here is the code for the mouse click events.
public partial class Form1 : Form
{
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
Random rnd = new Random();
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
var button1 = new Button();
button1.Location = new Point(10, 10);
button1.Text = "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;
public const int MOUSEEVENTF_RIGHTDOWN = 0x02;
public const int MOUSEEVENTF_RIGHTUP = 0x04;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
SimulateLeftClick();
SimulateRightClick();
}
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 SimulateRightClick()
{
int xpos1 = Cursor.Position.X;
int ypos1 = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_RIGHTDOWN, xpos1, ypos1, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, xpos1, ypos1, 0, 0);
}
private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
int ranNum = rnd.Next(1, 10);
string speech = e.Result.Text;
switch (speech)
{
case "next":
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;
case "back":
int xpos1 = Cursor.Position.X;
int ypos1 = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_RIGHTDOWN, xpos1, ypos1, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, xpos1, ypos1, 0, 0);
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Left Click simulated");
}
void button1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("Right click");
}
}
private void Form1_Load(object sender, EventArgs e)
{
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Gayath\Documents\myAI\commands.txt")))));
_recognizer.LoadGrammar(new DictationGrammar());
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
when i say "up" up arrow key should work likewise other keys should be operated. what changes i should do in above codes.. can anyone give me a solution.