Hello Today I will show you how Control Your Mouse with Speech Recognition.
First Create a Project Like My Previous tutorial C# -How To Convert Text To Speech.
Add A textbox and Set Properties Multiline true .... Now Write the following Code and fun with it . When you speak left,right,up,down the mouse move .Screenshot .
Run the program and find the mouse point .. in screenshot i can't find it :)
First Create a Project Like My Previous tutorial C# -How To Convert Text To Speech.
Add A textbox and Set Properties Multiline true .... Now Write the following Code and fun with it . When you speak left,right,up,down the mouse move .Screenshot .
Run the program and find the mouse point .. in screenshot i can't find it :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
namespace TextToSpeech
{
public partial class Form1 : Form
{
SpeechSynthesizer Speech = new SpeechSynthesizer();
SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
}
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;
}
txtText.Text += line;
txtText.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);
}
private void Form1_Load(object sender, EventArgs e)
{
Initialize();
}
}
}
Thanks ...Happy coding.
No comments:
Post a Comment