Example #1
0
        internal static bool EqualsNormalized(this ConsoleKeyInfo x, ConsoleKeyInfo y)
        {
            // In the common case, we mask something out of the ConsoleKeyInfo
            // e.g. Shift or somewhat meaningless Key (like Oem6) which might vary
            // in different keyboard layouts.
            //
            // That said, if all fields compare, it's a match and we return that.
            if (x.Key == y.Key && x.KeyChar == y.KeyChar && x.Modifiers == y.Modifiers)
            {
                return(true);
            }

            // We ignore Shift state as that can vary in different keyboard layouts.
            var xMods = NormalizeModifiers(x);
            var yMods = NormalizeModifiers(y);

            if (xMods != yMods)
            {
                return(false);
            }

            // If we don't have a character, we probably masked it out (except for Ctrl+@)
            // when building our key bindings, so compare Key instead.
            return(x.IgnoreKeyChar() || y.IgnoreKeyChar()
                ? x.Key == y.Key
                : x.NormalizeKeyChar() == y.NormalizeKeyChar());
        }
Example #2
0
        private static ConsoleModifiers NormalizeModifiers(this ConsoleKeyInfo key)
        {
            var keyChar = key.IgnoreKeyChar() ? key.KeyChar : key.NormalizeKeyChar();
            var result  = key.Modifiers;

            if (!char.IsControl(keyChar))
            {
                // Ignore Shift state unless it's a control character.
                result = result & ~ConsoleModifiers.Shift;
            }
            return(result);
        }
Example #3
0
        internal static int GetNormalizedHashCode(this ConsoleKeyInfo obj)
        {
            // Because a comparison of two ConsoleKeyInfo objects is a comparison of the
            // combination of the ConsoleKey and Modifiers, we must combine their hashes.
            // Note that if the ConsoleKey is default, we must fall back to the KeyChar,
            // otherwise every non-special key will compare as the same.
            int h1 = obj.IgnoreKeyChar()
                ? obj.Key.GetHashCode()
                : obj.NormalizeKeyChar().GetHashCode();

            int h2 = NormalizeModifiers(obj).GetHashCode();

            // This is based on Tuple.GetHashCode
            return(unchecked (((h1 << 5) + h1) ^ h2));
        }
Example #4
0
        public static string ToGestureString(this ConsoleKeyInfo key)
        {
            var useKeyEnum = key.IgnoreKeyChar();

            var mods = key.Modifiers;

            var sb = new StringBuilder();

            var isShift = (mods & ConsoleModifiers.Shift) != 0;
            var isCtrl  = (mods & ConsoleModifiers.Control) != 0;
            var isAlt   = (mods & ConsoleModifiers.Alt) != 0;

            if (useKeyEnum && isShift)
            {
                sb.Append("Shift");
            }

            if (isCtrl)
            {
                if (sb.Length > 0)
                {
                    sb.Append("+");
                }
                sb.Append("Ctrl");
            }

            if (isAlt)
            {
                if (sb.Length > 0)
                {
                    sb.Append("+");
                }
                sb.Append("Alt");
            }

            if (sb.Length > 0)
            {
                sb.Append("+");
            }

            if (useKeyEnum)
            {
                sb.Append(key.Key);
            }
            else
            {
                var    c = key.NormalizeKeyChar();
                string s;
                switch (c)
                {
                case ' ': s = "Space";     break;

                case '\x1b': s = "Escape";    break;

                case '\x1c': s = "\\";        break;

                case '\x1d': s = "]";         break;

                case '\x1f': s = "_";         break;

                case '\x7f': s = "Backspace"; break;

                case '\x08':
                    s = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Backspace" : "Ctrl+Backspace";
                    break;

                case char _ when c <= 26:
                    s = ((char)((isShift ? 'A' : 'a') + c - 1)).ToString();
                    break;

                default:
                    s = c.ToString();
                    break;
                }
                sb.Append(s);
            }

            return(sb.ToString());
        }