开发者

Playing the key value on keydown in C#

I'm quite new to C# and am working on developing a software for blind and visually impaired people.

in 开发者_StackOverflowthe software I want to have a text box that when a key is pressed it will make a

sound for instance when pressing j it will say j... I found the wonderful text to speech engine and tried using it with the keydown event as follows:

   SpeechSynthesizer synth = new SpeechSynthesizer();            
   char mappedChar = (char)e.KeyValue;       
   synth.Speak(Convert.ToString(mappedChar));
   synth.Dispose();

unfortunately it is way too slow and takes approx. 1 second between each key stroke.

Would appreciate any suggestion.


I'd start by not creating and disposing of your SpeechSynthesizer object in the event handler.

Create the object once when the program runs and just have:

char mappedChar = (char)e.KeyValue;       
synth.Speak(mappedChar.ToString());

in your event handler.


I suspect that the constructor of speechsynthesizer is taking most of the time. Try creating the synth object once and caching it, rather than creating it on each call.


Creating a new SpeechSynthesizer instance is expensive.

Define your synth object as a member of the form, instantiate it that scope, then just refer to it in your event code. e.g. in pseudo code...

class MyForm
{
  SpeechSynthesizer synth = new SpeechSynthesizer(); 
  ...

  void On_Click(<params>)
  {
     this.synth.Speak(<text>);
  }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜