Get Char from Key

Jesús Galindo picture Jesús Galindo · Sep 6, 2011 · Viewed 57.8k times · Source

I am coding a custom text editor, and I use KeyDown and KeyUp events. That events gets a KeyEventArgs from parameters where a "Key" instance is included.

How I can transform that "Key" to a real char?

Using Key.ToString() with a "." I get a "OmePeriod" or with "," I get a "OmeComma". I can transform that values directly...but It is a hard work, and I am sure that must exist any class that transform that Keys to the real "char".

Thanks!

Regards.

Answer

VdesmedT picture VdesmedT · Sep 6, 2011

The main problem here is that the event you are catching is reel keyboard event and that the key is indeed a key and NOT a character. In between is the keymap ! For example, when typing shift-A, you will receive two event instead of one for keypressed.

this discussion can help : C# How to translate virtual keycode to char?