コード例 #1
0
        public static TypedKey GetTypedLetter(this KeyboardState keyboardState)
        {
            var keysDown = keyboardState.GetKeysDown();

            var typedKey = new TypedKey()
            {
                Key      = keysDown.Length > 0 ? keysDown[0] : Keys.None,
                CapsLock = keyboardState.CapsLock,
                String   = "",
            };

            if (keysDown.Length == 0)
            {
                return(typedKey);
            }

            var pressedKeys = keyboardState.GetPressedKeys();

            foreach (var pressedKey in pressedKeys)
            {
                switch (pressedKey)
                {
                case Keys.LeftShift:
                case Keys.RightShift:
                    typedKey.Shift = true;
                    break;

                case Keys.LeftControl:
                case Keys.RightControl:
                    typedKey.Ctrl = true;
                    break;

                case Keys.LeftAlt:
                case Keys.RightAlt:
                    typedKey.Alt = true;
                    break;
                }
            }

            if (typedKey.Key >= Keys.A && typedKey.Key <= Keys.Z)
            {
                typedKey.String = char.ConvertFromUtf32((int)typedKey.Key);
                if (typedKey.Shift == typedKey.CapsLock)
                {
                    typedKey.String = typedKey.String.ToLower();
                }
            }
            else if (typedKey.Key >= Keys.D0 && typedKey.Key <= Keys.D9)
            {
                typedKey.String = char.ConvertFromUtf32(((int)typedKey.Key - (int)Keys.D0) + '0');
            }
            else if (typedKey.Key >= Keys.NumPad0 && typedKey.Key <= Keys.NumPad9)
            {
                typedKey.String = char.ConvertFromUtf32(((int)typedKey.Key - (int)Keys.NumPad0) + '0');
            }
            else if (typedKey.Key == Keys.Space)
            {
                typedKey.String = " ";
            }

            return(typedKey);
        }