title: Get text from inside google chrome using my c# app
body: <p>I am writing a small app that will among other things expand shortcuts into full text while typing.
  example:
  the user writes "BNN" somewhere and presses the relevant keyboard combination, the app would replace the "BNN" with a "Hi I am Banana".</p>
  
  <p>after some research i learned that it can be done using <code>user32.dll</code> and the process of achieving this task is as follows:</p>
  
  <p>1) get the active window handle <br/>
  2) get the active window thread handle <br/>
  3) attach input to active thread <br/>
  4) get focused control handle (+caret position but that is not the issue) <br/>
  5) detach input from active thread <br/>
  6) get the text from the focused control using its handle <br/></p>
  
  <p>and here is my code so far:</p>
  
  <pre><code>try
  {
      IntPtr activeWindowHandle = GetForegroundWindow();
      IntPtr activeWindowThread = GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero);
      IntPtr thisWindowThread = GetWindowThreadProcessId(this.Handle, IntPtr.Zero);
      AttachThreadInput(activeWindowThread, thisWindowThread, true);
      IntPtr focusedControlHandle = GetFocus();
  
      AttachThreadInput(activeWindowThread, thisWindowThread, false);
      if (focusedControlHandle != IntPtr.Zero)
      {
          TB_Output.Text += focusedControlHandle + " , " + GetText(focusedControlHandle) + Environment.NewLine;
      }
  }
  catch (Exception exp)
  {
      MessageBox.Show(exp.Message);
  }
  
  //...
  //...
  
  [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  internal static extern IntPtr GetForegroundWindow();
  
  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  internal static extern int GetWindowThreadProcessId(int handle, out int processId);
  
  [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  internal static extern int AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach);
  
  [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  internal static extern IntPtr GetFocus();
  </code></pre>
  
  <p>this works perfectly for some windows forms apps but it doesnt work with WPF nor browsers, just gives me the title of the WPF app or the title of the tab in chrome.</p>
  
  <p>if i run the app on this page while typing this question for instance, instead of the content of the question, the text i get is:</p>
  
  <blockquote>
    <p>Get text from inside google chrome using my c# app - Stack Overflow - Google </p>
  </blockquote>
  
  <p>probably because they use graphics to render the elements, and im not sure how i can get to the active element and read it's text.</p>
  
  <p>i only referred to web browsers in the question's title because this tool will be mostly used with web browsers.</p>
  
  <p>thank you in advance for any feedback.</p>