views:

7317

answers:

5

I am trying to map a virtual keycode to a char.

My code uses ProcessCmdKey to listen to WM_KEYDOWN which gives me access to the key pressed. For example, when I press single quote I get a key of 222 which I want to have it mapped to keychar 39 which represents... you guessed it... single quote.

My dev context is: - .net Framework 2.0 - UserControl placed in a lot of places

Do you know the answer to the question?

+2  A: 

MapVirtualKey() ?

plinth
Too short, needed the details I posted. Thanks anyway!
Horas
+1  A: 

Assuming you are working on a windows client you might want to see this tutorial from MSDN How to: Handle Keyboard Input at the Form Level

Perpetualcoder
The TreeView from Microsoft is not catching the label KeyPress (Down, Up) event. What you suggest does not work, thanks anyway!
Horas
+4  A: 

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!

Horas
Thanks for following up. If you had already looked at MapVirtualKey() perhaps you should've included that in your question (ie, "I've looked at MapVirtualKey() but I don't know how to call it from C#). For your future needs, you might find http://www.pinvoke.net useful.
plinth
And in the case of MapVirtualKey, here's the entry from pinvoke.net: http://www.pinvoke.net/default.aspx/user32/MapVirtualKey.html
plinth
Minor correction: MapVirtualKey actually returns a uint, not an int. But thank you for that bit of code, it was exactly what I needed for my own project.
Nick
+1  A: 

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

KeysConverter kc = new KeysConverter();
string keyChar = kc.ConvertToString(keyData);
R. Bemrose
Forgot to mention the context of my question:- .net Framework 2.0- UserControlSo the answer to your answer is no.
Horas
A: 

KeysConverter gets the key name not the keys "text" ex: "Num2" instead of "2" MapVirtualKey will work for english but for non-english chars documentation states using MapVirtualKeyEx but that requires a locale Identifier that identifier is loaded by LoadKeyBoardLayout which requires a culture id constant but then after finding the correct id values it didn't work as i tried it so finally i dumped the whole thing

mm