Example #1
0
        internal static void SetActive(bool isActive)
        {
            _isActive = isActive;

            if (!_isActive)
            {
                KeysDownList.Clear();
            }
        }
Example #2
0
        private static void UpdateState(bool onlyModifiers)
        {
            _modifiers = KeyModifiers.None;

            if (Console.CapsLock)
            {
                _modifiers |= KeyModifiers.CapsLock;
            }

            if (Console.NumberLock)
            {
                _modifiers |= KeyModifiers.NumLock;
            }

            if (_isActive)
            {
                Span <byte> keyState = stackalloc byte[256];
                if (GetKeyboardState(ref MemoryMarshal.GetReference(keyState)))
                {
                    if (onlyModifiers)
                    {
                        SetModifierByState(keyState, Keys.LeftShift, KeyModifiers.LeftShift);
                        SetModifierByState(keyState, Keys.RightShift, KeyModifiers.RightShift);
                        SetModifierByState(keyState, Keys.LeftAlt, KeyModifiers.LeftAlt);
                        SetModifierByState(keyState, Keys.RightAlt, KeyModifiers.RightAlt);
                        SetModifierByState(keyState, Keys.LeftControl, KeyModifiers.LeftControl);
                        SetModifierByState(keyState, Keys.RightControl, KeyModifiers.RightControl);
                    }
                    else
                    {
                        foreach (Keys key in DefinedKeys)
                        {
                            if (IsKeyPressed(keyState, key))
                            {
                                switch (key)
                                {
                                case Keys.LeftShift:
                                    _modifiers |= KeyModifiers.LeftShift;
                                    break;

                                case Keys.RightShift:
                                    _modifiers |= KeyModifiers.RightShift;
                                    break;

                                case Keys.LeftAlt:
                                    _modifiers |= KeyModifiers.LeftAlt;
                                    break;

                                case Keys.RightAlt:
                                    _modifiers |= KeyModifiers.RightAlt;
                                    break;

                                case Keys.LeftControl:
                                    _modifiers |= KeyModifiers.LeftControl;
                                    break;

                                case Keys.RightControl:
                                    _modifiers |= KeyModifiers.RightControl;
                                    break;
                                }

                                if (!KeysDownList.Contains(key))
                                {
                                    KeysDownList.Add(key);
                                }
                            }
                            else
                            {
                                KeysDownList.Remove(key);
                            }
                        }
                    }
                }
            }
            else
            {
                KeysDownList.Clear();
            }
        }