Example #1
0
        public bool Check()
        {
            if (KeyboardComponent.KeyReleased(_key))
            {
                _tempTime = TimeBetweenRepeat;
            }

            if (KeyboardComponent.KeyPressed(_key))
            {
                return(true);
            }

            if (KeyboardComponent.KeyHolded(_key, RequireElapsedTime) &&
                KeyboardComponent.TotalTimeKeyPressed[_key] >= _tempTime)
            {
                // _tempTime == TimeBetweenRepeat its like
                // KeyHoldedFirstTriggering possibly variable
                // If we do not do it we notice undesirable triggering
                // (RequireElapsedTime.Miliseconds / TimeBetweenRepeat.Miliseconds) times
                if (_tempTime == TimeBetweenRepeat)
                {
                    _tempTime += RequireElapsedTime;
                }

                _tempTime += TimeBetweenRepeat;
                return(true);
            }

            return(false);
        }
Example #2
0
        public static void Load(Game game)
        {
            if (Loaded)
            {
                throw new InvalidOperationException("Keyboard component already loaded");
            }

            Instance = new KeyboardComponent(game);
            game.Components.Add(Instance);
            Loaded = true;
            game.Components.ComponentRemoved += CheckForForceRemove;
        }
Example #3
0
        public override void Update(GameTime gameTime)
        {
            if (!Enabled)
            {
                return;
            }

            base.Update(gameTime);

            if (_leftArrowTracer.Check() && CursorPosition > 0)
            {
                int oldCursorPosition = CursorPosition;

                if (KeyboardComponent.IsKeyDown(Keys.LeftControl) || KeyboardComponent.IsKeyDown(Keys.RightControl))
                {
                    CursorPosition = GetIndexOfPreviousSection(Text, _sectionSplitSymbols, CursorPosition);
                }
                else
                {
                    CursorPosition--;
                }

                if (KeyboardComponent.IsKeyDown(Keys.LeftShift) || KeyboardComponent.IsKeyDown(Keys.RightShift))
                {
                    if (IsTextHighlighted)
                    {
                        int highlightedCount = oldCursorPosition - CursorPosition;
                        for (int i = 0; i < highlightedCount; i++)
                        {
                            if (CursorPosition < HighlightRange.X)
                            {
                                HighlightRange -= new Point(1, 0);
                            }
                            else
                            {
                                HighlightRange -= new Point(0, 1);
                            }
                        }
                    }
                    else
                    {
                        HighlightRange = new Point(CursorPosition, oldCursorPosition);
                    }
                }
                else
                {
                    ResetHighlighting();
                }
            }

            if (_rightArrowTracer.Check() && CursorPosition < _buffer.Length)
            {
                int oldCursorPosition = CursorPosition;

                if (KeyboardComponent.IsKeyDown(Keys.LeftControl) || KeyboardComponent.IsKeyDown(Keys.RightControl))
                {
                    CursorPosition = GetIndexOfNextSection(Text, _sectionSplitSymbols, CursorPosition);
                }
                else
                {
                    CursorPosition++;
                }

                if (KeyboardComponent.IsKeyDown(Keys.LeftShift) || KeyboardComponent.IsKeyDown(Keys.RightShift))
                {
                    if (IsTextHighlighted)
                    {
                        int highlightedCount = CursorPosition - oldCursorPosition;
                        for (int i = 0; i < highlightedCount; i++)
                        {
                            if (HighlightRange.Y >= CursorPosition)
                            {
                                HighlightRange += new Point(1, 0);
                            }
                            else
                            {
                                HighlightRange += new Point(0, 1);
                            }
                        }
                    }
                    else
                    {
                        HighlightRange = new Point(oldCursorPosition, CursorPosition);
                    }
                }
                else
                {
                    ResetHighlighting();
                }
            }

            if (_deleteTracer.Check())
            {
                // I send unused range of Unicode symbols
                // for reduce count of code (for example \u0010 is DLE symbol (Data link escape))
                if (KeyboardComponent.IsKeyDown(Keys.LeftControl) || KeyboardComponent.IsKeyDown(Keys.RightControl))
                {
                    WriteControlToBuffer('\u0010');
                }
                else
                {
                    WriteControlToBuffer('\u0000');
                }
            }
        }