15

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?

Murat Korkmaz
  • 1,329
  • 2
  • 19
  • 37
  • Did you ever get this resolved successfully? Do you still need help with this? – jcolebrand Dec 14 '10 at 04:21
  • Have you seen this answer? http://stackoverflow.com/questions/318777/c-how-to-translate-virtual-keycode-to-char – jcolebrand May 24 '10 at 17:23
  • Yes. I need converting from char to int. And I need a platform-independed solution. Without WM_Messages... – Murat Korkmaz May 24 '10 at 17:29
  • 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. – jcolebrand May 24 '10 at 17:52

4 Answers4

22

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.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Or you could keep it simple and cast it.

(Keys)keyToConvert
Pang
  • 9,564
  • 146
  • 81
  • 122
1

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
  • 98,240
  • 88
  • 296
  • 433
  • I was under the impression that `OemPipe` would be `|` and `OemBackslash` would be `\\` – Powerlord May 24 '10 at 19:06
  • @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 May 24 '10 at 19:10
0

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
  • 52,623
  • 10
  • 78
  • 89
  • Thanks for the link. I read: VK_OEM_5: 0xDC: 220 Used for miscellaneous characters; it can vary by keyboard. ~~~~~~~~~~~~~~~~~~~~~~~ For the US standard keyboard, the '\|' key I 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 Korkmaz May 24 '10 at 17:35
  • @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 May 24 '10 at 17:48