Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 906
Chris Ammerman Avatar asked Nov 25 '08 17:11

Chris Ammerman


2 Answers

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.

like image 153
gcores Avatar answered Oct 21 '22 10:10

gcores


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.

like image 26
Chris Ammerman Avatar answered Oct 21 '22 09:10

Chris Ammerman