I am using Windows.Automation framework to detect control having keyboard focus. So far I have tried the following :
private void M_GlobalHook_KeyDown(object sender, KeyEventArgs e)
{
var focusedElement = AutomationElement.FocusedElement;
Console.WriteLine("Name : " + focusedElement.Current.Name);
Console.WriteLine("AccessKey : " + focusedElement.Current.AccessKey);
Console.WriteLine("ClassName : " + focusedElement.Current.ClassName);
Console.WriteLine("IsControlElement : " + focusedElement.Current.IsControlElement);
Console.WriteLine("ItemType : " + focusedElement.Current.ItemType);
Console.WriteLine("IsPassword : " + focusedElement.Current.IsPassword);
}
I am getting incorrect control intermittently. It is fetching parent control for the target application (TrGUI.exe), instead of the textbox when I start typing immediately after launching.
I also tried to get the keyboard caret coordinates, so that I can use AutomationElement.FromPoint to get the desired control. But, I was not able to get those caret coordinates correctly.