/// <summary>
        /// Bind the specified key (with the specified modifiers) to the
        /// indicated operation.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="modifiers">The modifiers for the character.</param>
        /// <param name="op">If non-null, the operation to bind the key
        /// to; otherwise, unbinds the key.</param>
        public void Bind(ConsoleKey key, ConsoleModifiers modifiers, ConsoleInputOperation?op)
        {
            Dictionary <ConsoleKey, ConsoleInputOperation> bindings;

            if (modifiers.HasFlag(ConsoleModifiers.Control) && modifiers.HasFlag(ConsoleModifiers.Alt))
            {
                bindings = _controlAltKeyBindings;
            }
            else if (modifiers.HasFlag(ConsoleModifiers.Alt))
            {
                bindings = _altKeyBindings;
            }
            else if (modifiers.HasFlag(ConsoleModifiers.Control))
            {
                bindings = _controlKeyBindings;
            }
            else if (modifiers.HasFlag(ConsoleModifiers.Shift))
            {
                bindings = _shiftKeyBindings;
            }
            else
            {
                bindings = _plainKeyBindings;
            }

            if (op.HasValue)
            {
                bindings[key] = op.Value;
            }
            else if (bindings.ContainsKey(key))
            {
                bindings.Remove(key);
            }
        }
        /// <summary>
        /// Bind the specified character (with the specified modifiers) to the
        /// indicated operation.
        /// </summary>
        /// <param name="value">The character.</param>
        /// <param name="modifiers">The modifiers for the character.</param>
        /// <param name="op">If non-null, the operation to bind the character
        /// to; otherwise, unbinds the character.</param>
        public void Bind(char value, ConsoleModifiers modifiers, ConsoleInputOperation?op)
        {
            Dictionary <char, ConsoleInputOperation> bindings;

            if (modifiers.HasFlag(ConsoleModifiers.Control) && modifiers.HasFlag(ConsoleModifiers.Alt))
            {
                bindings = _controlAltCharBindings;
            }
            else if (modifiers.HasFlag(ConsoleModifiers.Alt))
            {
                bindings = _altCharBindings;
            }
            else if (modifiers.HasFlag(ConsoleModifiers.Control))
            {
                bindings = _controlCharBindings;
            }
            else
            {
                bindings = _plainCharBindings;
            }

            if (op.HasValue)
            {
                bindings[value] = op.Value;
            }
            else if (bindings.ContainsKey(value))
            {
                bindings.Remove(value);
            }
        }
Esempio n. 3
0
        public Handler(ConsoleKey key, ConsoleModifiers mod, Action h, Boolean resetCompletion = true)
        {
            var shift = mod.HasFlag(ConsoleModifiers.Shift);
            var alt   = mod.HasFlag(ConsoleModifiers.Alt);
            var ctrl  = mod.HasFlag(ConsoleModifiers.Control);

            KeyInfo         = new ConsoleKeyInfo((Char)0, key, shift, alt, ctrl);
            KeyHandler      = h;
            ResetCompletion = resetCompletion;
        }
Esempio n. 4
0
 static object[] MkConsoleKeyInfo(string requestedKeyChord, char keyChar, ConsoleKey consoleKey, ConsoleModifiers modifiers)
 {
     return(new object[]
     {
         requestedKeyChord,
         new ConsoleKeyInfo(keyChar, consoleKey,
                            control: modifiers.HasFlag(ConsoleModifiers.Control),
                            alt: modifiers.HasFlag(ConsoleModifiers.Alt),
                            shift: modifiers.HasFlag(ConsoleModifiers.Shift))
     });
 }
Esempio n. 5
0
            public static byte[] GetKeyState(ConsoleModifiers modifiers)
            {
                const byte keyDownFlag = 0x80;

                var keyState = new byte[256];

                if (modifiers.HasFlag(ConsoleModifiers.Alt)) keyState[(int)ConsoleModifierKeys.Alt] |= keyDownFlag;
                if (modifiers.HasFlag(ConsoleModifiers.Control)) keyState[(int)ConsoleModifierKeys.Control] |= keyDownFlag;
                if (modifiers.HasFlag(ConsoleModifiers.Shift)) keyState[(int)ConsoleModifierKeys.Shift] |= keyDownFlag;

                return keyState;
            }
Esempio n. 6
0
        public static void ReadKey_KeyChords(char keyChar, ConsoleKey key, ConsoleModifiers modifiers)
        {
            var expected = new ConsoleKeyInfo(keyChar, key,
                                              control: modifiers.HasFlag(ConsoleModifiers.Control),
                                              alt: modifiers.HasFlag(ConsoleModifiers.Alt),
                                              shift: modifiers.HasFlag(ConsoleModifiers.Shift));

            Console.Write($"Please type key chord {RenderKeyChord(expected)}: ");
            var actual = Console.ReadKey(intercept: true);

            Console.WriteLine();

            Assert.Equal(expected.Key, actual.Key);
            Assert.Equal(expected.Modifiers, actual.Modifiers);
            Assert.Equal(expected.KeyChar, actual.KeyChar);
        private static KeyValuePair <ConsoleKeyInfo, ConsoleInputOperation> CreatePair(
            char value,
            ConsoleModifiers modifiers,
            ConsoleInputOperation op)
        {
            var key = GetKey(value);

            var keyInfo = new ConsoleKeyInfo(
                value,
                key,
                modifiers.HasFlag(ConsoleModifiers.Shift),
                modifiers.HasFlag(ConsoleModifiers.Alt),
                modifiers.HasFlag(ConsoleModifiers.Control));

            return(new KeyValuePair <ConsoleKeyInfo, ConsoleInputOperation>(keyInfo, op));
        }
        private static KeyValuePair <ConsoleKeyInfo, ConsoleInputOperation> CreatePair(
            ConsoleKey key,
            ConsoleModifiers modifiers,
            ConsoleInputOperation op)
        {
            var c = InputUtilities.TryGetSingleChar(key, modifiers);

            var keyInfo = new ConsoleKeyInfo(
                c.GetValueOrDefault('\0'),
                key,
                modifiers.HasFlag(ConsoleModifiers.Shift),
                modifiers.HasFlag(ConsoleModifiers.Alt),
                modifiers.HasFlag(ConsoleModifiers.Control));

            return(new KeyValuePair <ConsoleKeyInfo, ConsoleInputOperation>(keyInfo, op));
        }
        private static bool TryGetValue(
            IReadOnlyDictionary <char, ConsoleInputOperation> charBindings,
            IReadOnlyDictionary <ConsoleKey, ConsoleInputOperation> keyBindings,
            IReadOnlyDictionary <ConsoleKey, ConsoleInputOperation> shiftKeyBindings,
            ConsoleKey key,
            ConsoleModifiers modifiers,
            out ConsoleInputOperation op)
        {
            var shiftModifiers = modifiers & ConsoleModifiers.Shift;
            var charWithoutNonShiftModifiers = InputUtilities.TryGetSingleChar(key, shiftModifiers);

            // It's important that we try the character bindings first.
            if (charWithoutNonShiftModifiers.HasValue &&
                charBindings.TryGetValue(char.ToLowerInvariant(charWithoutNonShiftModifiers.Value), out op))
            {
                return(true);
            }

            if (modifiers.HasFlag(ConsoleModifiers.Shift) &&
                (shiftKeyBindings != null) &&
                shiftKeyBindings.TryGetValue(key, out op))
            {
                return(true);
            }

            return(keyBindings.TryGetValue(key, out op));
        }
Esempio n. 10
0
        public static void Demo(ConsoleModifiers modifiers)
        {
            if (modifiers.HasFlag(ConsoleModifiers.Control))
            {
            }

            if ((modifiers & ConsoleModifiers.Control) != 0)
            {
            }
        }
Esempio n. 11
0
            public static byte[] GetKeyState(ConsoleModifiers modifiers)
            {
                const byte keyDownFlag = 0x80;

                var keyState = new byte[256];

                if (modifiers.HasFlag(ConsoleModifiers.Alt))
                {
                    keyState[(int)ConsoleModifierKeys.Alt] |= keyDownFlag;
                }
                if (modifiers.HasFlag(ConsoleModifiers.Control))
                {
                    keyState[(int)ConsoleModifierKeys.Control] |= keyDownFlag;
                }
                if (modifiers.HasFlag(ConsoleModifiers.Shift))
                {
                    keyState[(int)ConsoleModifierKeys.Shift] |= keyDownFlag;
                }

                return(keyState);
            }
Esempio n. 12
0
        /// <summary>
        /// Handles key input.
        /// </summary>
        public async Task ReadKeyAsync(ConsoleKeyInfo keyInfo)
        {
            await Task.Run(() =>
            {
                ConsoleModifiers modifiers = keyInfo.Modifiers;
                if (keyInfo.Key == ConsoleKey.Enter && modifiers == 0)
                {
                    NewLineDetected?.Invoke();
                    InputModified?.Invoke();

                    return;
                }

                bool inputModified = false;
                var input          = new ShortcutDefinition(keyInfo.Key, keyInfo.Modifiers, () => { });
                if (_shortcuts.TryGetValue(input, out ShortcutDefinition shortcutDefinition))
                {
                    shortcutDefinition.Action.Invoke();
                    inputModified = shortcutDefinition.ModifiesInput;
                }
                else if (modifiers.HasFlag(ConsoleModifiers.Control) && !modifiers.HasFlag(ConsoleModifiers.Alt))
                {
                    UnhandledControlSequenceDetected?.Invoke(ref keyInfo);
                    inputModified = true; //TODO: refactor - this should not be there - having a KeyHandler instaed of only line handler is problematic
                }
                else
                {
                    UnhandledKeyDetected?.Invoke(ref keyInfo);
                    inputModified = true; //TODO: refactor - this should not be there
                }

                if (inputModified)
                {
                    InputModified?.Invoke();
                }
            });
        }
Esempio n. 13
0
        /// <summary>
        /// Converts the indicated key (with modifiers) to the generated
        /// characters, in accordance with the currently active keyboard
        /// layout. Implementation is portable and expected to be supported
        /// on all host platforms.
        /// </summary>
        /// <param name="key">The key to translate.</param>
        /// <param name="modifiers">Key modifiers.</param>
        /// <returns>The characters.</returns>
        internal static char[] GetCharsPortable(ConsoleKey key, ConsoleModifiers modifiers)
        {
            if (key >= ConsoleKey.A && key <= ConsoleKey.Z)
            {
                var alphaIndex = (int)key - (int)ConsoleKey.A;

                char c;
                if (modifiers.HasFlag(ConsoleModifiers.Control))
                {
                    if (modifiers.HasFlag(ConsoleModifiers.Alt))
                    {
                        return(Array.Empty <char>());
                    }
                    else
                    {
                        c = (char)(alphaIndex + 1);
                    }
                }
                else
                {
                    c = (char)('a' + alphaIndex);
                    if (modifiers.HasFlag(ConsoleModifiers.Shift))
                    {
                        c = char.ToUpper(c);
                    }
                }

                return(new[] { c });
            }

            if (modifiers == ConsoleModifiers.Control)
            {
                int c = 0;
                switch (key)
                {
                case ConsoleKey.Backspace: c = 127; break;

                case ConsoleKey.Enter: c = 10; break;

                case ConsoleKey.Escape: c = (int)key; break;

                case ConsoleKey.Spacebar: c = (int)key; break;

                case ConsoleKey.Oem4: c = 27; break;

                case ConsoleKey.Oem5: c = 28; break;

                case ConsoleKey.Oem6: c = 29; break;
                }

                if (c != 0)
                {
                    return(new[] { (char)c });
                }
            }

            if (modifiers.HasFlag(ConsoleModifiers.Control))
            {
                if (modifiers == (ConsoleModifiers.Control | ConsoleModifiers.Shift))
                {
                    switch (key)
                    {
                    case ConsoleKey.D6: return(new[] { (char)30 });

                    case ConsoleKey.OemMinus: return(new[] { (char)31 });
                    }
                }

                return(Array.Empty <char>());
            }

            if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9)
            {
                char c;
                if (modifiers.HasFlag(ConsoleModifiers.Shift))
                {
                    // TODO: This is plain wrong. It is dependent on keyboard layout.
                    switch (key)
                    {
                    case ConsoleKey.D1: c = '!'; break;

                    case ConsoleKey.D2: c = '@'; break;

                    case ConsoleKey.D3: c = '#'; break;

                    case ConsoleKey.D4: c = '$'; break;

                    case ConsoleKey.D5: c = '%'; break;

                    case ConsoleKey.D6: c = '^'; break;

                    case ConsoleKey.D7: c = '&'; break;

                    case ConsoleKey.D8: c = '*'; break;

                    case ConsoleKey.D9: c = '('; break;

                    case ConsoleKey.D0: c = ')'; break;

                    default: throw new InternalInvariantBrokenException("Unexpected D key");
                    }
                }
                else
                {
                    var digitIndex = (int)key - (int)ConsoleKey.D0;
                    c = (char)('0' + digitIndex);
                }

                return(new[] { c });
            }

            if (key >= ConsoleKey.NumPad0 && key <= ConsoleKey.NumPad9)
            {
                if (modifiers.HasFlag(ConsoleModifiers.Shift))
                {
                    return(Array.Empty <char>());
                }

                var offset = (int)key - (int)ConsoleKey.NumPad0;
                return(new[] { (char)('0' + offset) });
            }

            switch (key)
            {
            case ConsoleKey.Backspace:
            case ConsoleKey.Enter:
            case ConsoleKey.Escape:
            case ConsoleKey.Spacebar:
            case ConsoleKey.Tab:
                return(new char[] { (char)key });

            case ConsoleKey.Multiply:
                return(new char[] { '*' });

            case ConsoleKey.Add:
                return(new char[] { '+' });

            case ConsoleKey.Subtract:
                return(new char[] { '-' });

            case ConsoleKey.Decimal:
                return(new char[] { '.' });

            case ConsoleKey.Divide:
                return(new char[] { '/' });

            case ConsoleKey.OemComma:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '<' } : new[] { ',' });

            case ConsoleKey.OemPeriod:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '>' } : new[] { '.' });

            case ConsoleKey.OemMinus:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '_' } : new[] { '-' });

            case ConsoleKey.OemPlus:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '+' } : new[] { '=' });

            case ConsoleKey.Oem1:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { ':' } : new[] { ';' });

            case ConsoleKey.Oem2:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '?' } : new[] { '/' });

            case ConsoleKey.Oem3:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '~' } : new[] { '`' });

            case ConsoleKey.Oem4:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '{' } : new[] { '[' });

            case ConsoleKey.Oem5:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '|' } : new[] { '\\' });

            case ConsoleKey.Oem6:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '}' } : new[] { ']' });

            case ConsoleKey.Oem7:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '"' } : new[] { '\'' });

            default:
                return(Array.Empty <char>());
            }
        }
Esempio n. 14
0
        private static char[] GetCharsOnAnyPlatform(ConsoleKey key, ConsoleModifiers modifiers)
        {
            if (key >= ConsoleKey.A && key <= ConsoleKey.Z)
            {
                var alphaIndex = (int)key - (int)ConsoleKey.A;

                char c;
                if (modifiers.HasFlag(ConsoleModifiers.Control))
                {
                    c = (char)(alphaIndex + 1);
                }
                else
                {
                    c = (char)('a' + alphaIndex);
                    if (modifiers.HasFlag(ConsoleModifiers.Shift))
                    {
                        c = char.ToUpper(c);
                    }
                }

                return(new[] { c });
            }

            if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9)
            {
                char c;
                if (modifiers.HasFlag(ConsoleModifiers.Shift))
                {
                    // TODO: This is plain wrong. It is dependent on keyboard layout.
                    switch (key)
                    {
                    case ConsoleKey.D1: c = '!'; break;

                    case ConsoleKey.D2: c = '@'; break;

                    case ConsoleKey.D3: c = '#'; break;

                    case ConsoleKey.D4: c = '$'; break;

                    case ConsoleKey.D5: c = '%'; break;

                    case ConsoleKey.D6: c = '^'; break;

                    case ConsoleKey.D7: c = '&'; break;

                    case ConsoleKey.D8: c = '*'; break;

                    case ConsoleKey.D9: c = '('; break;

                    case ConsoleKey.D0: c = ')'; break;

                    default:
                        return(Array.Empty <char>());
                    }
                }
                else
                {
                    var digitIndex = (int)key - (int)ConsoleKey.D0;
                    c = (char)('0' + digitIndex);
                }

                return(new[] { c });
            }

            switch (key)
            {
            case ConsoleKey.Spacebar:
                return(new char[] { ' ' });

            case ConsoleKey.Tab:
                return(new char[] { '\t' });

            case ConsoleKey.OemComma:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '<' } : new[] { ',' });

            case ConsoleKey.OemPeriod:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '>' } : new[] { '.' });

            case ConsoleKey.OemMinus:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '_' } : new[] { '-' });

            case ConsoleKey.OemPlus:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '+' } : new[] { '=' });

            case ConsoleKey.Oem1:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { ':' } : new[] { ';' });

            case ConsoleKey.Oem2:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '?' } : new[] { '/' });

            case ConsoleKey.Oem3:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '~' } : new[] { '`' });

            case ConsoleKey.Oem4:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '{' } : new[] { '[' });

            case ConsoleKey.Oem5:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '|' } : new[] { '\\' });

            case ConsoleKey.Oem6:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '}' } : new[] { ']' });

            case ConsoleKey.Oem7:
                return(modifiers.HasFlag(ConsoleModifiers.Shift) ? new[] { '"' } : new[] { '\'' });

            default:
                return(Array.Empty <char>());
            }
        }
Esempio n. 15
0
 public void Enqueue(ConsoleKey key, ConsoleModifiers modifiers = 0)
 {
     Enqueue(new ConsoleKeyInfo('\0', key, modifiers.HasFlag(ConsoleModifiers.Shift), modifiers.HasFlag(ConsoleModifiers.Alt), modifiers.HasFlag(ConsoleModifiers.Control)));
 }
Esempio n. 16
0
 public bool HasFlag() => modifiers.HasFlag(ConsoleModifiers.Control);