Partager via


New WPF Features: SynchronizedInputPattern

 This is part of a series on New WPF Features  

In previous releases, sending input through automation was tricky mainly because the app and the automation processes were separate. The app state could change between the time the input was sent and received. As an example, suppose you need to click something but before the click happens, the elements move (maybe due to resize …). In this case, some other element could get the mouse input.

To overcome this problem, we have introduced the SynchronizedInputPattern. Now the framework sees where the input is going and if its not the intended target, the input is cancelled. The sender is notified whether the input operation was successful or not and based on it can make a decision to try again.

So coming to the API, to determine if an element supports the pattern we get the property value

buttonElement.GetCurrentPropertyValue(AutomationElement.IsSynchronizedInputPatternAvailableProperty);

Next, you need to hook the events to the element so that we are notified of the operations success

InputReachedTargetEvent

Identifies the event raised when the input was received by the element currently listening for the input.

InputReachedOtherElementEvent

Identifies the event raised when the input was received by an element other than the one currently listening for the input.

InputDiscardedEvent

Identifies the event raised when the input was discarded by WPF.

 

Once the events are hooked, you can start listening to the specific SynchronizedInputType

    SynchronizedInputType.KeyUp,

    SynchronizedInputType.KeyDown,

    SynchronizedInputType.LeftMouseUp,

    SynchronizedInputType.LeftMouseDown,

    SynchronizedInputType.RightMouseUp,

    SynchronizedInputType.RightMouseDown

syncInputPattern.StartListening(SynchronizedInputType.MouseLeftButtonDown);

One thing to note is that the event fires only once. So if you need to send the input again, you will need to call the StartListening API again.

You can cancel listening by calling the Cancel() API

Sample app which shows the usage is attached. The app clicks on different controls simulating incorrect input received. The console output describes the operations being performed.

  

******Goal is to Click the Button******

 -- Click on the List

  ---- Input was Discarded - Try Again clicking On TextBox nested in Button

 -- Click on the Textbox inside Button

 ---- Input Reached Element other than Target - Try Again clicking on Button

 -- Click on the Button

 ---- SUCCESS: Input Reached Target

Share this post

 

UIA.zip