Skip to content

Instantly share code, notes, and snippets.

@margusmartsepp
Created February 11, 2016 21:05
Show Gist options
  • Save margusmartsepp/755e1f5322d0343d438f to your computer and use it in GitHub Desktop.
Save margusmartsepp/755e1f5322d0343d438f to your computer and use it in GitHub Desktop.
SpeechSynthesizer with global hookups
using System;
using System.Speech.Synthesis;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
namespace SoundSynthesize
{
internal class Program
{
public static SpeechSynthesizer Talk;
// Install-Package MouseKeyHook
[STAThread]
public static void Main()
{
using (var hook = Hook.GlobalEvents())
using (Talk = new SpeechSynthesizer())
{
hook.MouseDownExt += MouseEvent;
Talk.Rate = 5;
while (!Console.KeyAvailable)
Application.DoEvents();
}
}
private static void MouseEvent(object sender, MouseEventExtArgs e)
{
if (e.Button == MouseButtons.XButton2)
{
SendKeys.SendWait("^{c}");
var text = Clipboard.GetText(TextDataFormat.Text);
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] Play: \n{text}");
Talk.SpeakAsync(text);
}
if (e.Button == MouseButtons.XButton1)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] Stop");
Talk.SpeakAsyncCancelAll();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment