1
\$\begingroup\$

Been writing a class for a main menu, there's a play button and when pressed it goes to a window that asks for coordinates, sadly when writing with the keys and using the numbers the output is either D1, D2, D3... and so on, or NumPad1, NumPad2... and such. What I want is for the output to be just the number itself rather than the key name.

    private string Coordinates = string.Empty;
    private SpriteFont font;

    private Keys[] lastPressedKeys = new Keys[5];
    public void GetKeys()
    {
        KeyboardState kbState = Keyboard.GetState();

        Keys[] pressedKeys = kbState.GetPressedKeys();

        foreach (Keys key in lastPressedKeys)
        {
            if (!pressedKeys.Contains(key))
            {
                //key is no longer pressed
                OnKeyUp(key);
            }

        }
        foreach (Keys key in pressedKeys)
        {
            if (!lastPressedKeys.Contains(key))
            {
                OnKeyDown(key);
            }
        }
        lastPressedKeys = pressedKeys;
    }
    public void OnKeyUp(Keys key)
    {



    }

    public void OnKeyDown(Keys key)
    {
        KeyboardState kbState = Keyboard.GetState();

        if (key == Keys.Back && Coordinates.Length > 0)
        {
            Coordinates = Coordinates.Remove(Coordinates.Length - 1);
        }
        else
        {

            Coordinates += key.ToString();

        }

    }

Here's the whole class https://pastebin.com/ZJqXx0zc

is there something wrong with using ToString? Or should I be using something different for numbers?

\$\endgroup\$
1
  • \$\begingroup\$ Update I changed the 'key.ToString();' to 'Convert.ToInt32(key)' and it seems that it changes it into number values but this time its 49,50,51 where it should be 1,2,3 \$\endgroup\$
    – Pavvel
    Commented Nov 14, 2017 at 21:44

2 Answers 2

2
\$\begingroup\$

Since the Keys class is an enum, ToString simply converts the names into strings. Enums can't have names, like 1, 2, etc., so they were named as D1, D2, and so on.

The built-in KeyboardState class isn't a good way to manage text input for multiple reasons (including this one, and the fact, that it doesn't support foreign characters, such as ő, ö, á, é, etc. or even special characters, such as ˛, ˘ or °). You need to use something different, like this one.

\$\endgroup\$
2
  • \$\begingroup\$ Is there another mapping from key codes to text that you would recommend for this type of application? \$\endgroup\$
    – DMGregory
    Commented Nov 14, 2017 at 23:08
  • \$\begingroup\$ @DMGregory The link I posted contains code, that gives you a very convenient way to get that information, you just need to cast the result to a char. \$\endgroup\$
    – Bálint
    Commented Nov 14, 2017 at 23:17
1
\$\begingroup\$

I used this code:

string key = e.KeyCode.ToString();

if (key.Length > 1) key = key.Replace("NumPad", "").Replace("D", "");

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .