james_lohr's picture

Text-input, shift modifier.

I'd like to have text input within my game, and so I'd like to map key presses to their corresponding characters, complete with shift modifiers.

I can do this manually by mapping ranges to keys, e.g. :

            char keyChar;
            kval = (int)e.Key;
 
            if (kval >= (int)Key.A && kval <= (int)Key.Z)
 
                int val = kval - (int)Key.Number0;
 
 
                if (!shiftDown)
                {
                    val += (int)'a';
                }
                else
                {
                    val += (int)'A';
                }
 
               keyChar = (char)val;
            }

However, it's a bit painful to do this for every key on the keyboard, plus I'm sure it may vary depending on keyboard.

Is there a correct/better way of doing this?

Cheers,

James L.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

The KeyPress event does this translation for you.

james_lohr's picture

So it does! Thank you, that was easy. :)

I hadn't spotted it because I was expecting it to be in OpenTK.Input, but I appreciate that it depends on the native window and therefore needs to live where it is.