tags:

views:

4029

answers:

4

In a KeyDown event, I have the KeyEventArgs to work with. It has (among other things) these three properties:

  • e.KeyCode
  • e.KeyData
  • e.KeyValue

Which one should I use for what?

+5  A: 

Edit: Somehow I misread your question to include checking a valid character. Did you modify it? I've added a description of each.

  • KeyCode is the Keys enumeration value for the key that is down
  • KeyData is the same as KeyCode, but combined with any SHIFT/CTRL/ALT keys
  • KeyValue is simply an integer representation of KeyCode

If you just need the character, I'd probably recommend using the KeyPress event and using the KeyPressEventArgs.KeyChar property. You can then use Char.IsLetterOrDigit() to find out if it's a valid character.

Alternatively, you might be able to cast KeyEventArgs.KeyCode to a char and then use Char.IsLetterOrDigit on that.

Richard Szalay
I don't agree... You are assuming that the user does not want to catch any modifiers and his input is limited to keyboard ASCII.
Cerebrus
IsLetterOrDigit only works with ascii?
Svish
IsLetterOrDigit uses CharUnicodeInfo.GetUnicodeCategory if the character is not latin.
Richard Szalay
Yeah, sorry, I decided I should split it up, since I wanted to know this as well, but more in a general sense :)
Svish
Tested the IsLetterOrDigit now, and it works like it should. Both with regular ascii keys, norwegian æ, ø and å letters, and when combining an accent key with another. Like ¨ + o = ö. Perfect!
Svish
I also did validation against some russian and greek characters and it worked fine.
Richard Szalay
Sweet! I find it amusing that you edit your original answer to incorporate other answers, thus giving the impression that your original answer was comprehensive/complete.
Cerebrus
Actually, I had added in the first major addition (the list) before I saw your answer. I won't deny that they are similar, though.
Richard Szalay
Your explanation is valued and accepted. Thanks!:-)
Cerebrus
Thanks for the list with simple descriptions. I thought that's what the difference between KeyCode and KeyData, but why couldn't MSDN just say so?
Jon Turner
+1  A: 

I would suggest using the KeyCode property to check against the Keys enumeration for most operations. However some of the basic differences below might help you to better decide which one you need for your situation.

Differences:

  • KeyCode - Represents the Keys enumeration value that represents the key that is currently in Down state.

  • KeyData - Same as KeyCode, except that it has additional information in the form of modifiers - Shift/Ctrl/Alt etc.

  • KeyValue - The numeric value of the KeyCode.

Cerebrus
But KeyCode and KeyData has the same type, Keys enumeration. I can't find any extra there?
Svish
It will only be different if a key modifier (shift/alt/ctrl) is down.
Richard Szalay
ooh, cause its a flags thing?
Svish
Yep, 'coz it's a Flags "thing"! ;-)
Cerebrus
A: 

Very basic using of KeyDown

   private void tbSomeText_KeyDown (object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.B && e.Modifiers != Keys.Shift) {

            MessageBox.Show("You Pressed b");
        }
        else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Shift) {
            MessageBox.Show("You Pressed Shift+A");
        }
    }
Soltys
A: 

See my answer to your other question:

Use the KeyPressed event.

Quoting MSDN:

A KeyPressEventArgs specifies the character that is composed when the user presses a key. For example, when the user presses SHIFT + K, the KeyChar property returns an uppercase K.

This way you don't need to mess around with e.KeyCode, e.KeyData and e.KeyValue.

Treb
Yeah, accepted your answer in the other question. But here I was more looking for a general explanation of their uses. Which is why I split it up. Something which I came to think about juuust when I clicked the submit button, so, sorry about the confusion =)
Svish