我想创建一个应用程序,使用语音识别控制鼠标位置及其左右点击。
我已经构建了一个可以控制鼠标指针的应用程序,但我仍然坚持使用语音模拟“左右”点击的代码。
这是我到目前为止的代码:
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);
}
}
}
答案 0 :(得分: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,所以它的语言中立。