Is it possible to detect Keyboard focus events globally?

后端 未结 4 2374
眼角桃花
眼角桃花 2021-02-19 12:52

The following events can be used, but, they must be attach for each element:

GotKeyboardFocus, LostKeyboardFocus

Is there a way in .NET WPF to globally detect if

4条回答
  •  失恋的感觉
    2021-02-19 13:33

    You can do this in any class with this:

    //In the constructor
    EventManager.RegisterClassHandler(
            typeof(UIElement),
            Keyboard.PreviewGotKeyboardFocusEvent,
            (KeyboardFocusChangedEventHandler)OnPreviewGotKeyboardFocus);
    

    ...

    private void OnPreviewGotKeyboardFocus(object sender, 
                                           KeyboardFocusChangedEventArgs e)
    {
    
         // Your code here
    
    }
    

提交回复
热议问题