Did user use keyboard or mouse to leave WPF TextBox?

☆樱花仙子☆ 提交于 2019-12-08 19:30:20

问题


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:


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?



来源:https://stackoverflow.com/questions/6344652/did-user-use-keyboard-or-mouse-to-leave-wpf-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!