Did user use keyboard or mouse to leave WPF TextBo

2019-06-17 03:23发布

I have a handler for the TextBox's PreviewLostKeyboardFocus event. It fires when I leave the TextBox using the keyboard (Tab key) or the mouse (by clicking on another TextBox on the form).

The handler takes a KeyboardFocusChangedEventArgs, which has a property named KeyboardDevice, which isn't null in either scenario (I was hoping to find null here when using the mouse).

Question: How can I tell whether a user used the keyboard or the mouse to leave a WPF TextBox?

1条回答
Evening l夕情丶
2楼-- · 2019-06-17 04:16

The e.KeyboardDevice.GetKeyStates(Key.Tab) (where e is of type KeyboardFocusChangedEventArgs) reports:

  • None (when mouse was used to change focus)
  • Down, Toggled (when TAB was use to leave the TextBox)

Would that work for you?

查看更多
登录 后发表回答