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?
how to use KeyValue in KeyPress event in C#
3.8k Views Asked by user3567615 At
2
There are 2 best solutions below
1
On
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
}
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.