在AutomationFocusChangedEventHandler中评估按键和鼠标单击状态

时间:2019-02-05 15:57:00

标签: c# multithreading ui-automation

我正在开发一个应用程序,以通过从System.Windows.Automation命名空间(UIAutomationClient)订阅AutomationFocusChangedEventHandler来跟踪在Windows中执行的事件。

在每个事件上,我都会提供后台工作线程以在DoWork Handler中捕获事件的详细信息。现在,我想在捕获事件详细信息之前检查是否通过按键或鼠标单击引发了事件。

我尝试使用SetWindowsHookEx Win32 API创建用于跟踪键盘和mouseclick事件的全局挂钩。但是此API将帮助我们执行任务,而不是提供按键或鼠标单击的状态。我需要检查按键状态和鼠标单击状态,并将其传递给后台线程

这是我当前正在处理的代码段。请让我知道如何使用OnAutomationFocusChanged处理程序检查按键和鼠标单击的状态。

     /// <summary>
            /// Create an event handler and register it.
            /// </summary>
            public void SubscribeToFocusChange()
            {
                _hookID = _hook.SetHook(callback);    
                focusHandler = new AutomationFocusChangedEventHandler(OnAutomationFocusChanged);
                Automation.AddAutomationFocusChangedEventHandler(focusHandler);
            }


     protected virtual void OnAutomationFocusChanged(AutomationElement element)
     {
//       Console.WriteLine("bKeyPress:{0}", _hook.bKeyPress);              
// This is the place , we need to capture the state of keypress and mouse click

        //prepare background thread worker for work.
          InitiliazeBackgroundWorker();

        //we will lock the workLock so we have to wait until previous work completes
       if (this._focusChangedWorker.IsBusy)
                    return;    
// Here instead of element we need to pass custom object with Element and keypress and mouseclick status to update the background thread.
          this._focusChangedWorker.RunWorkerAsync(element);

    }

0 个答案:

没有答案