Receive character typed instead of key pressed?

Hi, i was wondering if it was possible to receive the character that was typed instead of only the key that was pressed.

For example when you press the two keys which i have marked with a red 1 and then go to another process and press space(2) you will get a "~ ". Keyboard.GetState() only receives the space key.

In my Keyboard class, I stored all keys currently being held down to array of keys if not yet on held list and has some properties that can return all keys in array keys or in string:

Store keys being held down to __Lastkeys if not yet on list.

E.G: string mLastKeys = _Keyb.GetLastKeysToString();
tho pressing Allt + ~ will output : “LeftAltOemTilde

You can work around with collected held keys and return your desired char.

  var    mKeys = _Keyboard.GetLastKeys();
  string mKeyStr = "";              
   //
   for (int i = 0; i < mKeys.Length; i++)
   {
           if( mKeys[i] == Keys.OemTilde )
           {
               m_KeysStr += "~";
           }
           // Trap other special keys here
           else
           {
            m_KeysStr += mKeys[i].ToString();
           }
    }

d

        /// <summary>
        /// Get all last keys currently being pressed down or 
        /// being held from this frame
        /// </summary>
        /// <returns>Returns Keys</returns>
        public XFI.Keys[] GetLastKeys()
        {
            return __LastKeys;
        }

        /// <summary>
        /// Get all last keys currently being pressed down or being 
        /// held from this frame in string.
        /// </summary>
        /// <returns>Returns Keys in string</returns>
        public string GetLastKeysToString()
        {
            string m_KeysStr = "";
            //
            for (int i = 0; i < __LastKeys.Length; i++)
            {
                m_KeysStr += __LastKeys[i].ToString();
            }
            //
            return m_KeysStr;
        }

Keyboard.GetState(); only returns the keys on the keyboard which are pressed. It does not return characters that are being typed like: !"#¤&%&((/)@££$€$€{[]}. And Tilde cant be received if the user presses Alt + ~ in another window and then goes back to the game. One could make a giant switch for every single possible character but would still be kinda limited.

Anyways i found a way around it using User32.dll even though i would prefer to use monogame api if that was possible.
https://www.gamedev.net/forums/topic/457783-xna-getting-text-from-keyboard/?tab=comments#comment-4040190

My posted above works on me out of the box.

MonoGame has the GameWindow.TextInput event. It give you a character instead of a Keys member. In your Game1 class you can use the Window property to subscribe to it.

2 Likes

Thanks. That is what i was looking for.

1 Like