/// ------------------------------------------------------------------- /// <summary></summary> /// ------------------------------------------------------------------- static public void WindowOpenedEventHandler(object obj, AutomationEventArgs argument) { try { if (obj is AutomationElement) { AutomationElement element = (AutomationElement)obj; Trace.WriteLine("Window Opened: \"" + element.Current.Name + "\" with AutomationId({" + element.Current.AutomationId + "})"); switch (element.Current.AutomationId) { case "VoiceLanguageWarningForm": // I know this UIAutomationID supports WindowPattern... ((WindowPattern)element.GetCurrentPattern(WindowPattern.Pattern)).Close(); _thread.Abort(); break; } } } catch (Exception exception) { // Just eat the exception Trace.WriteLine("Exception thrown (" + exception.GetType().ToString() + ") - " + exception.Message + "\""); } }
private static void AutoCorrect(object sender, AutomationEventArgs e) { var element = (AutomationElement) sender; string originalText = element.GetText(); string newText = originalText; if (newText.EndsWith(".")) { foreach (var replacement in REPLACEMENTS) { if (newText.Contains(replacement.Key)) { newText = newText.Replace(replacement.Key, replacement.Value); } } } if (newText != originalText) { element.SetFocus(); SendKeys.SendWait("^{HOME}"); SendKeys.SendWait("^+{END}"); SendKeys.SendWait("{DEL}"); SendKeys.SendWait(newText); } }
public void handleWindowEvents(object src, Automation.AutomationEventArgs e) { try { AutomationElement aeSrc = src as AutomationElement; if (aeSrc.Current.Name.Contains("Visual Basic")) { Int32?parentWindowHandle = (_currentScope.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty) as Int32?); if (parentWindowHandle != null && parentWindowHandle > 0) { IntPtr ptrHandle = (IntPtr)parentWindowHandle; //NativeMethods.ShowWindow(ptrHandle, 1); } AutomationElementCollection childrenAutomationElementCollection = aeSrc.FindAll(TreeScope.Children, Condition.TrueCondition); foreach (AutomationElement aeChildren in childrenAutomationElementCollection) { if (aeChildren.Current.Name.Length > 10) { Console.WriteLine(aeChildren.Current.Name); } } } } catch (Exception ex) { } }
//------------------------------------------------------ // // Constructors // //------------------------------------------------------ #region Constructors internal CalloutQueueItem(Delegate clientCallback, UiaCoreApi.UiaCacheResponse cacheResponse, AutomationEventArgs e, UiaCoreApi.UiaCacheRequest cacheRequest) { _clientCallback = clientCallback; _cacheResponse = cacheResponse; _e = e; _cacheRequest = cacheRequest; }
//------------------------------------------------------ // // Constructors // //------------------------------------------------------ #region Constructors internal ClientSideQueueItem(Delegate clientCallback, AutomationElement srcEl, UiaCoreApi.UiaCacheRequest request, AutomationEventArgs e) { _clientCallback = clientCallback; _srcEl = srcEl; _request = request; _e = e; }
public void RaiseAutomationEvent (AutomationEvent eventId, object provider, AutomationEventArgs e) { var providerSimple = provider as IRawElementProviderSimple; if (providerSimple == null) return; ClientEventManager.RaiseAutomationEvent (eventId, providerSimple, e); }
private void HandleAutomationEvent(object sender, UIA.AutomationEventArgs automationEventArgs) { var basicAutomationElement = new TestBasicAutomationElement((TestAutomation)Automation, (UIA.AutomationElement)sender); var senderElement = new AutomationElement(basicAutomationElement); var @event = EventId.Find(AutomationType.UIA2, automationEventArgs.EventId.Id); HandleAutomationEvent(senderElement, @event); }
private void HandleAutomationEvent(object sender, UIA.AutomationEventArgs automationEventArgs) { var frameworkElement = new UIA2FrameworkAutomationElement((UIA2Automation)Automation, (UIA.AutomationElement)sender); var senderElement = new AutomationElement(frameworkElement); var @event = EventId.Find(AutomationType.UIA2, automationEventArgs.EventId.Id); HandleAutomationEvent(senderElement, @event); }
public override void Terminate () { AutomationEventArgs args = new AutomationEventArgs (AEIds.MenuClosedEvent); AutomationInteropProvider.RaiseAutomationEvent (AEIds.MenuClosedEvent, this, args); base.Terminate (); }
public override void RaiseAutomationEvent (AutomationEvent eventId, AutomationEventArgs e) { if (eventId == GridPatternIdentifiers.ColumnReorderedEvent) EmitSignal ("column_reordered"); else base.RaiseAutomationEvent (eventId, e); // TODO }
public override void RaiseAutomationEvent (AutomationEvent eventId, AutomationEventArgs e) { if (eventId != AutomationElementIdentifiers.MenuClosedEvent) base.RaiseAutomationEvent (eventId, e); TopLevelRootItem.Instance.RemoveChild (Parent); ((ParentAdapter)Parent).RemoveChild (this); AutomationBridge.HandleTotalElementRemoval (this.Provider); }
public override void Initialize () { base.Initialize (); AutomationEventArgs args = new AutomationEventArgs (AEIds.MenuOpenedEvent); AutomationInteropProvider.RaiseAutomationEvent (AEIds.MenuOpenedEvent, this, args); }
public override void RaiseAutomationEvent (AutomationEvent eventId, AutomationEventArgs e) { if (eventId == AutomationElementIdentifiers.AsyncContentLoadedEvent) { // TODO: Handle AsyncContentLoadedEvent } else if (eventId == AutomationElementIdentifiers.StructureChangedEvent) { // TODO: Handle StructureChangedEvent } else base.RaiseAutomationEvent (eventId, e); }
protected void RaiseAutomationEvent () { if (AutomationInteropProvider.ClientsAreListening == true) { AutomationEventArgs args = new AutomationEventArgs (Event); AutomationInteropProvider.RaiseAutomationEvent (Event, Provider, args); } }
private IEnumerable<AutomationEventWrapper> AddPulsingHandlers(IEnumerable<AutomationEventWrapper> eventWrappers, AutomationElementWrapper element) { foreach (var wrapper in eventWrappers) { wrapper.Add((src, e) => { _triggeringEvent = e; PulseTheWaitingRoom(); }, element); } return eventWrappers; }
void UIAutomationClient.IUIAutomationEventHandler.HandleAutomationEvent( UIAutomationClient.IUIAutomationElement sender, int eventId) { AutomationEventArgs args; if (eventId != WindowPatternIdentifiers.WindowClosedEvent.Id) { args = new AutomationEventArgs(AutomationEvent.LookupById(eventId)); } else { args = new WindowClosedEventArgs((int[])sender.GetRuntimeId()); } _basicHandler(AutomationElement.Wrap(sender), args); }
//------------------------------------------------------ // // Internal Methods // //------------------------------------------------------ #region Internal Methods // The method that gets called from CallbackQueue's thread. Uses Post to invoke the callback on the proper thread. internal static void InvokeClientHandler(Delegate clientCallback, AutomationElement srcEl, AutomationEventArgs args) { try { if (args is AutomationPropertyChangedEventArgs) { ((AutomationPropertyChangedEventHandler)clientCallback)(srcEl, (AutomationPropertyChangedEventArgs)args); } else if (args is StructureChangedEventArgs) { ((StructureChangedEventHandler)clientCallback)(srcEl, (StructureChangedEventArgs)args); } else if (args is InternalAutomationFocusChangedEventArgs) { AutomationFocusChangedEventArgs realArgs = ((InternalAutomationFocusChangedEventArgs)args)._args; // For focus events, check that the event is actually more recent than the last one (see note at top of file). // Since the timestamps can wrap around, subtract and measure the delta instead of just comparing them. // Any events that appear to have taken place within the 5 seconds before the last event we got will be ignored. // (Because of wraparound, certain before- and after- time spans share the same deltas; 5000ms before has the // same value as MAXUINT-5000ms after. Since we're just trying to filter out very recent event race conditions, // confine this test to a small window, just the most recent 5 seconds. That means you'd have to wait a *very* // long time without any focus changes before getting a false positive here.) uint eventTime = ((InternalAutomationFocusChangedEventArgs)args)._eventTime; if (_lastFocusEventTime != 0) { uint delta = _lastFocusEventTime - eventTime; // Exclude events that happend before the last one, but do allow any that happened "at the same time", // (delta==0) since they likely actually happened after, but within the resolution of the event timer. if (delta < 5000 && delta != 0) { return; } } _lastFocusEventTime = eventTime; ((AutomationFocusChangedEventHandler)clientCallback)(srcEl, realArgs); } else { ((AutomationEventHandler)clientCallback)(srcEl, args); } } catch (Exception e) { if (Misc.IsCriticalException(e)) throw; // Since we can't predict what exceptions an outside client might throw intentionally ignore all } }
internal Waiter(AutomationEvent eventId, AutomationElement element = null, TreeScope scope = TreeScope.Descendants) { this._eventId = eventId; this._element = element ?? AutomationElement.RootElement; this._scope = scope; this._handler = new AutomationEventHandler((src, e) => { this.EvtArgs = e; this.Completion.SetResult(true); }); Automation.AddAutomationEventHandler( this._eventId, this._element, this._scope, this._handler); }
public bool WaitFor(AutomationElementWrapper element, SomethingToWaitFor check, TimeSpan timeout, FailureToHappenHandler failureHandler, IEnumerable<AutomationEventWrapper> events) { Monitor.Enter(_waitingRoom); _triggeringEvent = null; DateTime started = DateTime.Now; var handlerRemovers = AddPulsingHandlers(events, element); bool checkPassed = true; while (!check(element, _triggeringEvent) && DateTime.Now.Subtract(started).CompareTo(timeout) < 0) { checkPassed = false; Monitor.Wait(_waitingRoom, timeout); } Monitor.Exit(_waitingRoom); ClearPulsingHandlers(handlerRemovers); if (!checkPassed && !check(element, null)) { failureHandler(element); return false; } return true; }
// OnWindowShowOrOpen - Called by the WindowShowOrOpenTracker class when UI is shown or created private static void OnWindowShowOrOpen( IntPtr hwnd, AutomationElement rawEl ) { bool doWindowOpenedEvent = false; bool doStructureChangedEvent = false; lock ( _classLock ) { if (_listeners != null) { // if rawEl is w/in the scope of any listeners then register for events in the new UI for (int i = 0; i < _listeners.Count; i++) { EventListenerClientSide ec = (EventListenerClientSide)_listeners[i]; EventListener l = ec.EventListener; if ( l.EventId == WindowPattern.WindowOpenedEvent ) doWindowOpenedEvent = true; if ( l.EventId == AutomationElement.StructureChangedEvent ) doStructureChangedEvent = true; // Only advise UI contexts if the provider might raise that event. if (!ShouldAdviseProviders( l.EventId )) continue; // Only advise UI contexts if the element is w/in scope of the reference element if (!ec.WithinScope( rawEl )) continue; // Notify the server side UiaCoreApi.UiaEventAddWindow(ec.EventHandle, hwnd); } } } // Piggy-back on the listener for Windows hiding or closing to raise WindowClosed and StructureChanged events. if ( doWindowOpenedEvent ) { if ( HwndProxyElementProvider.IsWindowPatternWindow( NativeMethods.HWND.Cast( hwnd ) ) ) { // Go ahead and raise a client-side only WindowOpenedEvent (if anyone is listening) AutomationEventArgs e = new AutomationEventArgs( WindowPattern.WindowOpenedEvent ); RaiseEventInThisClientOnly( WindowPattern.WindowOpenedEvent, rawEl, e); } } if ( doStructureChangedEvent ) { // Filter on the control elements. Otherwise, this is extremely noisy. Consider not filtering if there is feedback. //ControlType ct = (ControlType)rawEl.GetPropertyValue( AutomationElement.ControlTypeProperty ); //if ( ct != null ) { // Last,raise an event for structure changed StructureChangedEventArgs e = new StructureChangedEventArgs( StructureChangeType.ChildAdded, rawEl.GetRuntimeId() ); RaiseEventInThisClientOnly(AutomationElement.StructureChangedEvent, rawEl, e); } } }
void PopupListElementSelectedHandler(object sender, AutomationEventArgs e) { _syncContext.Post(delegate { Debug.Print("### Thread receiving PopupListElementSelectedHandler: " + Thread.CurrentThread.ManagedThreadId); UpdateSelectedItem(sender as AutomationElement); }, null); }
void TextChanged(object sender, AutomationEventArgs e) { Debug.WriteLine("! Active Text text changed. Is it the Formula Bar? {0}, Is it the In Cell Edit? {1}", sender.Equals(_formulaBar), sender.Equals(_inCellEdit)); UpdateFormula(); }
public override void RaiseAutomationEvent (AutomationEvent eventId, AutomationEventArgs args) { base.RaiseAutomationEvent (eventId, args); }
public void Show (Control control) { if (AutomationInteropProvider.ClientsAreListening == true) { message = GetTextFromControl (control); //TODO: We need deeper tests in Vista because MS is generating both events Helper.RaiseStructureChangedEvent (StructureChangeType.ChildAdded, this); AutomationEventArgs eventArgs = new AutomationEventArgs (AutomationElementIdentifiers.ToolTipOpenedEvent); AutomationInteropProvider.RaiseAutomationEvent (AutomationElementIdentifiers.ToolTipOpenedEvent, this, eventArgs); } }
// Raise events for rawEl internal static void RaiseEventInThisClientOnly(AutomationEvent eventId, AutomationElement rawEl, AutomationEventArgs e) { // This version of RaiseEventInThisClientOnly can be called with a local (proxied) or remote (native)AutomationElement lock (_classLock) { if ( _listeners == null ) return; AutomationElement el = rawEl; foreach (EventListenerClientSide listener in _listeners) { // Is this event a type this listener is interested in? if (listener.EventListener.EventId == eventId) { // Did this event happen on an element this listener is interested in? if (rawEl == null || listener.WithinScope( rawEl )) { UiaCoreApi.UiaCacheRequest cacheRequest = listener.EventListener.CacheRequest; CBQ.PostWorkItem(new ClientSideQueueItem(listener.ClientCallback, el, cacheRequest, e)); } } } } }
/// <summary> /// Callback function invoked by Windows automation if there are any /// changes in the text control /// </summary> /// <param name="sender">sender of the event</param> /// <param name="e">event args</param> private void onTextChanged(object sender, AutomationEventArgs e) { triggerTextChanged(this); }
protected bool checkNotNull(object objectToTest, AutomationEventArgs e) { AutomationElement sourceElement; try { sourceElement = objectToTest as AutomationElement; this.EventSource = sourceElement; this.EventArgs = e; } catch { //(ElementNotAvailableException eNotAvailable) { return false; } return true; }
// Raise events for the element that has RuntimeId == rid (special case for events where the element is no longer available) internal static void RaiseEventInThisClientOnly( AutomationEvent eventId, int [] rid, AutomationEventArgs e) { // This version of RaiseEventInThisClientOnly can be called with a local (proxied) or remote (native)AutomationElement lock ( _classLock ) { if ( _listeners == null ) return; foreach ( EventListenerClientSide listener in _listeners ) { // Is this event a type this listener is interested in? if ( listener.EventListener.EventId == eventId ) { // Did this event happen on an element this listener is interested in? if ( listener.WithinScope( rid ) ) { CBQ.PostWorkItem(new ClientSideQueueItem(listener.ClientCallback, null, null, e)); } } } } }
protected internal void OnUIRecordingAutomationEvent(object src, AutomationEventArgs e) { try { // experimental AutomationElement sourceElement; string elementTitle = String.Empty; string elementType = String.Empty; AutomationEvent eventId = null; try { sourceElement = src as AutomationElement; try { elementTitle = sourceElement.Cached.Name; } catch { } try { elementType = sourceElement.Cached.ControlType.ProgrammaticName; } catch { } try { elementType = elementType.Substring( elementType.IndexOf('.') + 1); if (elementType.Length == 0) { return; } } catch { } try { eventId = e.EventId; if (sourceElement == null || elementType.Length == 0 || eventId == null) { return; } } catch { } } catch (ElementNotAvailableException) { return; } // try { string whatToWrite = String.Empty; // string elementType = // // getControlTypeNameOfAutomationElement(sourceElement, sourceElement); // sourceElement.Current.ControlType.ProgrammaticName.Substring( // sourceElement.Current.ControlType.ProgrammaticName.IndexOf('.') + 1); // if (elementType.Length == 0) { // return; //} string specificToEvent = String.Empty; // try { if (eventId == AutomationElement.AsyncContentLoadedEvent) { specificToEvent = "#AsyncContentLoadedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == SelectionItemPattern.ElementAddedToSelectionEvent) { specificToEvent = "SelectItem -AddToSelection:$true -ItemName " + elementTitle; } if (eventId == SelectionItemPattern.ElementRemovedFromSelectionEvent) { specificToEvent = "SelectItem -RemoveFromSelection:$true -ItemName " + elementTitle; } if (eventId == SelectionItemPattern.ElementSelectedEvent) { specificToEvent = "SelectItem -ItemName " + elementTitle; } if (eventId == SelectionPattern.InvalidatedEvent) { specificToEvent = "#InvalidatedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == InvokePattern.InvokedEvent) { specificToEvent = "Click"; } if (eventId == AutomationElement.LayoutInvalidatedEvent) { specificToEvent = "#LayoutInvalidatedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == AutomationElement.MenuClosedEvent) { specificToEvent = "#MenuClosedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == AutomationElement.MenuOpenedEvent) { specificToEvent = "#MenuOpenedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == TextPattern.TextChangedEvent) { specificToEvent = "#TextChangedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == TextPattern.TextSelectionChangedEvent) { specificToEvent = "#TextSelectionChangedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == AutomationElement.ToolTipClosedEvent) { specificToEvent = "#ToolTipClosedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == AutomationElement.ToolTipOpenedEvent) { specificToEvent = "#ToolTipOpenedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == WindowPattern.WindowOpenedEvent) { specificToEvent = "#WindowOpenedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == AutomationElement.AutomationFocusChangedEvent) { specificToEvent = "#AutomationFocusChangedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == AutomationElement.AutomationPropertyChangedEvent) { specificToEvent = "#AutomationPropertyChangedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; // specificToEvent += "old value: "; // specificToEvent += eventId. } if (eventId == AutomationElement.StructureChangedEvent) { specificToEvent = "#StructureChangedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } if (eventId == WindowPattern.WindowClosedEvent) { specificToEvent = "#WindowClosedEvent triggered\r\n#source title: " + elementTitle + " of the type " + elementType; } } catch (Exception e1) { WriteVerbose(this, "Event handling for element: " + sourceElement.Current.Name + " eventId: " + eventId + " failed"); WriteVerbose(this, e1.Message); } // } else { // // handle any other events // } if (specificToEvent.Length > 0) { if (specificToEvent.Substring(0, 1) != "#") { whatToWrite += "Invoke-UIA"; whatToWrite += elementType; whatToWrite += specificToEvent; } else { whatToWrite = specificToEvent; } if (whatToWrite != ((System.Collections.ArrayList)this.Recording[this.Recording.Count - 1])[0].ToString()) { ((System.Collections.ArrayList)this.Recording[this.Recording.Count - 1]).Insert(0, whatToWrite); } } //} catch { return; } } catch (Exception eUnknown) { // WriteVerbose("!!!OnUIRecording " + eUnknown.Message); WriteDebug(this, eUnknown.Message); } // experimental try { WriteVerbose(this, e.EventId + "on " + (src as AutomationElement) + " fired"); } catch { } }
protected void OnUIAutomationEvent(object src, AutomationEventArgs e) { if (!checkNotNull(src, e)) return; RunEventScriptBlocks(this); try { WriteVerbose(this, e.EventId + " on " + (src as AutomationElement) + " fired"); } catch { } }
/// <summary> /// Handler for window close event /// </summary> /// <param name="sender">Object that raised the event</param> /// <param name="ea">Event arguments</param> private void OnWindowCloseHandler(object sender, AutomationEventArgs ea) { Check.Ensure(ea.EventId == WindowPattern.WindowClosedEvent, "We subscribed only to the closed event"); // Invoke event handler if (this.WindowDestroyed != null) { this.WindowDestroyed(this, EventArgs.Empty); } }
public override void RaiseAutomationEvent (AutomationEvent eventId, AutomationEventArgs e) { if (eventId == InvokePatternIdentifiers.InvokedEvent) { OnPressed (); OnReleased (); } else if (eventId == AutomationElementIdentifiers.AutomationFocusChangedEvent) { // TODO: Handle AutomationFocusChangedEvent } else if (eventId == AutomationElementIdentifiers.StructureChangedEvent) { // TODO: Handle StructureChangedEvent } base.RaiseAutomationEvent (eventId, e); }
// OnMenuEvent - Called by MenuTracker class private static void OnMenuEvent( AutomationElement rawEl, bool menuHasOpened ) { AutomationEvent eventId = menuHasOpened ? AutomationElement.MenuOpenedEvent : AutomationElement.MenuClosedEvent; AutomationEventArgs e = new AutomationEventArgs( eventId ); RaiseEventInThisClientOnly(eventId, rawEl, e); }