user3567615
user3567615

Reputation: 31

how to use KeyValue in KeyPress event in C#

In key press event we can use KeyChar property and in KeyDown event we can use KeyValue. I need to use KeyValue and KeyChar property in KeyPress event. How can I do it?

Upvotes: 2

Views: 4087

Answers (2)

Nima Rostami
Nima Rostami

Reputation: 2932

You can not access KeyValue in KeyPress event. but you can use KeyDown event instead. then you can use KeyValue and then e.KeyCode.ToString() to get KeyChar.

in KeyDown event : e.KeyCode.ToString() = KeyChar

example:

Private void YourControl_KeyDown(sender As Object, e As KeyEventArgs) 
{
    var keyChar = e.KeyCode.ToString()
    var keyValue = e.KeyValue
}

Upvotes: 5

Ross Presser
Ross Presser

Reputation: 6255

I do not think you can. Consider that a single KeyPress might require the user pressing just one key, or more than one; and the same key being pressed might result in different characters firing in the KeyPress event. Pressing the "a" key on the keyboard might result in the characters "a", "A", or no character at all (ctrl-A), depending on whether caps lock is on, or the shift key or control key is pressed. In short, the KeyPress event does valuable preprocessing for you, and you cannot get the original information back from before the preprocessing.

Upvotes: 2

Related Questions