我在做什么:
我的主要目的是使用户友好text to speech
供个人在 Win 7 上使用。该方法应该适用于 Google Chrome、VS 和 Eclipse。
代码示例:
Following code creates global keyboard hook for ctrl
+ alt
+ space
, called hookEvent. If event fires, it starts/stops speaking clipboard contents ( that can be updated with ctrl
+ c
).
/// <summary>
/// KeyboardHook from: http://www.liensberger.it/web/blog/?p=207
/// </summary>
private readonly KeyboardHook hook = new KeyboardHook();
private readonly SpeechSynthesizer speaker = //
new SpeechSynthesizer { Rate = 3, Volume = 100 };
private void doSpeaking(string text)
{
// starts / stops speaking, while not blocking UI
if (speaker.State != SynthesizerState.Speaking)
speaker.SpeakAsync(text);
else
speaker.SpeakAsyncCancelAll();
}
private void hookEvent(object sender, KeyPressedEventArgs e)
{
this.doSpeaking(Convert.ToString(Clipboard.GetText()));
}
public Form1()
{
InitializeComponent();
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hookEvent);
hook.RegisterHotKey(ModifierKeysx.Control|ModifierKeysx.Alt, Keys.Space);
}
问题:
我宁愿不使用剪贴板。或者至少,恢复之后的值,例如:
[MethodImpl(MethodImplOptions.Synchronized)]
private string getSelectedTextHACK()
{
object restorePoint = Clipboard.GetData(DataFormats.UnicodeText);
SendKeys.SendWait("^c");
string result = Convert.ToString(Clipboard.GetText());
Clipboard.SetData(DataFormats.UnicodeText, restorePoint);
return result;
}
我有什么选择?
Edit:
令我惊讶的是,我发现我的剪贴板阅读器是最好的选择。我创建了一个通知区域应用程序,它响应左键单击(说出剪贴板)和右键单击(打开菜单)。在菜单中,用户可以改变速度、说话或创建音频文件。