简体   繁体   English

MonoGame 按键按下字符串

[英]MonoGame Key Pressed String

In MonoGame, how can I read which keyboard key is pressed in the form of a String?在 MonoGame 中,如何以字符串的形式读取按下了哪个键盘键?

I have tried String pressedKey = Keyboard.GetState().ToString();我试过String pressedKey = Keyboard.GetState().ToString(); , but it gives me "Microsoft.Xna.Framework.Input.KeyboardState". ,但它给了我“Microsoft.Xna.Framework.Input.KeyboardState”。

In games, you typically think of keys as buttons that have state rather than strings because you are usually checking if a button is up or down to move a character around, shoot, jump, etc.在游戏中,您通常将键视为具有状态而非字符串的按钮,因为您通常会检查按钮是向上还是向下以移动角色、射击、跳跃等。

As others have said, you said you should use IsKeyDown and IsKeyUp if you already know what keys you want to test.正如其他人所说,如果您已经知道要测试的键,您应该使用 IsKeyDown 和 IsKeyUp。 However, sometimes you just want to know what keys are being pressed.但是,有时您只想知道按下了哪些键。 For that, you can use the GetPressedKeys method which will give you an array of keys currently pressed on the keyboard.为此,您可以使用 GetPressedKeys 方法,该方法将为您提供当前在键盘上按下的键的数组。

If you wanted to turn those keys into a string, you could probably do something like this in your update method.如果您想将这些键转换为字符串,您可以在 update 方法中执行类似的操作。

    private string _stringValue = string.Empty;

    protected override void Update(GameTime gameTime)
    {
        var keyboardState = Keyboard.GetState();
        var keys = keyboardState.GetPressedKeys();

        if(keys.Length > 0)
        {
            var keyValue = keys[0].ToString();
            _stringValue += keyValue;
        }

        base.Update(gameTime);
    }

However, keep in mind that this won't be perfect.但是,请记住,这不会是完美的。 You'd have to add extra code to handle the SHIFT key for upper and lowercase letters and you would want to filter out other keys like Ctrl and Alt.您必须添加额外的代码来处理大写和小写字母的 SHIFT 键,并且您需要过滤掉其他键,例如 Ctrl 和 Alt。 Perhaps restrict the input to only alphanumeric values and so on..也许将输入限制为仅字母数字值等等..

Good luck.祝你好运。

Too little rep to comment but if you only want to allow AZ as input and you're using Monogame (and XNA maybe) you can look at the Keys enum and see if the key value you're testing for is within that range.评论太少,但如果您只想允许 AZ 作为输入并且您正在使用 Monogame(可能还有 XNA),您可以查看 Keys 枚举,看看您正在测试的键值是否在该范围内。

Something along the lines of类似的东西

if((int)key > 64 && (int)key <  91)
   {
      keyValue = key.ToString();
      this.EnteredString += keyValue;
   }

Where 64 is the key below A in the enum and 91 is the key above z in the enum :)其中 64 是枚举中 A 下方的键,91 是枚举中 z 上方的键:)

You can allow numbers with the same idea :您可以允许具有相同想法的数字:

else if ((int)key > 47 && (int)key < 58)
     {
     keyValue = key.ToString();
     keyValue = keyValue.TrimStart('D');

     this.EnteredString += keyValue;
     }

and will trim the unwanted "D" character which shows up并将修剪出现的不需要的“D”字符

You don't need to use the .ToString() function to find out what key(s) are pressed.您不需要使用.ToString()函数来找出按下了哪些键。

You need to track the KeyboardState in order to find out which key(s) are/were pressed.您需要跟踪KeyboardState以找出按下/按下了哪些键。 eg例如

KeyboardState ks = Keyboard.GetState()
if (ks.IsKeyDown(Keys.W)) {
   // Move forward
}
if (ks.IsKeyDown(Keys.S)) {
   // Move backward
}
if (ks.IsKeyDown(Keys.A)) {
   // Move left
}
if (ks.IsKeyDown(Keys.D)) {
   // Move right
}

I know this is answered already, but I found this topic here, and did a little refactoring in some code there:我知道这已经得到了回答,但我在这里找到了这个话题,并在那里对一些代码进行了一些重构:

public class KeyboardStringReader
    {
        private KeyboardState currentKeyboardState;
        private KeyboardState oldKeyboardState;

        public string TextString { get; set; }
        public bool IsFinished { get; set; }
        public KeyboardStringReader()
        {
            TextString = string.Empty;
            IsFinished = false;
        }

        public void UpdateInput()
        {
            if (!this.IsFinished)
            {
                oldKeyboardState = currentKeyboardState;
                currentKeyboardState = Keyboard.GetState();

                Keys[] pressedKeys;
                pressedKeys = currentKeyboardState.GetPressedKeys();

                foreach (Keys key in pressedKeys)
                {
                    if (oldKeyboardState.IsKeyUp(key))
                    {
                        if (key == Keys.Back && TextString.Length > 0)
                        {
                            TextString = TextString.Remove(TextString.Length - 1, 1);
                        }
                        else if (key == Keys.Space)
                        {
                            TextString = TextString.Insert(TextString.Length, " ");
                        }
                        else if (key == Keys.Enter)
                        {
                            this.IsFinished = true;
                        } 
                        else
                        {
                            string keyString = key.ToString();
                            bool isUpperCase = ((Control.IsKeyLocked(System.Windows.Forms.Keys.CapsLock) &&
                                                 (!currentKeyboardState.IsKeyDown(Keys.RightShift) &&
                                                  !currentKeyboardState.IsKeyDown(Keys.LeftShift))) ||
                                                (!Control.IsKeyLocked(System.Windows.Forms.Keys.CapsLock) &&
                                                 (currentKeyboardState.IsKeyDown(Keys.RightShift) ||
                                                  currentKeyboardState.IsKeyDown(Keys.LeftShift))));

                            if (keyString.Length == 1)
                            {
                                TextString += isUpperCase ? keyString.ToUpper() : keyString.ToLower();
                            }
                        }
                    }
                }
            }
        } 
    }

It's important to note that I needed a reference to windows.forms for the upper case stuff(I'm sure there's a better way, but i'm in a hurry here so :P )重要的是要注意,对于大写的东西,我需要对 windows.forms 的引用(我确定有更好的方法,但我很着急,所以 :P )

I don't know if C# 7 will allow pattern matching like this but other F# users might also look for something like this like I did.我不知道 C# 7 是否允许这样的模式匹配,但其他 F# 用户可能也会像我一样寻找这样的东西。

override this.Update(gameTime) =
  let HandleKeys K =
         match K with
         | Keys.Down -> DoSomething
         | Keys.Up -> DoSomethingElse
         | Keys.Left -> DoYetSomethingElse
         | Keys.Right->  DoAnythingOtherThanWhatWeDidAlready

  if Keyboard.GetState().GetPressedKeys().Length > 0 then HandleKeys (Keyboard.GetState().GetPressedKeys().[0]) |> ignore

I spent some time writing this for one of my projects.我花了一些时间为我的一个项目写这篇文章。 Have a look over it I think one of the keys may be wrong but here it is.看看它,我认为其中一个键可能是错误的,但它在这里。

using Microsoft.Xna.Framework.Input;

namespace EEngine.IO
{
static class EKeyboard
{
    public static Keys GetKeyFromString(string keyStr)
    {
        MonoGame.Framework.Keys key;

        switch (keyStr.ToUpper())
        {
            // Letters
            case "Q":
                key = Keys.Q;
                return key;
            case "W":
                key = Keys.W;
                return key;
            case "E":
                key = Keys.E;
                return key;
            case "R":
                key = Keys.R;
                return key;
            case "T":
                key = Keys.T;
                return key;
            case "Y":
                key = Keys.Y;
                return key;
            case "U":
                key = Keys.U;
                return key;
            case "I":
                key = Keys.I;
                return key;
            case "O":
                key = Keys.O;
                return key;
            case "P":
                key = Keys.P;
                return key;
            case "A":
                key = Keys.A;
                return key;
            case "S":
                key = Keys.S;
                return key;
            case "D":
                key = Keys.D;
                return key;
            case "F":
                key = Keys.F;
                return key;
            case "G":
                key = Keys.G;
                return key;
            case "H":
                key = Keys.H;
                return key;
            case "J":
                key = Keys.J;
                return key;
            case "K":
                key = Keys.K;
                return key;
            case "L":
                key = Keys.L;
                return key;
            case "Z":
                key = Keys.Z;
                return key;
            case "X":
                key = Keys.X;
                return key;
            case "C":
                key = Keys.C;
                return key;
            case "V":
                key = Keys.V;
                return key;
            case "B":
                key = Keys.B;
                return key;
            case "N":
                key = Keys.N;
                return key;
            case "M":
                key = Keys.M;
                return key;

            // TOP ROW F# KEYS ETC.
            case "ESCAPE":
                key = Keys.Escape;
                return key;
            case "F1":
                key = Keys.F1;
                return key;
            case "F2":
                key = Keys.F2;
                return key;
            case "F3":
                key = Keys.F3;
                return key;
            case "F4":
                key = Keys.F4;
                return key;
            case "F5":
                key = Keys.F5;
                return key;
            case "F6":
                key = Keys.F6;
                return key;
            case "F7":
                key = Keys.F7;
                return key;
            case "F8":
                key = Keys.F8;
                return key;
            case "F9":
                key = Keys.F9;
                return key;
            case "F10":
                key = Keys.F10;
                return key;
            case "F11":
                key = Keys.F11;
                return key;
            case "F12":
                key = Keys.F12;
                return key;
            case "PRINTSCR":
                key = Keys.PrintScreen;
                return key;
            case "SCROLLLOCK":
                key = Keys.Scroll;
                return key;
            case "PAUSE":
                key = Keys.Pause;
                return key;

            // SECOND ROW NUMBERS, ETC
            case "`":
                key = Keys.OemTilde;
                return key;
            case "1":
                key = Keys.D1;
                return key;
            case "2":
                key = Keys.D2;
                return key;
            case "3":
                key = Keys.D3;
                return key;
            case "4":
                key = Keys.D4;
                return key;
            case "5":
                key = Keys.D5;
                return key;
            case "6":
                key = Keys.D6;
                return key;
            case "7":
                key = Keys.D7;
                return key;
            case "8":
                key = Keys.D8;
                return key;
            case "9":
                key = Keys.D9;
                return key;
            case "0":
                key = Keys.D0;
                return key;
            case "-":
                key = Keys.OemMinus;
                return key;
            case "+":
                key = Keys.OemPlus;
                return key;
            case "BACKSPACE":
                key = Keys.Back;
                return key;
            case "INSERT":
                key = Keys.Insert;
                return key;
            case "HOME":
                key = Keys.Home;
                return key;
            case "PGUP":
                key = Keys.PageUp;
                return key;
            case "NUMLOCK":
                key = Keys.NumLock;
                return key;
            case "/":
                key = Keys.OemPipe; // <- NEED TO VERIFY
                return key;
            case "*":
                key = Keys.Multiply;
                return key;
            case "TAB":
                key = Keys.Tab;
                return key;
            case "ENTER":
                key = Keys.Enter;
                return key;
            case "DELETE":
                key = Keys.Delete;
                return key;
            case "END":
                key = Keys.End;
                return key;
            case "PGDN":
                key = Keys.PageDown;
                return key;
            case "CAPS":
                key = Keys.CapsLock;
                return key;
            case "LSHIFT":
                key = Keys.LeftShift;
                return key;
            case "RSHIFT":
                key = Keys.RightShift;
                return key;
            case "LCTRL":
                key = Keys.LeftControl;
                return key;
            case "LALT":
                key = Keys.LeftAlt;
                return key;
            case "RALT":
                key = Keys.RightAlt;
                return key;
            case "RCTRL":
                key = Keys.RightControl;
                return key;
            case "SPACE":
                key = Keys.Space;
                return key;
            case "KP_INS":
                key = Keys.NumPad0;
                return key;
            case "KP_END":
                key = Keys.NumPad1;
                return key;
            case "KP_DOWNARROW":
                key = Keys.NumPad2;
                return key;
            case "KP_PGDN":
                key = Keys.NumPad3;
                return key;
            case "KP_LEFTARROW":
                key = Keys.NumPad4;
                return key;
            case "KP_5":
                key = Keys.NumPad5;
                return key;
            case "KP_RIGHTARROW":
                key = Keys.NumPad6;
                return key;
            case "KP_HOME":
                key = Keys.NumPad7;
                return key;
            case "KP_UPARROW":
                key = Keys.NumPad8;
                return key;
            case "KP_PGUP":
                key = Keys.NumPad9;
                return key;
            case "UP":
                key = Keys.Up;
                return key;
            case "DOWN":
                key = Keys.Down;
                return key;
            case "LEFT":
                key = Keys.Left;
                return key;
            case "RIGHT":
                key = Keys.Right;
                return key;
        }

        return Keys.OemQuestion; // <- RETURN QUESTION AS "NO SUCH KEY"
    }

    public static string GetStringFromKey(Keys key)
    {
        string keyStr = "?";

        switch (key)
        {
            // Letters
            case Keys.Q:
                keyStr = "Q";
                return keyStr;
            case Keys.W:
                keyStr = "W";
                return keyStr;
            case Keys.E:
                keyStr = "E";
                return keyStr;
            case Keys.R:
                keyStr = "R";
                return keyStr;
            case Keys.T:
                keyStr = "T";
                return keyStr;
            case Keys.Y:
                keyStr = "Y";
                return keyStr;
            case Keys.U:
                keyStr = "U";
                return keyStr;
            case Keys.I:
                keyStr = "I";
                return keyStr;
            case Keys.O:
                keyStr = "O";
                return keyStr;
            case Keys.P:
                keyStr = "P";
                return keyStr;
            case Keys.A:
                keyStr = "A";
                return keyStr;
            case Keys.S:
                keyStr = "S";
                return keyStr;
            case Keys.D:
                keyStr = "D";
                return keyStr;
            case Keys.F:
                keyStr = "F";
                return keyStr;
            case Keys.G:
                keyStr = "G";
                return keyStr;
            case Keys.H:
                keyStr = "H";
                return keyStr;
            case Keys.J:
                keyStr = "J";
                return keyStr;
            case Keys.K:
                keyStr = "K";
                return keyStr;
            case Keys.L:
                keyStr = "L";
                return keyStr;
            case Keys.Z:
                keyStr = "Z";
                return keyStr;
            case Keys.X:
                keyStr = "X";
                return keyStr;
            case Keys.C:
                keyStr = "C";
                return keyStr;
            case Keys.V:
                keyStr = "V";
                return keyStr;
            case Keys.B:
                keyStr = "B";
                return keyStr;
            case Keys.N:
                keyStr = "N";
                return keyStr;
            case Keys.M:
                keyStr = "M";
                return keyStr;

            // TOP ROW F# keyStrS ETC.
            case Keys.Escape:
                keyStr = "ESCAPE";
                return keyStr;
            case Keys.F1:
                keyStr = "F1";
                return keyStr;
            case Keys.F2:
                keyStr = "F2";
                return keyStr;
            case Keys.F3:
                keyStr = "F3";
                return keyStr;
            case Keys.F4:
                keyStr = "F4";
                return keyStr;
            case Keys.F5:
                keyStr = "F5";
                return keyStr;
            case Keys.F6:
                keyStr = "F6";
                return keyStr;
            case Keys.F7:
                keyStr = "F7";
                return keyStr;
            case Keys.F8:
                keyStr = "F8";
                return keyStr;
            case Keys.F9:
                keyStr = "F9";
                return keyStr;
            case Keys.F10:
                keyStr = "F10";
                return keyStr;
            case Keys.F11:
                keyStr = "F11";
                return keyStr;
            case Keys.F12:
                keyStr = "F12";
                return keyStr;
            case Keys.PrintScreen:
                keyStr = "PRINTSCR";
                return keyStr;
            case Keys.Scroll:
                keyStr = "SCROLLLOCK";
                return keyStr;
            case Keys.Pause:
                keyStr = "PAUSE";
                return keyStr;

            // SECOND ROW NUMBERS, ETC
            case Keys.OemTilde:
                keyStr = "`";
                return keyStr;
            case Keys.D1:
                keyStr = "1";
                return keyStr;
            case Keys.D2:
                keyStr = "2";
                return keyStr;
            case Keys.D3:
                keyStr = "3";
                return keyStr;
            case Keys.D4:
                keyStr = "4";
                return keyStr;
            case Keys.D5:
                keyStr = "5";
                return keyStr;
            case Keys.D6:
                keyStr = "6";
                return keyStr;
            case Keys.D7:
                keyStr = "7";
                return keyStr;
            case Keys.D8:
                keyStr = "8";
                return keyStr;
            case Keys.D9:
                keyStr = "9";
                return keyStr;
            case Keys.D0:
                keyStr = "0";
                return keyStr;
            case Keys.OemMinus:
                keyStr = "-";
                return keyStr;
            case Keys.OemPlus:
                keyStr = "+";
                return keyStr;
            case Keys.Back:
                keyStr = "BACKSPACE";
                return keyStr;
            case Keys.Insert:
                keyStr = "INSERT";
                return keyStr;
            case Keys.Home:
                keyStr = "HOME";
                return keyStr;
            case Keys.PageUp:
                keyStr = "PGUP";
                return keyStr;
            case Keys.NumLock:
                keyStr = "NUMLOCK";
                return keyStr;
            case Keys.OemPipe: // <- NEED TO VERIFY
                keyStr = "/";
                return keyStr;
            case Keys.Multiply:
                keyStr = "*";
                return keyStr;
            case Keys.Tab:
                keyStr = "TAB";
                return keyStr;
            case Keys.Enter:
                keyStr = "ENTER";
                return keyStr;
            case Keys.Delete:
                keyStr = "DELETE";
                return keyStr;
            case Keys.End:
                keyStr = "END";
                return keyStr;
            case Keys.PageDown:
                keyStr = "PGDN";
                return keyStr;
            case Keys.CapsLock:
                keyStr = "CAPS";
                return keyStr;
            case Keys.LeftShift:
                keyStr = "LSHIFT";
                return keyStr;
            case Keys.RightShift:
                keyStr = "RSHIFT";
                return keyStr;
            case Keys.LeftControl:
                keyStr = "LCTRL";
                return keyStr;
            case Keys.LeftAlt:
                keyStr = "LALT";
                return keyStr;
            case Keys.RightAlt:
                keyStr = "RALT";
                return keyStr;
            case Keys.RightControl:
                keyStr = "RCTRL";
                return keyStr;
            case Keys.Space:
                keyStr = "SPACE";
                return keyStr;
            case Keys.NumPad0:
                keyStr = "KP_INS";
                return keyStr;
            case Keys.NumPad1:
                keyStr = "KP_END";
                return keyStr;
            case Keys.NumPad2:
                keyStr = "KP_DOWNARROW";
                return keyStr;
            case Keys.NumPad3:
                keyStr = "KP_PGDN";
                return keyStr;
            case Keys.NumPad4:
                keyStr = "KP_LEFTARROW";
                return keyStr;
            case Keys.NumPad5:
                keyStr = "KP_5";
                return keyStr;
            case Keys.NumPad6:
                keyStr = "KP_RIGHTARROW";
                return keyStr;
            case Keys.NumPad7:
                keyStr = "KP_HOME";
                return keyStr;
            case Keys.NumPad8:
                keyStr = "KP_UPARROW";
                return keyStr;
            case Keys.NumPad9:
                keyStr = "KP_PGUP";
                return keyStr;
            case Keys.Up:
                keyStr = "UP";
                return keyStr;
            case Keys.Down:
                keyStr = "DOWN";
                return keyStr;
            case Keys.Left:
                keyStr = "LEFT";
                return keyStr;
            case Keys.Right:
                keyStr = "RIGHT";
                return keyStr;
        }

        return keyStr; // <- RETURN QUESTION AS "NO SUCH KEY"
    }
}
}

There are two methods - GetKeyFromString(string keyStr) and GetStringFromKey(Keys key) and you can call them from anywhere like有两种方法 - GetKeyFromString(string keyStr) 和 GetStringFromKey(Keys key),你可以从任何地方调用它们

EKeyboard.GetKeyFromString("A");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM