/// <summary> /// Get the default Event Recording Configuration /// </summary> /// <returns></returns> public static RecorderSetting GetDefaultRecordingConfig() { RecorderSetting config = new RecorderSetting { // Set Individual Event Events = (from e in EventType.GetInstance().GetKeyValuePairList() select new RecordEntitySetting() { Type = RecordEntityType.Event, Id = e.Key, Name = e.Value, IsRecorded = false }).ToList(), // Set properties Properties = (from e in PropertyType.GetInstance().GetKeyValuePairList() select new RecordEntitySetting() { Type = RecordEntityType.Property, Id = e.Key, Name = e.Value, IsRecorded = false }).ToList(), // Set Global event IsListeningFocusChangedEvent = true, // Individual Event Scope ListenScope = ListenScope.Subtree, }; return(config); }
/// <summary> /// Constructor for event or property leaf node from id /// </summary> /// <param name="id"></param> /// <param name="type"></param> public EventConfigNodeViewModel(int id, EventConfigNodeType type) { this.Id = id; if (type == EventConfigNodeType.Event) { this.Header = EventType.GetInstance().GetNameById(id); } else { this.Header = PropertyType.GetInstance().GetNameById(id); } this.Type = type; this.ButtonVisibility = Visibility.Collapsed; this.TextVisibility = Visibility.Visible; }
public static RecorderSetting LoadConfiguration(string path) { // Get Recorder configuration from local location. but if it is not available, get it from default location. RecorderSetting config = new RecorderSetting(); config = RecorderSetting.LoadFromJSON <RecorderSetting>(path); if (config == null) { config = GetDefaultRecordingConfig(); config.SerializeInJSON(path); } else { // check whether there is any new events to be added into configuration. var events = EventType.GetInstance(); var ms = from e in events.GetKeyValuePairList() where IsNotInList(e.Key, config.Events) select e; if (ms.Any()) { foreach (var m in ms) { config.Events.Add(new RecordEntitySetting() { Id = m.Key, Name = m.Value, IsRecorded = false, Type = RecordEntityType.Event, }); } config.SerializeInJSON(path); } config.IsListeningAllEvents = false; } return(config); }
/// <summary> /// Create a property bag based on RecorderSetting changes. /// </summary> /// <param name="cfg"></param> /// <returns></returns> private static IDictionary <string, string> GetPropertyBag(RecorderSetting cfg) { Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("Scope", cfg.ListenScope.ToString()); if (cfg.IsListeningFocusChangedEvent) { dic.Add(EventType.UIA_AutomationFocusChangedEventId.ToString(CultureInfo.InvariantCulture), EventType.GetInstance().GetNameById(EventType.UIA_AutomationFocusChangedEventId)); } foreach (var e in cfg.Events) { if (e.IsRecorded) { dic.Add(e.Id.ToString(CultureInfo.InvariantCulture), e.Name); } } foreach (var p in cfg.Properties) { if (p.IsRecorded) { dic.Add(p.Id.ToString(CultureInfo.InvariantCulture), p.Name); } } return(dic); }
/// <summary> /// Process Register event message /// </summary> /// <param name="msgData"></param> private void RegisterEventListener(EventListenerFactoryMessage msgData) { try { EventMessage m = null; var win32Helper = new Win32Helper(); switch (msgData.EventId) { case EventType.UIA_AutomationFocusChangedEventId: if (this.EventListenerFocusChanged == null) { var uia = (IUIAutomation)UIAutomation8 ?? UIAutomation; this.EventListenerFocusChanged = new FocusChangedEventListener(uia, msgData.Listener); } break; case EventType.UIA_StructureChangedEventId: if (this.EventListenerStructureChanged == null) { this.EventListenerStructureChanged = new StructureChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_AutomationPropertyChangedEventId: if (this.EventListenerPropertyChanged == null) { this.EventListenerPropertyChanged = new PropertyChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener, msgData.Properties); } break; case EventType.UIA_TextEdit_TextChangedEventId: if (this.EventListenerTextEditTextChanged == null) { this.EventListenerTextEditTextChanged = new TextEditTextChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_ChangesEventId: if (this.EventListenerChanges == null) { this.EventListenerChanges = new ChangesEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_NotificationEventId: if (win32Helper.IsWindowsRS3OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerNotification = new NotificationEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { #pragma warning disable CA2000 // Call IDisposable.Dispose() m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Event listener registration is rejected."), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Reason", "Not supported platform"), }; msgData.Listener(m); } break; case EventType.UIA_ActiveTextPositionChangedEventId: if (win32Helper.IsWindowsRS5OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerActiveTextPositionChanged = new ActiveTextPositionChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { #pragma warning disable CA2000 // Call IDisposable.Dispose() m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Event listener registration is rejected."), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Reason", "Not supported platform"), }; msgData.Listener(m); } break; default: if (this.EventListeners.ContainsKey(msgData.EventId) == false) { this.EventListeners.Add(msgData.EventId, new EventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.EventId, msgData.Listener)); } break; } #pragma warning disable CA2000 // Call IDisposable.Dispose() m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Succeeded to register an event listener"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), }; msgData.Listener(m); if (msgData.EventId == EventType.UIA_AutomationFocusChangedEventId) { this.EventListenerFocusChanged.ReadyToListen = true; } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Failed to register an event listener"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Error", e.Message) }; msgData.Listener(m); } #pragma warning restore CA1031 // Do not catch general exception types }
/// <summary> /// Process Unregister event message /// </summary> /// <param name="msgData"></param> private void UnregisterEventListener(EventListenerFactoryMessage msgData) { HandleUIAutomationEventMessage listener = null; try { switch (msgData.EventId) { case EventType.UIA_AutomationFocusChangedEventId: if (this.EventListenerFocusChanged != null) { listener = this.EventListenerFocusChanged.ListenEventMessage; this.EventListenerFocusChanged.Dispose(); this.EventListenerFocusChanged = null; } break; case EventType.UIA_StructureChangedEventId: if (this.EventListenerStructureChanged != null) { listener = this.EventListenerStructureChanged.ListenEventMessage; this.EventListenerStructureChanged.Dispose(); this.EventListenerStructureChanged = null; } break; case EventType.UIA_AutomationPropertyChangedEventId: if (this.EventListenerPropertyChanged != null) { listener = this.EventListenerPropertyChanged.ListenEventMessage; this.EventListenerPropertyChanged.Dispose(); this.EventListenerPropertyChanged = null; } break; case EventType.UIA_TextEdit_TextChangedEventId: if (this.EventListenerTextEditTextChanged != null) { listener = this.EventListenerTextEditTextChanged.ListenEventMessage; this.EventListenerTextEditTextChanged.Dispose(); this.EventListenerTextEditTextChanged = null; } break; case EventType.UIA_ChangesEventId: if (this.EventListenerChanges != null) { listener = this.EventListenerChanges.ListenEventMessage; this.EventListenerChanges.Dispose(); this.EventListenerChanges = null; } break; case EventType.UIA_NotificationEventId: if (this.EventListenerNotification != null) { listener = this.EventListenerNotification.ListenEventMessage; this.EventListenerNotification.Dispose(); this.EventListenerNotification = null; } break; case EventType.UIA_ActiveTextPositionChangedEventId: if (this.EventListenerActiveTextPositionChanged != null) { listener = this.EventListenerActiveTextPositionChanged.ListenEventMessage; this.EventListenerActiveTextPositionChanged.Dispose(); this.EventListenerActiveTextPositionChanged = null; } break; default: if (this.EventListeners.ContainsKey(msgData.EventId)) { var l = this.EventListeners[msgData.EventId]; listener = l.ListenEventMessage; this.EventListeners.Remove(msgData.EventId); l.Dispose(); } break; } if (listener != null) { #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Succeeded to unregister a event listeners"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), }; listener(m); } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Failed to unregister a event listeners"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Error", e.Message) }; listener(m); /// it is very unexpected situation. /// need to figure out a way to prevent it or handle it more gracefully } #pragma warning restore CA1031 // Do not catch general exception types }
/// <summary> /// Process Register event message /// </summary> /// <param name="msgData"></param> private void RegisterEventListener(EventListenerFactoryMessage msgData) { try { EventMessage m = null; switch (msgData.EventId) { case EventType.UIA_AutomationFocusChangedEventId: if (this.EventListenerFocusChanged == null) { this.EventListenerFocusChanged = new FocusChangedEventListener(this.UIAutomation, msgData.Listener); } break; case EventType.UIA_StructureChangedEventId: if (this.EventListenerStructureChanged == null) { this.EventListenerStructureChanged = new StructureChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_AutomationPropertyChangedEventId: if (this.EventListenerPropertyChanged == null) { this.EventListenerPropertyChanged = new PropertyChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener, msgData.Properties); } break; case EventType.UIA_TextEdit_TextChangedEventId: if (this.EventListenerTextEditTextChanged == null) { this.EventListenerTextEditTextChanged = new TextEditTextChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_ChangesEventId: if (this.EventListenerChanges == null) { this.EventListenerChanges = new ChangesEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_NotificationEventId: if (NativeMethods.IsWindowsRS3OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerNotification = new NotificationEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Event listener registration is rejected."), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Reason", "Not supported platform"), }; msgData.Listener(m); } break; case EventType.UIA_ActiveTextPositionChangedEventId: if (NativeMethods.IsWindowsRS5OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerActiveTextPositionChanged = new ActiveTextPositionChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Event listener registration is rejected."), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Reason", "Not supported platform"), }; msgData.Listener(m); } break; default: if (this.EventListeners.ContainsKey(msgData.EventId) == false) { this.EventListeners.Add(msgData.EventId, new EventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.EventId, msgData.Listener)); } break; } m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Succeeded to register an event listener"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), }; msgData.Listener(m); if (msgData.EventId == EventType.UIA_AutomationFocusChangedEventId) { this.EventListenerFocusChanged.ReadyToListen = true; } } catch (Exception e) { var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Failed to register an event listener"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Error", e.Message) }; msgData.Listener(m); } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { // do not let the culture default to local to prevent variable outcome re decimal syntax var eId = (int)value; return(EventType.GetInstance().GetNameById(eId)); }