gpt4 book ai didi

c# - 通过语音识别控制鼠标

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:59 25 4
gpt4 key购买 nike

我想创建一个使用语音识别控制鼠标位置及其左右点击的应用程序。

我已经构建了一个可以控制鼠标指针的应用程序但是我被困在代码上以通过语音模拟“左右”点击。

这是我目前的代码:

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);
}
}
}

最佳答案

将以下代码粘贴到 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种语言。最近,一名澳大利亚高中学生因四肢瘫痪的声控乐高 Storm 轮椅项目获奖。 http://www.yayalu.net/Yaya-Lu-2012/Yaya-Lu-2012.htm - 她使用基本的单词组合,例如:ma-mi、mi-ma,所以它的语言是中性的。

关于c# - 通过语音识别控制鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17006053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com