如何在 Mongame 中正确地将数字键转换为字符串?

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

我正在尝试实时更新播放器文本输入。

在 Monogame / XNA 中,Key class 确实有 toString() 方法,但他们的 return 并不是我想要的。例如,如果我在 QWERTY 键盘上按 2(Q 和 W 上面的 2),Key 值将是 D2,这是可以理解的。但是当我调用 toString() 时,我仍然会得到 "D2".

当然,我可以截断第一个字符以仅获取数字,但是有没有其他方法,比如 XNA 方法,可以 return 我 "2" 当给定 Key.D2?

没有 build-in XNA/.Net 方法自动将每个自定义字符串映射到键枚举。如果您使用 non-QWERTY 键盘(例如法语 AZERTY),则尤其如此。因此,您必须制作一个 class 将每个 Key 映射到您想要显示的字符串。这是一个例子。

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;
        }
    }