title: Using c# to get selected text from other applications
body: <p>Iam working on a project that need to get selected text from other application anytime user selects some text.
  <br><br>I have used <a href="http://globalmousekeyhook.codeplex.com/" rel="nofollow noreferrer">globalmousekeyhook</a> to monitor when user clicks on an application and i have not problem with that.<br><br>My problem is with getting selected text, As i know there are two ways to get selected text from other Applications:<br><br>
  1-Sending copy command(<a href="https://stackoverflow.com/questions/21460943/how-to-get-selected-text-of-any-application-into-a-windows-form-application/21501291#21501291">like this</a>):<br>
  This way works on clipboard and causes some problems with some applications like Adobe reader (<a href="https://forums.adobe.com/thread/441370" rel="nofollow noreferrer">problem</a>),IDM (<a href="https://www.internetdownloadmanager.com/support/clipboard_links.html" rel="nofollow noreferrer">clipboard links</a>) And other applications that also work on clipboard.
  <br><br>
  2-Using UI Automation:<br>As i have tried and seen that this way cant be used for <a href="https://stackoverflow.com/questions/4243944/how-to-get-selected-text-from-any-window-using-ui-automation-c-sharp">get selected text from ANY window (using UI Automation)</a><br><br>I also edited <a href="https://stackoverflow.com/questions/517694/ui-automation-selected-text?answertab=votes#tab-top">this</a> code like bellow but it still not working and slows down my computer<br></p>
  
  <pre><code>Process p = Process.GetCurrentProcess();
  
  AutomationElement ae = AutomationElement.FromHandle(p.MainWindowHandle);
  
  AutomationElement npEdit = ae.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"));
  
  TextPattern tp = npEdit.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
  
  TextPatternRange[] trs;
  
  if (tp.SupportedTextSelection == SupportedTextSelection.None)
  {
        return;
  }
  else
  {
        trs = tp.GetSelection();
        label1.Text = trs[0].GetText(-1);
  }
  </code></pre>
  
  <p><br>Isn't there another way to get selected text from other applications?
  <br>Or second way can't be used to perform the work?
  <br><br>Thanks for any help...</p>