views:

50

answers:

3

If I use the following code:

for (int i = 0; i < text.Length; i++)
    {
        char c = text[i];
        Keys k = (Keys)(byte)c;
        MessageBox.Show(c.ToString() + "|" + k.ToString());
    }

I can get a correct conversion for uppercase letters only. The problem is, I need to be able to replicate lower case characters as well, and I am getting conversion errors on them. For instance, 'e' converts to 'NumPad5', where 'E' converts correctly to 'E'. How do I attack this? I'm going to be taking input strings and creating virtual key presses for a macro player I am trying to develop.

+4  A: 

That seems like the wrong approach. Have you considered http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx ?

Jonathan
I'd give you +1 million if I could. That is a LOT easier than I was thinking it could be. I just used `SendKeys.SendWait(text);` and it acted exactly as I wanted!
manyxcxi
great, glad I could help. :)
Jonathan
+1  A: 

The Keys enumeration is not a straight copy of the character values of the character generated when the key is pressed. Sometimes it is, but sometimes it is not. The way the value is encoded for each keypress is described in the documentation:

This class contains constants to use for processing keyboard input. Keys are identified by key values, which consist of a key code and a set of modifiers combined into a single integer value. The four left digits of a key value contain the key code (which is the same as a Windows virtual key code). The four right digits of a key value contain modifier bits for the SHIFT, CONTROL, and ALT keys.

Mark Byers
A: 

Look at this: http://stackoverflow.com/questions/544141/how-to-convert-a-character-in-to-equivalent-system-windows-input-key-enum-value

AS-CII
It converts all my lowercase to uppercase and drops my uppercase chars...
manyxcxi