Eminem
Eminem

Reputation: 7484

How to capture UIAutomation event

I need to monitor changes in the google chrome url textbox. I have the code the finds the url and correctly reads it.

     AutomationElement elementx = element.FindFirst(System.Windows.Automation.TreeScope.Descendants, conditions);
  return ((ValuePattern)elementx.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;

elementx is the url textbar and the url is returned

Now I need to monitor the url textbar for text changes. I found some code on stackoverflow but it doesnt work . Handling ProgressBar's Value change with UIAutomation

I have used an app called accevent.exe and see that it is indeed possible to monitor for text changes as is evident per the screenshot. So basically I need a way to monitor and report changes to the url text.

using accevent to monitor for changes

and here are some settings enter image description here

Upvotes: 2

Views: 2035

Answers (1)

Eminem
Eminem

Reputation: 7484

 ValuePattern chromeValuePattern;
  AutomationPropertyChangedEventHandler propChangeHandler = null;

  chromeValuePattern = elementx.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

                propChangeHandler += new AutomationPropertyChangedEventHandler(OnPropertyChange);
                Automation.AddAutomationPropertyChangedEventHandler(elementx,
                    System.Windows.Automation.TreeScope.Subtree, propChangeHandler,
                    AutomationProperty.LookupById(UIA_PropertyIds.UIA_ValueValuePropertyId));



   private void OnPropertyChange(object src, AutomationPropertyChangedEventArgs e)
        { 
            AutomationElement sourceElement = src as AutomationElement;

            if (e.Property == AutomationProperty.LookupById(UIA_PropertyIds.UIA_ValueValuePropertyId))
            {

                Debug.WriteLine(e.NewValue);

            }
            else
            {

            }
        }

Upvotes: 1

Related Questions