C# UI Automation Focus Event not working with notepad++

Viewed 184

I have created a simple UI Automation Program that reports if the focus in a program changes. For example if I have multiple files open in VS 2019 and I click on the tabs to view the different files the program reports that the focus is changed. However if I try the same thing with Notepad++, having multiple files open and clicking the tabs to view the different files it does not report that the focus has changed.

using System.Windows.Automation;
using System.Diagnostics;

namespace FocusChanged
{
 class Program
 {
     static void Main(string[] args)
     {
         Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler);
         Console.WriteLine("Monitoring... Hit enter to end.");
         Console.ReadLine();
     }

     private static void OnFocusChangedHandler(object src, AutomationFocusChangedEventArgs args)
     {
         Console.WriteLine("Focus changed!");
         AutomationElement element = src as AutomationElement;
         if (element != null)
         {
             string name = element.Current.Name;
             string id = element.Current.AutomationId;
             int processId = element.Current.ProcessId;
             using (Process process = Process.GetProcessById(processId))
             {
                 Console.WriteLine("  Name: {0}, Id: {1}, Process: {2}", name, id, process.ProcessName);
             }
         }
     }
 }
}
0 Answers
Related