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".
after some research i learned that it can be done using user32.dll
and the process of achieving this task is as follows:
1) get the active window handle
2) get the active window thread handle
3) attach input to active thread
4) get focused control handle (+caret position but that is not the issue)
5) detach input from active thread
6) get the text from the focused control using its handle
and here is my code so far:
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();
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.
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:
Get text from inside google chrome using my c# app - Stack Overflow - Google
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.
i only referred to web browsers in the question's title because this tool will be mostly used with web browsers.
thank you in advance for any feedback.