Beispiel #1
0
        private void LogKey(ConsoleKeyInfo key)
        {
            var x = ConsoleOutput.CursorLeft;
            var y = ConsoleOutput.CursorTop;

            var fgColor = ConsoleOutput.ForegroundColor;
            var bgColor = ConsoleOutput.BackgroundColor;

            try
            {
                ConsoleOutput.SetCursorPosition(0, 0);
                ConsoleOutput.ForegroundColor = ConsoleColor.Yellow;
                ConsoleOutput.BackgroundColor = ConsoleColor.DarkBlue;

                var builder = new StringBuilder();
                builder.AppendFormat("[Key: {{{0, -12}}}] ", key.Key);

                if (char.IsControl(key.KeyChar))
                {
                    builder.AppendFormat("[Char: 0x{0:X}] ", (int)key.KeyChar);
                }
                else if (key.KeyChar != (char)0)
                {
                    builder.AppendFormat("[Char: '{0}' : 0x{1:X}] ", key.KeyChar, (int)key.KeyChar);
                }

                var modifiers            = (ConsoleModifiers)0;
                var translationModifiers = (ConsoleModifiers)0;
                var modifierNames        = new List <string>();

                if (key.Modifiers.HasFlag(ConsoleModifiers.Control))
                {
                    modifiers |= ConsoleModifiers.Control;
                    modifierNames.Add("Ctrl");
                }

                if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
                {
                    modifiers |= ConsoleModifiers.Alt;
                    modifierNames.Add("Alt");
                }

                if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
                {
                    modifiers            |= ConsoleModifiers.Shift;
                    translationModifiers |= ConsoleModifiers.Shift;
                }

                var translatedToChars = false;
                if (modifiers.HasFlag(ConsoleModifiers.Alt) || modifiers.HasFlag(ConsoleModifiers.Control))
                {
                    var chars = InputUtilities.GetChars(key.Key, translationModifiers);
                    if (chars.Length > 0)
                    {
                        var charsAsString = new string(chars);
                        builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), charsAsString);
                        translatedToChars = true;
                    }
                }

                if (!translatedToChars)
                {
                    if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
                    {
                        modifierNames.Add("Shift");
                    }

                    if (modifierNames.Count > 0)
                    {
                        builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), key.Key);
                    }
                }

                if (builder.Length < ConsoleOutput.BufferWidth)
                {
                    builder.Append(new string(' ', ConsoleOutput.BufferWidth - builder.Length));
                }

                ConsoleOutput.Write(builder.ToString());
            }
            finally
            {
                ConsoleOutput.ForegroundColor = fgColor;
                ConsoleOutput.BackgroundColor = bgColor;
                ConsoleOutput.SetCursorPosition(x, y);
            }
        }
        private void LogKey(ConsoleKeyInfo key)
        {
            var builder = new StringBuilder();

            builder.AppendFormat("[Key: {{{0, -12}}}] ", key.Key);

            if (char.IsControl(key.KeyChar))
            {
                builder.AppendFormat("[Char: 0x{0:X}] ", (int)key.KeyChar);
            }
            else if (key.KeyChar != (char)0)
            {
                builder.AppendFormat("[Char: '{0}' : 0x{1:X}] ", key.KeyChar, (int)key.KeyChar);
            }

            var modifiers            = (ConsoleModifiers)0;
            var translationModifiers = (ConsoleModifiers)0;
            var modifierNames        = new List <string>();

            if (key.Modifiers.HasFlag(ConsoleModifiers.Control))
            {
                modifiers |= ConsoleModifiers.Control;
                modifierNames.Add("Ctrl");
            }

            if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
            {
                modifiers |= ConsoleModifiers.Alt;
                modifierNames.Add("Alt");
            }

            if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
            {
                modifiers            |= ConsoleModifiers.Shift;
                translationModifiers |= ConsoleModifiers.Shift;
            }

            var translatedToChars = false;

            if (modifiers.HasFlag(ConsoleModifiers.Alt) || modifiers.HasFlag(ConsoleModifiers.Control))
            {
                var chars = InputUtilities.GetChars(key.Key, translationModifiers);
                if (chars.Length > 0)
                {
                    var charsAsString = new string(chars);
                    builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), charsAsString);
                    translatedToChars = true;
                }
            }

            if (!translatedToChars)
            {
                if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
                {
                    modifierNames.Add("Shift");
                }

                if (modifierNames.Count > 0)
                {
                    builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), key.Key);
                }
            }

            if (builder.Length < ConsoleOutput.BufferWidth)
            {
                builder.Append(new string(' ', ConsoleOutput.BufferWidth - builder.Length));
            }

            _statusBar.Set(builder.ToString());
        }