title: Get text and caret from a textbox in another application
body: <p><strong>Background information</strong></p>
  
  <p>I am working on a Windows Speech-To-Text application.</p>
  
  <p>When speech has been recognized, the text should be inserted into the textbox that currently has keyboard focus (Think Word/Firefox/other applications). 
  To insert the text I am currently using <a href="https://github.com/TChatzigiannakis/InputSimulatorPlus" rel="nofollow noreferrer">InputSimulatorPlus</a>. </p>
  
  <p>When inserting the text, it is important the recognized text is formatted to fit the surrounding text, like:</p>
  
  <ul>
  <li>uppercase letter after punctuation</li>
  <li>lowercase first letter when it is NOT after punctuation</li>
  <li>uppercase first letter on a new line</li>
  <li>space after punctuation</li>
  </ul>
  
  <p>And so on</p>
  
  <p><strong>Problem</strong></p>
  
  <p>To be able to format the text I need <strong><em>the text</em></strong>, and <strong><em>the caret position</em></strong>. </p>
  
  <p>Currently I have been using <a href="https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview" rel="nofollow noreferrer">UI Automation</a> <a href="https://github.com/TestStack/UIAComWrapper" rel="nofollow noreferrer">NuGet Package</a> with <a href="https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-textpattern-overview" rel="nofollow noreferrer">Text Pattern</a>.
  This works well for all textboxes that support Text Pattern, but alot of programs do not support Text Pattern.</p>
  
  <p><strong>Strategy question:</strong> Should I use another approach than UI Automation?</p>
  
  <p>I have noticed that a lot of the applications I want to support do not support Text Pattern, but do support Value Pattern or Legacy IAccessible Pattern(<a href="https://docs.microsoft.com/en-us/windows/desktop/winauto/microsoft-active-accessibility" rel="nofollow noreferrer">Microsoft Active Accessibility</a>). </p>
  
  <p>I have been looking into using the <a href="https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/implementing-the-ui-automation-value-control-pattern" rel="nofollow noreferrer">Value Pattern</a> and can get the text of the textbox, but <em>not</em> the caret position. </p>
  
  <pre><code>using System.Windows.Automation;
  using System.Windows.Automation.Text;
  ...
  
  AutomationElement automationElement = AutomationElement.FocusedElement;
  var elements = automationElement.FindAll(TreeScope.Element,
      new AndCondition(
          new PropertyCondition(AutomationElement.HasKeyboardFocusProperty, true),
          new PropertyCondition(AutomationElement.IsValuePatternAvailableProperty, true)));
  foreach (AutomationElement element in elements)
  {
      if (element.GetCurrentPattern(ValuePattern.Pattern) is ValuePattern valuePattern)
      {
          var text = valuePattern.Current.Value;
          var caret = ? //How to get caret position?
          Console.WriteLine($"Caret: {caret}, Text: {text}");
          return (text,caret);
      }
  }
  </code></pre>
  
  <p>I have also been looking into using the <a href="https://docs.microsoft.com/en-us/windows/desktop/winauto/uiauto-implementinglegacyiaccessible" rel="nofollow noreferrer">Legacy IAccessible Pattern</a> and can get the text of the textbox, but <em>not</em> the caret position.</p>
  
  <pre><code>using System.Windows.Automation;
  using System.Windows.Automation.Text;
  ...
  
  AutomationElement automationElement = AutomationElement.FocusedElement;
  var elements = automationElement.FindAll(TreeScope.Element,
      new AndCondition(
          new PropertyCondition(AutomationElement.HasKeyboardFocusProperty, true),
          new PropertyCondition(AutomationElement.IsLegacyIAccessiblePatternAvailableProperty, true)));
  foreach (AutomationElement element in elements)
  {
      if (element.GetCurrentPattern(LegacyIAccessiblePattern.Pattern) is LegacyIAccessiblePattern legazyAccessiblePattern)
      {
          var text = legazyAccessiblePattern.Current.Value;
          var caret = ? //How to get caret position?
          Console.WriteLine($"Caret: {caret}, Text: {text}");
          return (text,caret);
      }
  }
  </code></pre>
  
  <p><strong>Main question:</strong> How do I get the caret position of a given textbox using UI Automation?</p>
  
  <p>P.S. I know this will never work for all applications, but if it can work for most normal applications, that would be awesome. </p>