EventManager.RegisterClassHandler( typeof(UIElement), UIElement.PreviewMouseDownEvent, new MouseButtonEventHandler(MouseDown), true );
EventManager.RegisterClassHandler( typeof(UIElement), UIElement.PreviewMouseUpEvent, new MouseButtonEventHandler(MouseUp), true ); EventManager.RegisterClassHandler( typeof(UIElement), UIElement.PreviewKeyDownEvent, new KeyEventHandler(KeyDown), true ); EventManager.RegisterClassHandler( typeof(UIElement), UIElement.PreviewKeyUpEvent, new KeyEventHandler(KeyUp), true ); EventManager.RegisterClassHandler( typeof(UIElement), UIElement.PreviewTextInputEvent, new TextCompositionEventHandler(TextInput), true ); EventManager.RegisterClassHandler( typeof(UIElement), Keyboard.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnKeyboardFocusChanged), true ); EventManager.RegisterClassHandler( typeof(UIElement), Keyboard.LostKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnKeyboardFocusChanged), true );
Dictionary<string, string> CollectCommonProperties(FrameworkElement source) { Dictionary<string, string> properties = new Dictionary<string, string>(); properties["Name"] = source.Name; properties["ClassName"] = source.GetType().ToString(); return properties; }
void MouseDown(object sender, MouseButtonEventArgs e) { FrameworkElement source = sender as FrameworkElement; if(source == null) return; var properties = CollectCommonProperties(source); LogMouse(properties, e, isUp: false); } void MouseUp(object sender, MouseButtonEventArgs e) { FrameworkElement source = sender as FrameworkElement; if(source == null) return; var properties = CollectCommonProperties(source); LogMouse(properties, e, isUp: true); } void LogMouse(IDictionary<string, string> properties, MouseButtonEventArgs e, bool isUp) { properties["mouseButton"] = e.ChangedButton.ToString(); properties["ClickCount"] = e.ClickCount.ToString(); Breadcrumb item = new Breadcrumb(); if(e.ClickCount == 2) { properties["action"] = "doubleClick"; item.Event = BreadcrumbEvent.MouseDoubleClick; } else if(isUp) { properties["action"] = "up"; item.Event = BreadcrumbEvent.MouseUp; } else { properties["action"] = "down"; item.Event = BreadcrumbEvent.MouseDown; } item.CustomData = properties; AddBreadcrumb(item); }
void KeyDown(object sender, KeyEventArgs e) { FrameworkElement source = sender as FrameworkElement; if(source == null) return; var properties = CollectCommonProperties(source); LogKeyboard(properties, e.Key, isUp: false, isPassword: CheckPasswordElement(e.OriginalSource as UIElement)); } void KeyUp(object sender, KeyEventArgs e) { FrameworkElement source = sender as FrameworkElement; if(source == null) return; var properties = CollectCommonProperties(source); LogKeyboard(properties, e.Key, isUp: true, isPassword: CheckPasswordElement(e.OriginalSource as UIElement)); } void LogKeyboard(IDictionary<string, string> properties, Key key, bool isUp, bool isPassword) { properties["key"] = GetKeyValue(key, isPassword).ToString(); properties["action"] = isUp ? "up" : "down"; Breadcrumb item = new Breadcrumb(); item.Event = isUp ? BreadcrumbEvent.KeyUp : BreadcrumbEvent.KeyDown; item.CustomData = properties; AddBreadcrumb(item); } Key GetKeyValue(Key key, bool isPassword) { if(!isPassword) return key; switch(key) { case Key.Tab: case Key.Left: case Key.Right: case Key.Up: case Key.Down: case Key.PageUp: case Key.PageDown: case Key.LeftCtrl: case Key.RightCtrl: case Key.LeftShift: case Key.RightShift: case Key.Enter: case Key.Home: case Key.End: return key; default: return Key.Multiply; } } bool CheckPasswordElement(UIElement targetElement) { if(targetElement != null) { AutomationPeer automationPeer = GetAutomationPeer(targetElement); return (automationPeer != null) ? automationPeer.IsPassword() : false; } return false; }
void TextInput(object sender, TextCompositionEventArgs e) { FrameworkElement source = sender as FrameworkElement; if(source == null) return; var properties = CollectCommonProperties(source); LogTextInput(properties, e, CheckPasswordElement(e.OriginalSource as UIElement)); } void LogTextInput(IDictionary<string, string> properties, TextCompositionEventArgs e, bool isPassword) { properties["text"] = isPassword ? "*" : e.Text; properties["action"] = "press"; Breadcrumb item = new Breadcrumb(); item.Event = BreadcrumbEvent.KeyPress; item.CustomData = properties; AddBreadcrumb(item); }
void OnKeyboardFocusChanged(object sender, KeyboardFocusChangedEventArgs e) { FrameworkElement oldFocus = e.OldFocus as FrameworkElement; if(oldFocus != null) { var properties = CollectCommonProperties(oldFocus); LogFocus(properties, isGotFocus: false); } FrameworkElement newFocus = e.NewFocus as FrameworkElement; if(newFocus != null) { var properties = CollectCommonProperties(newFocus); LogFocus(properties, isGotFocus: true); } } void LogFocus(IDictionary<string, string> properties, bool isGotFocus) { Breadcrumb item = new Breadcrumb(); item.Event = isGotFocus ? BreadcrumbEvent.GotFocus : BreadcrumbEvent.LostFocus; item.CustomData = properties; AddBreadcrumb(item); }
IInputElement FocusedElement { get; set; } void OnKeyboardFocusChanged(object sender, KeyboardFocusChangedEventArgs e) { if(FocusedElement != e.NewFocus) { FrameworkElement oldFocus = FocusedElement as FrameworkElement; if(oldFocus != null) { var properties = CollectCommonProperties(oldFocus); LogFocus(properties, false); } FrameworkElement newFocus = e.NewFocus as FrameworkElement; if(newFocus != null) { var properties = CollectCommonProperties(newFocus); LogFocus(properties, true); } FocusedElement = e.NewFocus; } }
EventManager.RegisterClassHandler( typeof(Control), UIElement.PreviewMouseDownEvent, new MouseButtonEventHandler(MouseDown), true );
EventManager.RegisterClassHandler( typeof(Control), UIElement.PreviewMouseUpEvent, new MouseButtonEventHandler(MouseUp), true ); EventManager.RegisterClassHandler( typeof(Control), UIElement.PreviewKeyDownEvent, new KeyEventHandler(KeyDown), true ); EventManager.RegisterClassHandler( typeof(Control), UIElement.PreviewKeyUpEvent, new KeyEventHandler(KeyUp), true ); EventManager.RegisterClassHandler( typeof(Control), UIElement.PreviewTextInputEvent, new TextCompositionEventHandler(TextInput), true ); EventManager.RegisterClassHandler( typeof(Control), Keyboard.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnKeyboardFocusChanged), true ); EventManager.RegisterClassHandler( typeof(Control), Keyboard.LostKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnKeyboardFocusChanged), true );
Source: https://habr.com/ru/post/343358/
All Articles