如何使用UIAutomation获取鼠标和键盘事件

问题描述:

我尝试使用C#UIAutomation库创建测试工具应用程序。 示例代码是:如何使用UIAutomation获取鼠标和键盘事件

Automation.AddAutomationFocusChangedEventHandler(
    new AutomationFocusChangedEventHandler(OnAutomationFocusChanged)); 

当我发现焦点变了,我需要得到事件的类型(左/右键点击或键盘按下)。 是否有可能做到这一点我如何在方法OnAutomationFocusChanged()中找到这些值?

请参阅下面的代码:

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

也可参考此:

Move mouse with c#

希望它有帮助。

+0

THANKs,但我需要检测鼠标和键盘事件,而不是发送鼠标和键盘事件。 – 2013-04-10 05:02:45

不,这在UI自动化中不可行。像鼠标和键盘事件这样的低级交互不是API的一部分。该API是围绕更高级别的交互设计的,如Invoke,或Select中的SelectionItemPattern。有这些交互的事件,但没有检测到按键或鼠标移动/点击/拖动。

我知道获取这些事件的唯一方法是通过windows api中的全局钩子。这project好像它可能是一个很好的开始。