当我说" space"它应该继续插入空间,直到我说"停止"。所以这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = true;
button1.Enabled = false;
Choices sList = new Choices();
sList.Add(new string[] { "space", "stop" });
Grammar gr = new Grammar(new GrammarBuilder(sList));
try
{
sRecognize.RequestRecognizerUpdate();
sRecognize.LoadGrammar(gr);
sRecognize.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sRecognize_SpeechRecognized);
sRecognize.SetInputToDefaultAudioDevice();
sRecognize.RecognizeAsync(RecognizeMode.Multiple);
}
catch { return; }
}
void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
textBox1.Text = e.Result.Text.ToString();
//throw new NotImplementedException();
if (textBox1.Text == "space")
//while (!e.Result.Text.ToString().Equals("stop"))
{
SendKeys.Send(" ");
}
}
由于我只是初学者,所以如果可能的话请帮助我理解您的代码和评论。
答案 0 :(得分:0)
创建一个调用Sendkeys.Send(" ")
的计时器事件,然后在识别'空格'时启动计时器,并在识别'停止'时停止计时器。
using System.Threading;
Timer thetimer;
void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text.ToString() == "space")
{
theTimer = new Timer(TimerCallback(timeExpired), 500); // 500 ms
}
else // if is stop
{
theTimer.Change(Timeout.Infinite, Timeout.Infinite); // stop the timer
}
}
void timeExpired(Object stateInfo)
{
SendKeys.Send(" ");
}
您也可以使用其他计时器机制,但这非常简单。