title: How to get selected text from an active application, without using a clipboard
body: <h2>What am I doing:</h2>
  
  <p>My main intent is to enable user friendly <code>text to speech</code> for personal use on Win 7. Approach should work in Google Chrome, VS and Eclipse. </p>
  
  <h2>Code example:</h2>
  
  <p>Following code creates global keyboard hook for <kbd><code>ctrl</code></kbd> + <kbd><code>alt</code></kbd> + <kbd><code>space</code></kbd>, called hookEvent. If event fires, it starts/stops speaking clipboard contents ( that can be updated with <kbd><code>ctrl</code></kbd> + <kbd><code>c</code></kbd> ).</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// KeyboardHook from: http://www.liensberger.it/web/blog/?p=207
      /// &lt;/summary&gt;
      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&lt;KeyPressedEventArgs&gt;(hookEvent);
          hook.RegisterHotKey(ModifierKeysx.Control|ModifierKeysx.Alt, Keys.Space);
      }
  </code></pre>
  
  <h2>Question:</h2>
  
  <p>I would prefer not using the clipboard. Or at least, restoring the value after, something like:</p>
  
  <pre><code>    [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;
      }
  </code></pre>
  
  <p>What are my options?</p>
  
  <p>Edit:</p>
  
  <p>To my surprise, I found that my clipboard reader is the best way to go. I created a notification area app, that responds to left click (speaking clipboard) and right click (menu opens up). In menu the user can chance speed, speak or create a audio file.</p>