Search code examples
c#xnamonogame

How to properly convert keys of digits to string in Mongame?


I'm trying to update player text input in real-time.

In Monogame / XNA, the Key class does have toString() method, but their return is not exactly what I wish to have. For example, if I press 2 on a QWERTY keyboard (the 2 on top of Q and W), the Key value would be D2, this is understandable. But when I invoke toString(), I will still get "D2".

I can, of course, truncate the first character to get only the digit, but is there any other way, like an XNA method, that could return me "2" when given Key.D2?


Solution

  • There is no build-in XNA/.Net method that automatically maps each custom string to Key enums. This is especially true if you are working with non-QWERTY keyboards, such as the French AZERTY. Therefore, you must make a class that maps each Key to the string you want displayed. Here is an example.

    public  class KeyboardLayout
        {
            public  char? ToChar(Keys key, KeyboardModifiers modifiers = KeyboardModifiers.None)
            {
                var isShiftDown = (modifiers & KeyboardModifiers.Shift) == KeyboardModifiers.Shift;
                var isAltGrDown = false;
                if (!isShiftDown)
                    isAltGrDown = (modifiers & KeyboardModifiers.Alt) == KeyboardModifiers.Alt || (modifiers & KeyboardModifiers.Control) == KeyboardModifiers.Control;
    
                if (isAltGrDown)
                {
                    //international keyboards
                    if (key == Keys.V) return '@';
                    if (key == Keys.D2) return '@';
                    if (key == Keys.Q) return '@';
                }
    
                if (key == Keys.A) return isShiftDown ? 'A' : 'a';
                if (key == Keys.B) return isShiftDown ? 'B' : 'b';
                if (key == Keys.C) return isShiftDown ? 'C' : 'c';
                if (key == Keys.D) return isShiftDown ? 'D' : 'd';
                if (key == Keys.E) return isShiftDown ? 'E' : 'e';
                if (key == Keys.F) return isShiftDown ? 'F' : 'f';
                if (key == Keys.G) return isShiftDown ? 'G' : 'g';
                if (key == Keys.H) return isShiftDown ? 'H' : 'h';
                if (key == Keys.I) return isShiftDown ? 'I' : 'i';
                if (key == Keys.J) return isShiftDown ? 'J' : 'j';
                if (key == Keys.K) return isShiftDown ? 'K' : 'k';
                if (key == Keys.L) return isShiftDown ? 'L' : 'l';
                if (key == Keys.M) return isShiftDown ? 'M' : 'm';
                if (key == Keys.N) return isShiftDown ? 'N' : 'n';
                if (key == Keys.O) return isShiftDown ? 'O' : 'o';
                if (key == Keys.P) return isShiftDown ? 'P' : 'p';
                if (key == Keys.Q) return isShiftDown ? 'Q' : 'q';
                if (key == Keys.R) return isShiftDown ? 'R' : 'r';
                if (key == Keys.S) return isShiftDown ? 'S' : 's';
                if (key == Keys.T) return isShiftDown ? 'T' : 't';
                if (key == Keys.U) return isShiftDown ? 'U' : 'u';
                if (key == Keys.V) return isShiftDown ? 'V' : 'v';
                if (key == Keys.W) return isShiftDown ? 'W' : 'w';
                if (key == Keys.X) return isShiftDown ? 'X' : 'x';
                if (key == Keys.Y) return isShiftDown ? 'Y' : 'y';
                if (key == Keys.Z) return isShiftDown ? 'Z' : 'z';
    
                if (((key == Keys.D0) && !isShiftDown) || (key == Keys.NumPad0)) return '0';
                if (((key == Keys.D1) && !isShiftDown) || (key == Keys.NumPad1)) return '1';
                if (((key == Keys.D2) && !isShiftDown) || (key == Keys.NumPad2)) return '2';
                if (((key == Keys.D3) && !isShiftDown) || (key == Keys.NumPad3)) return '3';
                if (((key == Keys.D4) && !isShiftDown) || (key == Keys.NumPad4)) return '4';
                if (((key == Keys.D5) && !isShiftDown) || (key == Keys.NumPad5)) return '5';
                if (((key == Keys.D6) && !isShiftDown) || (key == Keys.NumPad6)) return '6';
                if (((key == Keys.D7) && !isShiftDown) || (key == Keys.NumPad7)) return '7';
                if (((key == Keys.D8) && !isShiftDown) || (key == Keys.NumPad8)) return '8';
                if (((key == Keys.D9) && !isShiftDown) || (key == Keys.NumPad9)) return '9';
    
                if ((key == Keys.D0) && isShiftDown) return ')';
                if ((key == Keys.D1) && isShiftDown) return '!';
                if ((key == Keys.D2) && isShiftDown) return '@';
                if ((key == Keys.D3) && isShiftDown) return '#';
                if ((key == Keys.D4) && isShiftDown) return '$';
                if ((key == Keys.D5) && isShiftDown) return '%';
                if ((key == Keys.D6) && isShiftDown) return '^';
                if ((key == Keys.D7) && isShiftDown) return '&';
                if ((key == Keys.D8) && isShiftDown) return '*';
                if ((key == Keys.D9) && isShiftDown) return '(';
    
                if (key == Keys.Space) return ' ';
                if (key == Keys.Tab) return '\t';
                if (key == Keys.Enter) return (char)13;
                if (key == Keys.Back) return (char)8;
    
                if (key == Keys.Add) return '+';
                if (key == Keys.Decimal) return '.';
                if (key == Keys.Divide) return '/';
                if (key == Keys.Multiply) return '*';
                if (key == Keys.OemBackslash) return '\\';
                if ((key == Keys.OemComma) && !isShiftDown) return ',';
                if ((key == Keys.OemComma) && isShiftDown) return '<';
                if ((key == Keys.OemOpenBrackets) && !isShiftDown) return '[';
                if ((key == Keys.OemOpenBrackets) && isShiftDown) return '{';
                if ((key == Keys.OemCloseBrackets) && !isShiftDown) return ']';
                if ((key == Keys.OemCloseBrackets) && isShiftDown) return '}';
                if ((key == Keys.OemPeriod) && !isShiftDown) return '.';
                if ((key == Keys.OemPeriod) && isShiftDown) return '>';
                if ((key == Keys.OemPipe) && !isShiftDown) return '\\';
                if ((key == Keys.OemPipe) && isShiftDown) return '|';
                if ((key == Keys.OemPlus) && !isShiftDown) return '=';
                if ((key == Keys.OemPlus) && isShiftDown) return '+';
                if ((key == Keys.OemMinus) && !isShiftDown) return '-';
                if ((key == Keys.OemMinus) && isShiftDown) return '_';
                if ((key == Keys.OemQuestion) && !isShiftDown) return '/';
                if ((key == Keys.OemQuestion) && isShiftDown) return '?';
                if ((key == Keys.OemQuotes) && !isShiftDown) return '\'';
                if ((key == Keys.OemQuotes) && isShiftDown) return '"';
                if ((key == Keys.OemSemicolon) && !isShiftDown) return ';';
                if ((key == Keys.OemSemicolon) && isShiftDown) return ':';
                if ((key == Keys.OemTilde) && !isShiftDown) return '`';
                if ((key == Keys.OemTilde) && isShiftDown) return '~';
                if (key == Keys.Subtract) return '-';
    
                return null;
            }
        }