jeudi 17 avril 2014

C# Comment virtual keycode en char la traduction ? -Débordement de pile


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?




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


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



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!




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




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



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?



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


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


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!



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



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


0 commentaires:

Enregistrer un commentaire