tags:

views:

358

answers:

5

Hello everyone,

How can I convert backslash key ('\') to key code?

On my keyboard backslash code is 220, but the method below

(int)'\\'

returns me 92.

I need some generic conversion like

 int ConvertCharToKeyValue(char c)
 {
     // some code here...
 }

Any ideas?

A: 

It depends on the ASCII table you are using.

try the following code:

System.Text.ASCIIEncoding.ASCII.GetBytes(myString);
Rodrigo
Unfortunatelly this returns me 92 instead of 220.I get 220 code key when I hooked KeyDown method of the textbox:public void TextBox_KeyDown(object sender, KeyEventArgs e){ int keycode = e.KeyCode;}
Murat
Sorry, I thought you wanted the ASCII conversion, but this is not the case.For your case I think you can take the vlue from the following enumeration:System.Windows.Forms.Keys.*
Rodrigo
A: 

Have you seen this answer? http://stackoverflow.com/questions/318777/c-how-to-translate-virtual-keycode-to-char

drachenstern
Yes. I need converting from char to int. And I need a platform-independed solution. Without WM_Messages...
Murat
I seem to be under the belief that there is no such "platform independent" for keyboard mangling. Consider a Mac keyboard does not have a Windows key, and a Windows keyboard does not have a Command key. Linux has neither, per se. All three can and do recognize the other keys, but the mappings are not consistent. Or are you ignoring modifiers in your application? That could present other difficulties. ~ However, as always, I may be wrong on my understanding. YMMV. HTTH.
drachenstern
A: 

There is not a function that I know of that will map a character to a virtual key code. However, you can use the following table to start building such a mapping.

http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx.

Note that you will need to know the keyboard, looking at the key you mention '\' this is the VK_OEM_5 virtual key which for US keyboards is '\' if not shifted and '|' if shifted, so your function will need to know the keyboard being used as well.

Of course if you want to map from a virtual key code to a character you can use interop to call the MapVirtualKeyEx function.

Update Based on your comment this would give you what you want.

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

const uint MAPVK_VK_TO_CHAR = 0x02;

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
  int key = MapVirtualKey((int)e.KeyCode, MAPVK_VK_TO_CHAR);
  if (key == (int)'\\')
  {

  }
}
Chris Taylor
Thanks for the link. I read:VK_OEM_5: 0xDC: 220Used for miscellaneous characters; it can vary by keyboard.~~~~~~~~~~~~~~~~~~~~~~~For the US standard keyboard, the '\|' keyI need the keyboard-independed solution. Probably the best solution will be something like:public void TextBox_KeyDown(object sender, KeyEventArgs e) { if ((String.Parse(e.KeyCode).EndsWith("\\"))...}
Murat
@Murat, there is no String.Parse() function. I have added a sample that should help with what you seem to want to do here.
Chris Taylor
+2  A: 

You can P/Invoke VkKeyScan() to convert a typing key code back to a virtual key. Beware that the modifier key state is important, getting "|" requires holding down the shift key on my keyboard layout. Your function signature doesn't allow for this so I just made something up:

    public static Keys ConvertCharToVirtualKey(char ch) {
        short vkey = VkKeyScan(ch);
        Keys retval = (Keys)(vkey & 0xff);
        int modifiers = vkey >> 8;
        if ((modifiers & 1) != 0) retval |= Keys.Shift;
        if ((modifiers & 2) != 0) retval |= Keys.Control;
        if ((modifiers & 4) != 0) retval |= Keys.Alt;
        return retval;
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern short VkKeyScan(char ch);

Also beware of keyboard layouts that need to use dead keys (Alt+Gr) to generate typing keys. This kind of code is really best avoided.

Hans Passant
A: 

If

var char = System.Windows.Forms.Keys.OemPipe; // 220
var code = (int)char;

then

public static int ToKeyValue(this char ch)
{
    return (int)(System.Windows.Forms.Keys)ch;
}
abatishchev
I was under the impression that `OemPipe` would be `|` and `OemBackslash` would be `\\`
R. Bemrose
@R. Bemrose: Yes it would be. But when I press '\' on keyboard (which is under Backspace and above Enter) my test textbox fires `KeyUp` event where `KeyCode = 220` which OP is looking for
abatishchev