3

I try to create test tool application use C# UIAutomation library. The example code is:

Automation.AddAutomationFocusChangedEventHandler(
   new AutomationFocusChangedEventHandler(OnAutomationFocusChanged));

When I detect focus changed, I need to get type of event(left/right click or keyboard pressed). Is it possible to do this an How can I find these value in method OnAutomationFocusChanged()?

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122

2 Answers2

2

Refer Following code:

public static void RightClick(this AutomationElement element)
    {
     var ev = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Right);
     ev.RoutedEvent = Mouse.MouseDownEvent;
     this.OnMouseDown(ev);
    }

Also refer this:

Move mouse with c#

Hope its helpful.

Community
  • 1
  • 1
Freelancer
  • 9,008
  • 7
  • 42
  • 81
2

No, this is not possible with UI automation. Low-level interactions like mouse and keyboard events are not part of the API. The API is designed around higher-level interactions like Invoke in the InvokePattern or Select in the SelectionItemPattern. There are events for those kinds of interactions, but nothing for detecting key presses or mouse movement/clicks/drags.

The only way I know of to get these events would be through global hooks in the windows api. This project seems like it might be a good place to start.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122