Beispiel #1
0
        /// <summary>
        /// Moves to what is to the right of the cursor position.
        /// </summary>
        /// <param name="consoleModifiers">Enumeration for Alt, Control,
        /// and Shift keys.</param>
        private void OnRight(ConsoleModifiers consoleModifiers)
        {
            if ((consoleModifiers & ConsoleModifiers.Control) != 0)
            {
                // Move to the next word.
                if (buffer.Length != 0 && current < buffer.Length)
                {
                    bool nonLetter = IsSeperator(buffer[current]);
                    while (current < buffer.Length)
                    {
                        MoveRight();

                        if (current == buffer.Length)
                        {
                            break;
                        }

                        if (IsSeperator(buffer[current]) != nonLetter)
                        {
                            if (nonLetter)
                            {
                                break;
                            }

                            nonLetter = true;
                        }
                    }
                }
            }
            else
            {
                MoveRight();
            }
        }
        internal static char GetCharFromConsoleKey(ConsoleKey key, ConsoleModifiers modifiers)
        {
            // default for unprintables and unhandled
            char keyChar = '\u0000';

            // emulate GetKeyboardState bitmap - set high order bit for relevant modifier virtual keys
            var state = new byte[256];
            state[NativeMethods.VK_SHIFT] = (byte)(((modifiers & ConsoleModifiers.Shift) != 0) ? 0x80 : 0);
            state[NativeMethods.VK_CONTROL] = (byte)(((modifiers & ConsoleModifiers.Control) != 0) ? 0x80 : 0);
            state[NativeMethods.VK_ALT] = (byte)(((modifiers & ConsoleModifiers.Alt) != 0) ? 0x80 : 0);

            // a ConsoleKey enum's value is a virtual key code
            uint virtualKey = (uint)key;

            // get corresponding scan code
            uint scanCode = NativeMethods.MapVirtualKey(virtualKey, NativeMethods.MAPVK_VK_TO_VSC);

            // get corresponding character  - maybe be 0, 1 or 2 in length (diacriticals)
            var chars = new char[2];
            int charCount = NativeMethods.ToUnicode(
                virtualKey, scanCode, state, chars, chars.Length, NativeMethods.MENU_IS_INACTIVE);

            // TODO: support diacriticals (charCount == 2)
            if (charCount == 1)
            {
                keyChar = chars[0];
            }

            return keyChar;
        }
Beispiel #3
0
 public ConsoleCommand(Action handler, ConsoleKey key, string description)
 {
     _handler    = handler;
     _key        = key;
     Description = description;
     _modifiers  = 0;
 }
Beispiel #4
0
 /// <summary>
 /// Creates a new InputButton from the given controller button.
 /// </summary>
 public InputButton(Buttons Button)
 {
     this._Key          = 0;    // Prevent complaining about readonly.
     this._KeyModifiers = 0;
     this._Button       = Button;
     this.Type          = InputMethod.Button;
 }
        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));
        }
Beispiel #6
0
 /// <summary>
 /// Initializes an instance of <see cref="ShortcutDefinition"/>.
 /// </summary>
 public ShortcutDefinition(ConsoleKey key, ConsoleModifiers modifiers, Action action)
 {
     Key           = key;
     Modifiers     = modifiers;
     Action        = action;
     ModifiesInput = false;
 }
Beispiel #7
0
		public ConsoleKeyInfo (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
		{
			_key = key;
			_keyChar = keyChar;
			_mods = 0;
			SetModifiers (shift, alt, control);
		}
        /// <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);
            }
        }
        internal static char GetCharFromConsoleKey(ConsoleKey key, ConsoleModifiers modifiers)
        {
            // default for unprintables and unhandled
            char keyChar = '\u0000';

            // emulate GetKeyboardState bitmap - set high order bit for relevant modifier virtual keys
            var state = new byte[256];

            state[NativeMethods.VK_SHIFT]   = (byte)(((modifiers & ConsoleModifiers.Shift) != 0) ? 0x80 : 0);
            state[NativeMethods.VK_CONTROL] = (byte)(((modifiers & ConsoleModifiers.Control) != 0) ? 0x80 : 0);
            state[NativeMethods.VK_ALT]     = (byte)(((modifiers & ConsoleModifiers.Alt) != 0) ? 0x80 : 0);

            // a ConsoleKey enum's value is a virtual key code
            uint virtualKey = (uint)key;

            // get corresponding scan code
            uint scanCode = NativeMethods.MapVirtualKey(virtualKey, NativeMethods.MAPVK_VK_TO_VSC);

            // get corresponding character  - maybe be 0, 1 or 2 in length (diacriticals)
            var chars     = new char[2];
            int charCount = NativeMethods.ToUnicode(
                virtualKey, scanCode, state, chars, chars.Length, NativeMethods.MENU_IS_INACTIVE);

            // TODO: support diacriticals (charCount == 2)
            if (charCount == 1)
            {
                keyChar = chars[0];
            }

            return(keyChar);
        }
Beispiel #10
0
        public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
        {
            _mods = 0;

            // Limit ConsoleKey values to 0 to 255, but don't check whether the
            // key is a valid value in our ConsoleKey enum.  There are a few
            // values in that enum that we didn't define, and reserved keys
            // that might start showing up on keyboards in a few years.
            if ((int)key < 0 || (int)key > 255)
            {
                _keyChar = '\0';
                _key     = 0;
                return;
                //TODO Fix throw and use dotnet version of ConsoleKeyInfo
                //throw new ArgumentOutOfRangeException();
            }

            _keyChar = keyChar;
            _key     = key;
            _mods    = 0;
            if (shift)
            {
                _mods |= ConsoleModifiers.Shift;
            }
            if (alt)
            {
                _mods |= ConsoleModifiers.Alt;
            }
            if (control)
            {
                _mods |= ConsoleModifiers.Control;
            }
        }
        public Subscription PushUnmanaged(ConsoleKey key, ConsoleModifiers? modifier, Action<ConsoleKeyInfo> handler)
        {
            Dictionary<ConsoleKey, Stack<Action<ConsoleKeyInfo>>> target;

            if (modifier.HasValue == false) target = nakedHandlers;
            else if (modifier.Value.HasFlag(ConsoleModifiers.Alt)) target = altHandlers;
            else if (modifier.Value.HasFlag(ConsoleModifiers.Shift)) target = shiftHandlers;
            else if (modifier.Value.HasFlag(ConsoleModifiers.Control)) target = controlHandlers;
            else throw new ArgumentException("Unsupported modifier: "+modifier.Value);

            Stack<Action<ConsoleKeyInfo>> targetStack;
            if(target.TryGetValue(key, out targetStack) == false)
            {
                targetStack = new Stack<Action<ConsoleKeyInfo>>();
                target.Add(key, targetStack);
            }

            targetStack.Push(handler);
            var sub = new Subscription(() =>
            {
                targetStack.Pop();
                if(targetStack.Count == 0)
                {
                    target.Remove(key);
                }
            });
            return sub;
        }
Beispiel #12
0
        private void MoveLeft(ConsoleModifiers keyModifiers)
        {
            if ((keyModifiers & ConsoleModifiers.Control) != 0)
            {
                // move back to the start of the previous word
                if (_input.Length > 0 && _current != 0)
                {
                    bool nonLetter = IsSeperator(_input[_current - 1]);
                    while (_current > 0 && (_current - 1 < _input.Length))
                    {
                        MoveLeft();

                        if (IsSeperator(_input[_current]) != nonLetter)
                        {
                            if (!nonLetter)
                            {
                                MoveRight();
                                break;
                            }

                            nonLetter = false;
                        }
                    }
                }
            }
            else
            {
                MoveLeft();
            }
        }
Beispiel #13
0
        private void MoveRight(ConsoleModifiers keyModifiers)
        {
            if ((keyModifiers & ConsoleModifiers.Control) != 0)
            {
                // move to the next word
                if (_input.Length != 0 && _current < _input.Length)
                {
                    bool nonLetter = IsSeperator(_input[_current]);
                    while (_current < _input.Length)
                    {
                        MoveRight();

                        if (_current == _input.Length)
                        {
                            break;
                        }
                        if (IsSeperator(_input[_current]) != nonLetter)
                        {
                            if (nonLetter)
                            {
                                break;
                            }

                            nonLetter = true;
                        }
                    }
                }
            }
            else
            {
                MoveRight();
            }
        }
 public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
 {
     _key     = key;
     _keyChar = keyChar;
     _mods    = 0;
     SetModifiers(shift, alt, control);
 }
Beispiel #15
0
 internal ConsoleKeyInfo
     (char keyChar, ConsoleKey key, ConsoleModifiers modifiers)
 {
     this.keyChar   = keyChar;
     this.key       = key;
     this.modifiers = modifiers;
 }
Beispiel #16
0
 /// <summary>
 /// Creates a new InputButton from the given controller button.
 /// </summary>
 public InputButton(Buttons Button)
 {
     this._Key = 0; // Prevent complaining about readonly.
     this._KeyModifiers = 0;
     this._Button = Button;
     this.Type = InputMethod.Button;
 }
Beispiel #17
0
 public KeyPressPattern(ConsoleModifiers modifiers, ConsoleKey key)
 {
     type      = KeyPressPatternType.ConsoleKey;
     Modifiers = modifiers;
     Key       = key;
     Character = MapToCharacter(key);
 }
        /// <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);
            }
        }
Beispiel #19
0
 public KeyPressPattern(char character)
 {
     type      = KeyPressPatternType.Character;
     Modifiers = default;
     Key       = default;
     Character = character;
 }
Beispiel #20
0
 /// <summary>
 /// Creates a new InputButton from the given key and, optionally, modifiers.
 /// </summary>
 public InputButton(Keys Key, ConsoleModifiers Modifiers = 0)
 {
     this._Button = 0; // Make it not complain about readonly being assigned, even though it's incorrect.
     this._Key = Key;
     this._KeyModifiers = Modifiers;
     this.Type = InputMethod.Key;
 }
Beispiel #21
0
        /// <summary>
        /// Gets a string representation of a KeyEvent.
        /// </summary>
        /// <param name="evt">The KeyEvent.</param>
        /// <returns>A string representation of a KeyEvent.</returns>
        public static string GetString(KeyEvent evt)
        {
            CheckKeyboardLayout();

            string sc = evt.Key.ToString().ToUpperInvariant();

            var node = currentXmlDoc.Descendants("Key")
                       .FirstOrDefault(key => key.Element("VirtualKey").Value.ToUpperInvariant() == sc);

            if (node != null)
            {
                string           modElement = "Char";
                ConsoleModifiers mod        = evt.Modifiers;
                if (mod != 0)
                {
                    if (mod.IsFlagSet(ConsoleModifiers.Shift))
                    {
                        modElement = mod.IsFlagSet(ConsoleModifiers.Control) ? "ShiftCtrl" : "Shift";
                    }
                    else if (mod.IsFlagSet(ConsoleModifiers.Control))
                    {
                        modElement = mod.IsFlagSet(ConsoleModifiers.Alt) ? "CtrlAlt" : "Ctrl";
                    }
                }

                XElement keyNode = node.Element(modElement);
                if (keyNode != null)
                {
                    return(keyNode.Value);
                }
            }

            return(string.Empty);
        }
Beispiel #22
0
 /// <summary>
 /// Initializes an instance of <see cref="ShortcutDefinition"/>.
 /// </summary>
 internal ShortcutDefinition(ConsoleKey key, ConsoleModifiers modifiers, bool modifiesInput, Action action)
 {
     Key           = key;
     Modifiers     = modifiers;
     Action        = action;
     ModifiesInput = modifiesInput;
 }
Beispiel #23
0
		public ConsoleKeyInfo (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
		{
			this.key = key;
			this.keychar = keyChar;
			modifiers = 0;
			SetModifiers (shift, alt, control);
		}
Beispiel #24
0
 /// <summary>
 /// Creates a new InputButton from the given key and, optionally, modifiers.
 /// </summary>
 public InputButton(Keys Key, ConsoleModifiers Modifiers = 0)
 {
     this._Button       = 0;       // Make it not complain about readonly being assigned, even though it's incorrect.
     this._Key          = Key;
     this._KeyModifiers = Modifiers;
     this.Type          = InputMethod.Key;
 }
Beispiel #25
0
 public KeyBinding(EditMode mode, ConsoleModifiers modifiers, ConsoleKey key, Action action)
 {
     Mode      = mode;
     Modifiers = modifiers;
     Key       = key;
     Action    = action;
 }
Beispiel #26
0
        /// <summary>
        /// Moves to the left of the cursor postion.
        /// </summary>
        /// <param name="consoleModifiers">Enumeration for Alt, Control,
        /// and Shift keys.</param>
        private void OnLeft(ConsoleModifiers consoleModifiers)
        {
            if ((consoleModifiers & ConsoleModifiers.Control) != 0)
            {
                // Move back to the start of the previous word.
                if (buffer.Length > 0 && current != 0)
                {
                    bool nonLetter = IsSeperator(buffer[current - 1]);
                    while (current > 0 && (current - 1 < buffer.Length))
                    {
                        MoveLeft();

                        if (IsSeperator(buffer[current]) != nonLetter)
                        {
                            if (!nonLetter)
                            {
                                MoveRight();
                                break;
                            }

                            nonLetter = false;
                        }
                    }
                }
            }
            else
            {
                MoveLeft();
            }
        }
Beispiel #27
0
        /// <summary>
        /// Moves to what is to the right of the cursor position.
        /// </summary>
        /// <param name="consoleModifiers">Enumeration for Alt, Control,
        /// and Shift keys.</param>
        private void OnRight(ConsoleModifiers consoleModifiers)
        {
            if ((consoleModifiers & ConsoleModifiers.Control) != 0)
            {
                // Move to the next word.
                if (this.buffer.Length != 0 && this.current < this.buffer.Length)
                {
                    bool nonLetter = IsSeparator(this.buffer[this.current]);
                    while (this.current < this.buffer.Length)
                    {
                        this.MoveRight();

                        if (this.current == this.buffer.Length)
                        {
                            break;
                        }

                        if (IsSeparator(this.buffer[this.current]) != nonLetter)
                        {
                            if (nonLetter)
                            {
                                break;
                            }

                            nonLetter = true;
                        }
                    }
                }
            }
            else
            {
                this.MoveRight();
            }
        }
Beispiel #28
0
        /// <summary>
        /// Registers the system-wide hot key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="modifiers">The key modifiers.</param>
        /// <returns>The registered <see cref="HotKey"/>.</returns>
        public HotKey Register(ConsoleKey key, ConsoleModifiers modifiers)
        {
            var hotKey = new HotKey(key, modifiers);

            Register(hotKey);
            return(hotKey);
        }
Beispiel #29
0
        static void AddKey(ConsoleKey key, ConsoleModifiers mod)
        {
            if ((int)mod != 0)

                Console.Write(mod.ToString() + " + ");
                //Console.WriteLine(key);
        }
Beispiel #30
0
        /// <summary>
        /// Converts the indicated key (with modifiers) to the generated
        /// characters, in accordance with the currently active keyboard
        /// layout.
        /// </summary>
        /// <param name="key">The key to translate.</param>
        /// <param name="modifiers">Key modifiers.</param>
        /// <returns>The characters.</returns>
        public static char[] GetChars(ConsoleKey key, ConsoleModifiers modifiers)
        {
            //
            // TODO: This whole method needs to be cleaned up.  We have
            // existing code that is Windows-specific, which p/invokes
            // into user32.dll to convert a ConsoleKey to an array of chars.
            // Firstly, this definitely doesn't work on non-Windows platforms;
            // secondly, it's not clear we need such a generic facility here
            // anyhow.  Someone should look back into this to figure out what
            // we *really* need, and find a way to provide that in a
            // platform-agnostic way.
            //

#if NET461
            return(GetCharsOnWindows(key, modifiers));
#else
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return(GetCharsOnWindows(key, modifiers));
            }
            else
            {
                return(GetCharsOnAnyPlatform(key, modifiers));
            }
#endif
        }
	internal ConsoleKeyInfo
				(char keyChar, ConsoleKey key, ConsoleModifiers modifiers)
			{
				this.keyChar = keyChar;
				this.key = key;
				this.modifiers = modifiers;
			}
Beispiel #32
0
        private bool DoTwoCodePathsAgree(ConsoleKey key, ConsoleModifiers modifiers)
        {
            var portableChars = InputUtilities.GetCharsPortable(key, modifiers);
            var winChars      = NClap.Utilities.Windows.InputUtilities.GetChars(key, modifiers);

            if (portableChars.Length != winChars.Length)
            {
                Console.Error.WriteLine(
                    $"Different char counts for '{key}' (mods={modifiers}): " +
                    $"portable=[{string.Join(", ", portableChars.Select(c => ((byte)c).ToString()))}] " +
                    $"win=[{string.Join(", ", winChars.Select(c => ((byte)c).ToString()))}]");
                return(false);
            }

            for (var i = 0; i < portableChars.Length; ++i)
            {
                if (portableChars[i] != winChars[i])
                {
                    Console.Error.WriteLine(
                        $"Different chars for '{key}' (mods={modifiers}): " +
                        $"portable=[{string.Join(", ", portableChars.Select(c => ((byte)c).ToString()))}] " +
                        $"win=[{string.Join(", ", winChars.Select(c => ((byte)c).ToString()))}]");
                    return(false);
                }
            }

            return(true);
        }
Beispiel #33
0
        public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
        {
            // Limit ConsoleKey values to 0 to 255, but don't check whether the
            // key is a valid value in our ConsoleKey enum.  There are a few
            // values in that enum that we didn't define, and reserved keys
            // that might start showing up on keyboards in a few years.
            if (((int)key) < 0 || ((int)key) > 255)
            {
                throw new ArgumentOutOfRangeException(nameof(key), SR.ArgumentOutOfRange_ConsoleKey);
            }

            _keyChar = keyChar;
            _key     = key;
            _mods    = 0;
            if (shift)
            {
                _mods |= ConsoleModifiers.Shift;
            }
            if (alt)
            {
                _mods |= ConsoleModifiers.Alt;
            }
            if (control)
            {
                _mods |= ConsoleModifiers.Control;
            }
        }
            internal CommandKeyMapping2(Command command, ConsoleKey key, bool shift = false)
            {
                Command = command;

                _key       = key;
                _modifiers = shift ? ConsoleModifiers.Shift : default;
            }
Beispiel #35
0
 public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
 {
     this.key     = key;
     this.keychar = keyChar;
     modifiers    = 0;
     SetModifiers(shift, alt, control);
 }
Beispiel #36
0
 static void AddKey(ConsoleKey key, ConsoleModifiers mod)
 {
     if ((int)mod != 0)
     {
         Console.Write(mod.ToString() + " + ");
     }
     Console.WriteLine(key);
 }
Beispiel #37
0
 public virtual void DownArrow(ConsoleKey key, ConsoleModifiers modifiers)
 {
     if (Console.CursorTop == (_displayLineIndex + Text.GetLines().Count() - 1))
     {
         return;
     }
     Console.CursorTop++;
 }
Beispiel #38
0
 public virtual void UpArrow(ConsoleKey key, ConsoleModifiers modifiers)
 {
     if (Console.CursorTop == _displayLineIndex)
     {
         return;
     }
     Console.CursorTop--;
 }
	// Constructors.
	public ConsoleKeyInfo(char keyChar, ConsoleKey key,
						  bool shift, bool alt, bool control)
			{
				this.keyChar = keyChar;
				this.key = key;
				this.modifiers =
					(shift ? ConsoleModifiers.Shift : 0) |
					(alt ? ConsoleModifiers.Alt : 0) |
					(control ? ConsoleModifiers.Control : 0);
			}
Beispiel #40
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;
            }
Beispiel #41
0
        /// <summary>
        /// Converts the indicated key (with modifiers) to the generated
        /// characters, in accordance with the currently active keyboard
        /// layout.
        /// </summary>
        /// <param name="key">The key to translate.</param>
        /// <param name="modifiers">Key modifiers.</param>
        /// <returns>The characters.</returns>
        public static char[] GetChars(ConsoleKey key, ConsoleModifiers modifiers)
        {
            var virtKey = (uint)key;
            var output = new char[32];

            var result = NativeMethods.ToUnicode(virtKey, 0, NativeMethods.GetKeyState(modifiers), output, output.Length, 0 /* flags */);
            if (result < 0) result = 0;

            var relevantOutput = new char[result];
            Array.Copy(output, relevantOutput, result);

            return relevantOutput;
        }
Beispiel #42
0
 public ConsoleKeyInfoEx(char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
 {
     this.KeyChar = keyChar;
     this.Key = key;
     this.Modifiers = (ConsoleModifiers)0;
     if (shift)
     {
         this.Modifiers |= ConsoleModifiers.Shift;
     }
     if (alt)
     {
         this.Modifiers |= ConsoleModifiers.Alt;
     }
     if (control)
     {
         this.Modifiers |= ConsoleModifiers.Control;
     }
 }
Beispiel #43
0
        public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool control) {
            // Limit ConsoleKey values to 0 to 255, but don't check whether the
            // key is a valid value in our ConsoleKey enum.  There are a few 
            // values in that enum that we didn't define, and reserved keys 
            // that might start showing up on keyboards in a few years.
            if (((int)key) < 0 || ((int)key) > 255)
                throw new ArgumentOutOfRangeException("key", Environment.GetResourceString("ArgumentOutOfRange_ConsoleKey"));
            Contract.EndContractBlock();

            _keyChar = keyChar;
            _key = key;
            _mods = 0;
            if (shift)
                _mods |= ConsoleModifiers.Shift;
            if (alt)
                _mods |= ConsoleModifiers.Alt;
            if (control)
                _mods |= ConsoleModifiers.Control;
        }
		public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
		{
			if (key < (ConsoleKey)0 || key > (ConsoleKey)255)
			{
				throw new ArgumentOutOfRangeException("key", Environment.GetResourceString("ArgumentOutOfRange_ConsoleKey"));
			}
			this._keyChar = keyChar;
			this._key = key;
			this._mods = (ConsoleModifiers)0;
			if (shift)
			{
				this._mods |= ConsoleModifiers.Shift;
			}
			if (alt)
			{
				this._mods |= ConsoleModifiers.Alt;
			}
			if (control)
			{
				this._mods |= ConsoleModifiers.Control;
			}
		}
Beispiel #45
0
        private static byte[] GetKeyState(ConsoleModifiers modifiers)
        {
            var keyState = new byte[256];

            if ((modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift)
                keyState[VK_SHIFT] = HighBit;
            if ((modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
                keyState[VK_CONTROL] = HighBit;
            if ((modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt)
                keyState[VK_ALT] = HighBit;

            keyState[VK_CAPITAL] = (byte) (_queryKeyState(VK_CAPITAL) & 0xFF);

            return keyState;
        }
 public void PushForLifetime(ConsoleKey key, ConsoleModifiers? modifier, Action handler, LifetimeManager manager)
 {
     PushForLifetime(key, modifier, (k) => { handler(); }, manager);
 }
 public void PushForLifetime(ConsoleKey key, ConsoleModifiers? modifier, Action<ConsoleKeyInfo> handler, LifetimeManager manager)
 {
     manager.Manage(PushUnmanaged(key, modifier, handler));
 }
        /// <summary>
        /// Moves to the left of the cursor postion.
        /// </summary>
        /// <param name="consoleModifiers">Enumeration for Alt, Control, 
        /// and Shift keys.</param>
        private void OnLeft(ConsoleModifiers consoleModifiers)
        {
            if ((consoleModifiers & ConsoleModifiers.Control) != 0)
            {
                // Move back to the start of the previous word.
                if (this.buffer.Length > 0 && this.current != 0)
                {
                    bool nonLetter = IsSeperator(this.buffer[this.current - 1]);
                    while (this.current > 0 && (this.current - 1 < this.buffer.Length))
                    {
                        this.MoveLeft();

                        if (IsSeperator(this.buffer[this.current]) != nonLetter)
                        {
                            if (!nonLetter)
                            {
                                this.MoveRight();
                                break;
                            }

                            nonLetter = false;
                        }
                    }
                }
            }
            else
            {
                this.MoveLeft();
            }
        }
        /// <summary>
        /// Moves to what is to the right of the cursor position.
        /// </summary>
        /// <param name="consoleModifiers">Enumeration for Alt, Control, 
        /// and Shift keys.</param>
        private void OnRight(ConsoleModifiers consoleModifiers)
        {
            if ((consoleModifiers & ConsoleModifiers.Control) != 0)
            {
                // Move to the next word.
                if (this.buffer.Length != 0 && this.current < this.buffer.Length)
                {
                    bool nonLetter = IsSeperator(this.buffer[this.current]);
                    while (this.current < this.buffer.Length)
                    {
                        this.MoveRight();

                        if (this.current == this.buffer.Length)
                        {
                            break;
                        }

                        if (IsSeperator(this.buffer[this.current]) != nonLetter)
                        {
                            if (nonLetter)
                            {
                                break;
                            }

                            nonLetter = true;
                        }
                    }
                }
            }
            else
            {
                this.MoveRight();
            }
        }
Beispiel #50
0
        private void MoveRight(ConsoleModifiers keyModifiers)
        {
            if ((keyModifiers & ConsoleModifiers.Control) != 0)
            {
                // move to the next word
                if (input.Length != 0 && current < input.Length)
                {
                    var nonLetter = IsSeperator(input[current]);
                    while (current < input.Length)
                    {
                        MoveRight();

                        if (current == input.Length)
                            break;
                        if (IsSeperator(input[current]) != nonLetter)
                        {
                            if (nonLetter)
                                break;

                            nonLetter = true;
                        }
                    }
                }
            }
            else
            {
                MoveRight();
            }
        }
 public Subscription PushUnmanaged(ConsoleKey key, ConsoleModifiers? modifier, Action handler)
 {
     return PushUnmanaged(key, modifier, (k) => { handler(); });
 }
Beispiel #52
0
		internal ConsoleKeyInfo (ConsoleKeyInfo other)
		{
			_key = other._key;
			_keyChar = other._keyChar;
			_mods = other._mods;
		}
Beispiel #53
0
        private void MoveLeft(ConsoleModifiers keyModifiers)
        {
            if ((keyModifiers & ConsoleModifiers.Control) != 0)
            {
                // move back to the start of the previous word
                if (input.Length > 0 && current != 0)
                {
                    var nonLetter = IsSeperator(input[current - 1]);
                    while (current > 0 && (current - 1 < input.Length))
                    {
                        MoveLeft();

                        if (IsSeperator(input[current]) != nonLetter)
                        {
                            if (!nonLetter)
                            {
                                MoveRight();
                                break;
                            }

                            nonLetter = false;
                        }
                    }
                }
            }
            else
            {
                MoveLeft();
            }
        }
        private static bool TryParseCharLiteral(char literal, ref ConsoleModifiers modifiers, ref ConsoleKey key, out string failReason)
        {
            bool valid = false;

#if UNIX
            bool isShift;
            bool isCtrl;
            key = GetKeyFromCharValue(literal, out isShift, out isCtrl);

            // Failure to get a key for the char just means that the key is not
            // special (in the ConsoleKey enum), so we return a default key.
            // Thus this never fails.
            valid = true;
            failReason = null;

            if (isShift)
            {
                modifiers |= ConsoleModifiers.Shift;
            }
            if (isCtrl)
            {
                modifiers |= ConsoleModifiers.Control;
            }
            // alt is not possible to get
#else
            // shift state will be in MSB
            short virtualKey = NativeMethods.VkKeyScan(literal);
            int hresult = Marshal.GetLastWin32Error();

            if (virtualKey != 0)
            {
                // e.g. "}" = 0x01dd but "]" is 0x00dd, ergo } = shift+].
                // shift = 1, control = 2, alt = 4, hankaku = 8 (ignored)
                int state = virtualKey >> 8;

                if ((state & 1) == 1)
                {
                    modifiers |= ConsoleModifiers.Shift;
                }
                if ((state & 2) == 2)
                {
                    modifiers |= ConsoleModifiers.Control;
                }
                if ((state & 4) == 4)
                {
                    modifiers |= ConsoleModifiers.Alt;
                }

                virtualKey &= 0xff;

                if (Enum.IsDefined(typeof (ConsoleKey), (int) virtualKey))
                {
                    failReason = null;
                    key = (ConsoleKey) virtualKey;
                    valid = true;
                }
                else
                {
                    // haven't seen this happen yet, but possible
                    failReason = String.Format(CultureInfo.CurrentCulture, PSReadLineResources.UnrecognizedKey, virtualKey);
                }                
            }
            else
            {
                Exception e = Marshal.GetExceptionForHR(hresult);
                failReason = e.Message;
            }
#endif

            return valid;
        }
Beispiel #55
0
 private void SendKeysMainApp(ConsoleModifiers m, ConsoleKey e)
 {
     try
     {
         var blobSettings = string.Empty;
         var iModifierKey = 0;
         switch (m)
         {
             case ConsoleModifiers.Control:
                 iModifierKey = 2;
                 break;
             case ConsoleModifiers.Alt:
                 iModifierKey = 4;
                 break;
         }
         blobSettings = "";
         TsMemFiles.SetString("Lookup Command", "Function", "101", ref blobSettings);
         TsMemFiles.SetString("Lookup Command", "Control", this.Name, ref blobSettings);
         TsMemFiles.SetString("Lookup Command", "Shift", Convert.ToString(iModifierKey), ref blobSettings);
         TsMemFiles.SetString("Lookup Command", "Key", Convert.ToString(Convert.ToInt32(e)), ref blobSettings);
         //MessageBox.Show(blobSettings);
         var i = TsCallBackHelper.SetCustom(ref blobSettings);
     }
     catch (Exception ex)
     {
         if (Support.Debug)
         {
             Support.WriteDebug("SendKeysMainApp - " + ex.Message);
         }
     }
 }
Beispiel #56
0
 /// <summary>
 /// Creates a new shortut
 /// </summary>
 /// <param name="key">the shortcut key</param>
 /// <param name="modifier">A key modifier (e.g. shift, alt) that, when present, must be pressed in order for the shortcut key to trigger.  Note that control is not
 /// supported because it doesn't play well in a console</param>
 public KeyboardShortcut(ConsoleKey key, ConsoleModifiers? modifier = null)
 {
     this.Key = key;
     this.Modifier = modifier;
     if(modifier == ConsoleModifiers.Control)
     {
         throw new InvalidOperationException("Control is not supported as a keyboard shortcut modifier");
     }
 }
Beispiel #57
0
		internal void SetModifiers (bool shift, bool alt, bool control)
		{
			this.modifiers = (shift) ? ConsoleModifiers.Shift : 0;
			this.modifiers |= (alt) ? ConsoleModifiers.Alt : 0;
			this.modifiers |= (control) ? ConsoleModifiers.Control : 0;
		}
Beispiel #58
0
		internal ConsoleKeyInfo (ConsoleKeyInfo other)
		{
			this.key = other.key;
			this.keychar = other.keychar;
			this.modifiers = other.modifiers;
		}
Beispiel #59
0
 public ConsoleCommand(Action handler, ConsoleKey key, ConsoleModifiers modifiers = 0)
 {
     _handler = handler;
     _key = key;
     _modifiers = modifiers;
 }
        private static bool TryParseCharLiteral(char literal, ref ConsoleModifiers modifiers, ref ConsoleKey key, out string failReason)
        {
            bool valid = false;

            // shift state will be in MSB
            short virtualKey = NativeMethods.VkKeyScan(literal);
            int hresult = Marshal.GetLastWin32Error();

            if (virtualKey != 0)
            {
                // e.g. "}" = 0x01dd but "]" is 0x00dd, ergo } = shift+].
                // shift = 1, control = 2, alt = 4, hankaku = 8 (ignored)
                int state = virtualKey >> 8;

                if ((state & 1) == 1)
                {
                    modifiers |= ConsoleModifiers.Shift;
                }
                if ((state & 2) == 2)
                {
                    modifiers |= ConsoleModifiers.Control;
                }
                if ((state & 4) == 4)
                {
                    modifiers |= ConsoleModifiers.Alt;
                }

                virtualKey &= 0xff;

                if (Enum.IsDefined(typeof (ConsoleKey), (int) virtualKey))
                {
                    failReason = null;
                    key = (ConsoleKey) virtualKey;
                    valid = true;
                }
                else
                {
                    // haven't seen this happen yet, but possible
                    failReason = String.Format(CultureInfo.CurrentCulture, PSReadLineResources.UnrecognizedKey, virtualKey);
                }
            }
            else
            {
                Exception e = Marshal.GetExceptionForHR(hresult);
                failReason = e.Message;
            }

            return valid;
        }