What is the difference between the KeyCode and KeyData properties on the .NET WinForms key event argument objects?

本秂侑毒 提交于 2021-02-05 14:33:12

问题


The two key event argument classes KeyEventArgs and PreviewKeyDownEventArgs each have two properties, KeyCode and KeyData, which are both of the enumeration type Keys.

What is the difference between these two properties? Do the values in them ever differ from each other? If so, when and why?


回答1:


KeyCode is an enumeration that represents all the possible keys on the keyboard. KeyData is the KeyCode combined with the modifiers (Ctrl, Alt and/or Shift).

Use KeyCode when you don't care about the modifiers, KeyData when you do.




回答2:


The difference that I have observed is that the value in KeyCode only holds a Keys enumeration value for the key that triggered the current firing of the event. KeyData, on the other hand, will contain a logical OR of the value in KeyCode with any modifier keys (CTRL, SHIFT, ALT, etc.) that are held at the time.



来源:https://stackoverflow.com/questions/318160/what-is-the-difference-between-the-keycode-and-keydata-properties-on-the-net-wi

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