c# ascii to keycode?

1.9k views Asked by At
[DllImport("user32.dll")]
private static extern short VkKeyScan(char ch);

set = set + "\r\n";
int txL = set.Length;
for (int o = 1; o < (txL); o++)
{
    short vkey = VkKeyScan(set[o]); //MessageBox.Show(vkey.ToString());
    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;
}

This functions convert standard Alphabet and Keyboard Layout to KeyCode , how can i Convert all other Unicodes as Chinese and also symbols in ASCII ? it doesn't work also for shift keys : "! , @ , # , $ ..." So, How can combine ALT and other Key and then convert to KeyCode ? e.g ALT+33 = "!" and KeyCode = 0xF0

2

There are 2 answers

2
Scott Chamberlain On BEST ANSWER

The ALT code is just the decimal representation of the hex value of the Unicode symbol. For example if you check the official chart you will see that the value for ! is 0021 if you convert that from hex to decimal you get 33 and that is where the alt + numPad-3 + numPad-3 comes from.

You can get every possible Unicode keycode value from looking at the appropriate PDF from http://www.unicode.org/charts

3
fejesjoco On

There are many Unicode characters which have no corresponding keys. For example, I have a 105 key keyboard, while there are about a million different Unicode characters. Also the keycode-character mapping depends on the keyboard layout. So there is no general answer to your problem.