C# How to translate virtual keycode to char?

Solution 1:

Isn't that what the System.Windows.Form.KeysConverter class is for?

KeysConverter kc = new KeysConverter();
string keyChar = kc.ConvertToString(keyData);

Solution 2:

Yes, I did use the MapVirtualKey method. But I was expecting more details on how to use it: what DllImport directive to use, what enum is specific for mapping to characters, etc.

I don't like these answers where you google for like 5 seconds and then just suggest a solution: the real challenge is to put all the pieces together and not have to waste your time with tons of sample-less MSDN pages or other coding forums in order to get your answer. No offense plinth, but your answer (even good) was worhtless since I had this answer even before posting my question on the forum!

So there you go, I am going to post what I was looking for - an out-of-the-box C# solution:

1- Place this directive inside your class:

[DllImport("user32.dll")]
static extern int MapVirtualKey(uint uCode, uint uMapType);

2- Retrieve your char like this:

  protected override bool ProcessCmdKey(ref Message msg, Keys keyData)      
  {
     const int WM_KEYDOWN = 0x100;

     if (msg.Msg == WM_KEYDOWN)
     {            
        // 2 is used to translate into an unshifted character value 
        int nonVirtualKey = MapVirtualKey((uint)keyData, 2);

        char mappedChar = Convert.ToChar(nonVirtualKey);
     }

     return base.ProcessCmdKey(ref msg, keyData);
  }

Thanks for caring... and enjoy!