Esempio n. 1
0
        public static KeysModifiers GetModifiers()
        {
            KeysModifiers m = KeysModifiers.None;

            KeyStates shiftState = GetKeyState(Keys.LeftShift, RegisteredKeyStates);

            if (shiftState == KeyStates.Released | shiftState == KeyStates.Releasing)
            {
                shiftState = GetKeyState(Keys.RightShift, RegisteredKeyStates);
            }
            if (shiftState == KeyStates.Pressed | shiftState == KeyStates.Pressing)
            {
                m |= KeysModifiers.Shift;
            }

            if (KeysMethods.CapsLocked())
            {
                if (m.HasFlag(KeysModifiers.Shift))
                {
                    m &= ~KeysModifiers.Shift;
                }
                else
                {
                    m |= KeysModifiers.Shift;
                }
            }


            KeyStates altState = GetKeyState(Keys.LeftAlt, RegisteredKeyStates);

            if (altState == KeyStates.Released | altState == KeyStates.Releasing)
            {
                altState = GetKeyState(Keys.RightAlt, RegisteredKeyStates);
            }
            if (altState == KeyStates.Pressed | altState == KeyStates.Pressing)
            {
                m |= KeysModifiers.Alt;
            }

            KeyStates ctrlState = GetKeyState(Keys.LeftControl, RegisteredKeyStates);

            if (ctrlState == KeyStates.Released | ctrlState == KeyStates.Releasing)
            {
                ctrlState = GetKeyState(Keys.RightControl, RegisteredKeyStates);
            }
            if (ctrlState == KeyStates.Pressed | ctrlState == KeyStates.Pressing)
            {
                m |= KeysModifiers.Control;
            }

            return(m);
        }
Esempio n. 2
0
        protected internal override void Update()
        {
            base.Update();

            if (!Hovered && Editing && Input.IsPressing(Keys.MouseLeftButton))
            {
                Editing = false;
                this.OnLeaveEdit?.Invoke();
            }

            if (!Graphics.Window.Focused)
            {
                Editing = false;
            }

            bool textEdited = false;
            int  chars      = Text == null ? 0 : Text.Length;

            KeysModifiers modifiers = Input.GetModifiers();

            if (Editing)
            {
                Keys[] keys = Input.GetAllKeys(KeyStates.Pressing);

                for (int i = 0; i < keys.Length; i++)
                {
                    if (keys[i] != Keys.Back && Text?.Length >= MaxChars)
                    {
                        Text       = Text.Substring(0, MaxChars);
                        textEdited = true;
                        break;
                    }

                    if (keys[i].IsKeyboard() && !keys[i].IsModifier())
                    {
                        if (keys[i] == Keys.Escape || keys[i] == Keys.Enter)
                        {
                            bool stop = true;
                            if (AllowNewLine)
                            {
                                if (modifiers.HasFlag(KeysModifiers.Shift))
                                {
                                    stop       = false;
                                    Text      += "\n";
                                    textEdited = true;
                                }
                            }
                            if (stop)
                            {
                                this.Editing = false;
                                this.OnLeaveEdit?.Invoke();
                                break;
                            }
                        }
                        else if (keys[i] == Keys.Back)
                        {
                            Text       = Text.Substring(0, WMath.Clamp(Text.Length - 1, 0, int.MaxValue));
                            textEdited = true;
                        }
                        else if (keys[i] == Keys.Space)
                        {
                            Text      += " ";
                            textEdited = true;
                        }
                        else if (keys[i] == Keys.Tab)
                        {
                            Text      += "    ";
                            textEdited = true;
                        }
                        else
                        {
                            textEdited = true;
                            Text      += keys[i].ToString(modifiers);
                        }
                    }
                }
            }

            if (_FirstRun || textEdited)
            {
                _FirstRun = false;

                if (!string.IsNullOrEmpty(this.Text))
                {
                    this.Label.Color = this.TextColor;
                    this.Label.Text  = this.Text;
                }
                else
                {
                    this.Label.Color = this.EmptyTextColor;
                    this.Label.Text  = this.EmptyText;
                }
            }

            if (Editing)
            {
                this.IdleColor  = this.EditingColor;
                this.HoverColor = this.EditingColor;
            }

            else
            {
                this.IdleColor  = this.NotEditingColor;
                this.HoverColor = this.TextInputHoverColor;
            }
        }