Is it possible to detect Keyboard focus events globally?

后端 未结 4 1728
无人及你
无人及你 2021-02-19 12:45

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:34

    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
    
    }
    

提交回复
热议问题