bool IsEnoughDelay(Keys key, int forcedDelay)
        {
            MyGuiControlTextboxKeyToString keyEx = m_keyToString[(int)key];

            if (keyEx == null)
            {
                return(true);
            }

            return((MyMinerGame.TotalTimeInMilliseconds - keyEx.LastKeyPressTime) > forcedDelay);
        }
 //  Call this every update, it will check if key as UNPRESSED (thus UP), and if yes, we will remove delay, so multiple
 //  keypresses are possible fast, but not automatic. User must press DOWN/UP.
 void ResetLastKeyPressTimes(MyGuiInput input)
 {
     for (int i = 0; i < m_keyToString.Length; i++)
     {
         MyGuiControlTextboxKeyToString keyEx = m_keyToString[i];
         if (keyEx != null)
         {
             if (input.IsKeyPress(keyEx.Key) == false)
             {
                 keyEx.LastKeyPressTime = MyConstants.FAREST_TIME_IN_PAST;
             }
         }
     }
 }
 /// <summary>
 /// Call this every update, it will check if key as UNPRESSED (thus UP), and if yes, we will remove delay, so multiple
 /// keypresses are possible fast, but not automatic. User must press DOWN/UP.
 /// </summary>
 private void ResetLastKeyPressTimes()
 {
     for (int i = 0; i < m_keyToString.Length; i++)
     {
         MyGuiControlTextboxKeyToString keyEx = m_keyToString[i];
         if (keyEx != null)
         {
             if (MyInput.Static.IsKeyPress(keyEx.Key) == false)
             {
                 keyEx.LastKeyPressTime = MyGuiManager.FAREST_TIME_IN_PAST;
             }
         }
     }
 }
        //  Call this after some specified keypress was detected so we will make delay
        void UpdateLastKeyPressTimes(Keys?key)
        {
            //  This will reset the counter so it starts blinking whenever we enter the textbox
            //  And also when user presses a lot of keys, it won't blink for a while
            m_carriageBlinkerTimer = 0;

            //  Making delays between one long key press
            if (key.HasValue)
            {
                MyGuiControlTextboxKeyToString keyEx = m_keyToString[(int)key];
                if (keyEx != null)
                {
                    keyEx.LastKeyPressTime = MyMinerGame.TotalTimeInMilliseconds;
                }
            }
        }
 private static void AddKey(MyKeys key, string character, string characterWhenShift)
 {
     m_keyToString[(int)key] = new MyGuiControlTextboxKeyToString(key, character, characterWhenShift);
 }
        //  Method returns true if input was captured by control, so no other controls, nor screen can use input in this update
        public override bool HandleInput(MyGuiInput input, bool hasKeyboardActiveControl, bool hasKeyboardActiveControlPrevious, bool isThisFirstHandleInput)
        {
            if (!Visible)
            {
                return(false);
            }

            bool ret = base.HandleInput(input, hasKeyboardActiveControl, hasKeyboardActiveControlPrevious, isThisFirstHandleInput);

            if (ret == false)
            {
                if ((IsMouseOver() == true) && (input.IsNewLeftMousePressed() == true))
                {
                    m_carriagePositionIndex = GetCarriagePositionFromMouseCursor();
                    ret = true;
                }

                if ((m_hasKeyboardActiveControl == true) && (hasKeyboardActiveControlPrevious == false))
                {
                    UpdateLastKeyPressTimes(null);
                }

                if (hasKeyboardActiveControl == true)
                {
                    bool isShiftPressed = input.IsKeyPress(Keys.LeftShift) || input.IsKeyPress(Keys.RightShift);
                    bool isCapsLockOn   = (ushort)MyGuiInput.GetKeyState(0x14) == 1;

                    //(deltaFromLastKeypress > MyGuiConstants.TEXTBOX_KEYPRESS_DELAY)
                    //int deltaFromLastKeypress = MyMinerGame.TotalGameTime_Miliseconds - m_lastKeyPressTime;

                    input.GetPressedKeys(m_pressedKeys);

                    for (int i = 0; i < m_pressedKeys.Count; i++)
                    {
                        System.Diagnostics.Debug.Assert(input.IsKeyPress((Keys)m_pressedKeys[i]));
                    }

                    //  Transform pressed letters, characters, numbers, etc to real character/string
                    for (int i = 0; i < m_pressedKeys.Count; i++)
                    {
                        bool tempShift     = isShiftPressed;
                        bool transformText = true;
                        MyGuiControlTextboxKeyToString pressedKey = m_keyToString[(int)m_pressedKeys[i]];

                        //Allow to enter only digits in case such textbox type is set
                        if (m_type == MyGuiControlTextboxType.DIGITS_ONLY)
                        {
                            if ((pressedKey != null) && !input.IsKeyDigit(pressedKey.Key) && pressedKey.Key != Keys.OemPeriod && pressedKey.Key != Keys.Decimal)
                            {
                                transformText = false;
                            }
                        }

                        if (transformText)
                        {
                            //  If we have mapping from this key to string (that means it's allowed character)
                            if (pressedKey != null && m_text.Length < m_maxLength && pressedKey.Character != null && pressedKey.CharacterWhenShift != null)
                            {
                                ret = true;
                                if (IsEnoughDelay((Keys)m_pressedKeys[i], MyGuiConstants.TEXTBOX_CHANGE_DELAY))
                                {
                                    if (Char.IsLetter(pressedKey.Character, 0))
                                    {
                                        if (isCapsLockOn)
                                        {
                                            tempShift = !tempShift;//carefull here variable is not used anymore so we can invert it
                                        }
                                    }
                                    m_text = m_text.Insert(m_carriagePositionIndex, (tempShift == true) ? pressedKey.CharacterWhenShift : pressedKey.Character);

                                    m_carriagePositionIndex++;
                                    UpdateLastKeyPressTimes((Keys)m_pressedKeys[i]);

                                    OnTextChanged();
                                }
                            }
                        }
                    }

                    //  Move left
                    if ((input.IsKeyPress(Keys.Left)) && (IsEnoughDelay(Keys.Left, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex--;
                        if (m_carriagePositionIndex < 0)
                        {
                            m_carriagePositionIndex = 0;
                        }
                        UpdateLastKeyPressTimes(Keys.Left);
                    }

                    //  Move right
                    if ((input.IsKeyPress(Keys.Right)) && (IsEnoughDelay(Keys.Right, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex++;
                        if (m_carriagePositionIndex > m_text.Length)
                        {
                            m_carriagePositionIndex = m_text.Length;
                        }
                        UpdateLastKeyPressTimes(Keys.Right);
                    }

                    //  Move home
                    if ((input.IsNewKeyPress(Keys.Home)) && (IsEnoughDelay(Keys.Home, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex = 0;
                        UpdateLastKeyPressTimes(Keys.Home);
                    }

                    //  Move end
                    if ((input.IsNewKeyPress(Keys.End)) && (IsEnoughDelay(Keys.End, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        m_carriagePositionIndex = m_text.Length;
                        UpdateLastKeyPressTimes(Keys.End);
                    }

                    //  Delete
                    if ((input.IsKeyPress(Keys.Delete)) && (IsEnoughDelay(Keys.Delete, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        if (m_carriagePositionIndex < m_text.Length)
                        {
                            m_text = m_text.Remove(m_carriagePositionIndex, 1);
                            UpdateLastKeyPressTimes(Keys.Delete);

                            OnTextChanged();
                        }
                    }

                    //  Backspace
                    if ((input.IsKeyPress(Keys.Back)) && (IsEnoughDelay(Keys.Back, MyGuiConstants.TEXTBOX_MOVEMENT_DELAY)))
                    {
                        if (m_carriagePositionIndex > 0)
                        {
                            // Handle text scrolling, basicaly try hold carriage on same position in textBox window (avoid textBox with hidden characters)
                            var carriagePositionChange = GetCarriagePosition(m_carriagePositionIndex) - GetCarriagePosition(m_carriagePositionIndex - 1);;
                            var textAreaPosition       = GetTextAreaPosition();
                            if (m_slidingWindowPosition.X - carriagePositionChange.X >= textAreaPosition.X)
                            {
                                m_slidingWindowPosition.X -= carriagePositionChange.X;
                            }
                            else
                            {
                                m_slidingWindowPosition.X = textAreaPosition.X;
                            }

                            m_text = m_text.Remove(m_carriagePositionIndex - 1, 1);
                            m_carriagePositionIndex--;
                            UpdateLastKeyPressTimes(Keys.Back);

                            OnTextChanged();
                        }
                    }

                    ResetLastKeyPressTimes(input);
                    m_formattedAlready = false;
                }
                else
                {
                    if (m_type == MyGuiControlTextboxType.DIGITS_ONLY && m_formattedAlready == false && !string.IsNullOrEmpty(m_text))
                    {
                        var number        = MyValueFormatter.GetDecimalFromString(m_text, 1);
                        int decimalDigits = (number - (int)number > 0) ? 1 : 0;
                        m_text = MyValueFormatter.GetFormatedFloat((float)number, decimalDigits, "");
                        m_carriagePositionIndex = m_text.Length;
                        m_formattedAlready      = true;
                    }
                }
            }

            return(ret);
        }
 static void AddKey(Keys key, string character, string characterWhenShift)
 {
     m_keyToString[(int)key] = new MyGuiControlTextboxKeyToString(key, character, characterWhenShift);
 }