22

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Jesús Galindo
  • 471
  • 1
  • 5
  • 11
  • 1
    It was discussed already http://stackoverflow.com/questions/7103360/how-to-get-pressed-char-from-system-windows-input-keyeventargs – Samich Sep 06 '11 at 08:12
  • I think answers to http://stackoverflow.com/questions/544141/how-to-convert-a-character-in-to-equivalent-system-windows-input-key-enum-value question may help. – Dmitriy Sep 06 '11 at 08:16
  • Possible duplicate of [C# How to translate virtual keycode to char?](http://stackoverflow.com/questions/318777/c-sharp-how-to-translate-virtual-keycode-to-char) – bummi Jan 11 '16 at 07:53

5 Answers5

7

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?

Community
  • 1
  • 1
VdesmedT
  • 9,037
  • 3
  • 34
  • 50
5

Instead of using the KeyDown event, try using the TextInput event. Attach an event handler that looks like this:

private void Grid_TextInput(object sender, TextCompositionEventArgs e)
{
    Char keyChar = (Char)System.Text.Encoding.ASCII.GetBytes(e.Text)[0];
    Debug.WriteLine(keyChar);
}

Note, this grabs only the first byte, and this will not work for international (Unicode) text. It may not work for more complex text input events either, so be advised!

See this MSDN article for some more info.

markmuetz
  • 9,334
  • 2
  • 32
  • 33
  • there are many questions to capturing textinput in wpf, some answers are even refering to system dlls. But your answer is by far the best i've seen so far – jflepp Sep 07 '17 at 18:34
3

This gives you exactly what you need .... that too locale specific character from the Keyboard's Key code ...

how to capture the '#' character on different locale keyboards in WPF/C#?

Community
  • 1
  • 1
WPF-it
  • 19,625
  • 8
  • 55
  • 71
0

Use the KeyEventArgs.Key property and the following: How to convert a character in to equivalent System.Windows.Input.Key Enum value, but instead use the following: KeyInterop.VirtualKeyFromKey Method

Community
  • 1
  • 1
Paul
  • 4,009
  • 1
  • 17
  • 15
-4

Cast the e.KeyValue to char. like example:

eg.

private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
    char pressedCharacter = (char)e.KeyValue;
}
Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109
  • There is no e.KeyValue – MSL Oct 31 '16 at 12:25
  • This is what I was after for my purposes. Thanks for a helpful answer! – Arvo Bowen Jan 25 '17 at 16:22
  • 1
    I just realized that this might return bad results and should probably not be used at all. When I was using this I hit the `NumberPad 8` key and it shows up as such as KeyData and KeyCode with a KeyValue of `104`. When using `(char)e.KeyValue` on the other hand the value `104` converts to character `h` and NOT `8`! – Arvo Bowen Jan 25 '17 at 17:37
  • The best method I have found was located at http://stackoverflow.com/a/35377638/1039753 – Arvo Bowen Jan 25 '17 at 17:46