none
Mouse focus versus keyboard focus RRS feed

  • Question

  • How does WPF set keyboard focus internally?

    I'm in a situation where clicking on a user control does not seem to be giving the control keyboard focus. Don't mouse clicks typically give keyboard focus to a control?

    The control gets keyboard focus when it is tabbed to, but somebody seems to intercepting the key strokes otherwise.

    Any thoughts?
    Thursday, January 17, 2008 2:55 PM

Answers

  • In WPF, there is two type of focus, they are keyboard focus and logical focus as documented in this msdn article. There is no concept called mouse focus. In WPF, some elements will get keyboard focus when clicked such as ButtonBase derivatives(Button, CheckBox, ToggleButton, RadioButton etc), if you want this behaviour for other type of element, you can use the following trick:
    userControl.MouseLeftButtonDown += delegate 
    {
    userControl.Focusable = true;
    IInputElement element = Keyboard.Focus(userControl);
    };

    Hope this helps


    Monday, January 21, 2008 5:19 AM

All replies

  • The main control wasn't getting the focus because a child was handling the mouse events. I now getting the keyboard events at the top level because I am not marking the mouse events as handled.

    I am still interested in discovering how keyboard focus is set and handled by WPF. Often is seems like a mystery.
    Thursday, January 17, 2008 3:31 PM
  • Im not sure either, but I know you can force keyboard focus using UIElement.Focus() within your mousedown event.

    Thursday, January 17, 2008 3:37 PM
  • Yeah, I tried that. As long as the sub-element sents the event argument to be handled (e.Handled = trueWink, protected override void OnKeyDown(KeyEventArgs e) does not get called, even if I call this.Focus() in the mouse down handler for the sub-item in the user control.

    This is still a very relevant topic... I have some nested sub-items; the inner-most items MUST mark the event as handled so that the outer sub-items do not attempt to handle the mouse events as well. Any time mouse events are occuring inside of the main user control I would like to receive keyboard events in one top level location.

    Any other suggestions?

    Thanks.
    Thursday, January 17, 2008 4:25 PM
  • In WPF, there is two type of focus, they are keyboard focus and logical focus as documented in this msdn article. There is no concept called mouse focus. In WPF, some elements will get keyboard focus when clicked such as ButtonBase derivatives(Button, CheckBox, ToggleButton, RadioButton etc), if you want this behaviour for other type of element, you can use the following trick:
    userControl.MouseLeftButtonDown += delegate 
    {
    userControl.Focusable = true;
    IInputElement element = Keyboard.Focus(userControl);
    };

    Hope this helps


    Monday, January 21, 2008 5:19 AM
  • This worked. Thanks.

    Why do you think "Keyboard.Focus(userControl);" worked when "userControl.Focus()" didn't?

    Was it because of "userControl.Focusable = false;"?
    Monday, January 21, 2008 12:50 PM
  • Keyboard.Focus() and userControl.Focus behaves the same, both of those methods will only move focus to element which is focusable(aka Focusable is set to true).

    Hope this helps
    Tuesday, January 22, 2008 4:56 AM