Example #1
0
        private void HandlePress(KeyboardState state)
        {
            bool shift    = false;
            bool capslock = false;

            int previousCaretPosition = caretPosition;

            if (state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift))
            {
                shift = true;
            }
            // jos on sallittu liikkuminen tekstissä
            if (IsTravelsingEnabled)
            {
                // halutaan päästä vasemmalle
                if (state.IsKeyDown(Keys.Left))
                {
                    caretPosition = (int)MathHelper.Clamp(caretPosition - 1, 0, int.MaxValue);
                }
                // halutaan oikealle
                else if (state.IsKeyDown(Keys.Right))
                {
                    caretPosition = (int)MathHelper.Clamp(caretPosition + 1, 0, text.Length);
                }
                // halutaan tekstin loppuun
                else if (state.IsKeyDown(Keys.End))
                {
                    caretPosition = text.Length;
                    if (caretPosition < 0)
                    {
                        caretPosition = 0;
                    }
                }
                // halutaan tekstin alkuun
                else if (state.IsKeyDown(Keys.Home))
                {
                    caretPosition = 0;
                }

                if (IsTextSelectionEnabled && !shift)
                {
                    if (previousCaretPosition != caretPosition)
                    {
                        // selectionia ei ole ennää
                        SelectionStartIndex = -1;
                        SelectionEndIndex   = -1;
                        selectedText        = "";
                    }
                }
            }

            if (IsTextSelectionEnabled)
            {
                if ((state.IsKeyDown(Keys.LeftControl) || state.IsKeyDown(Keys.RightControl)) && state.IsKeyDown(Keys.A))
                {
                    selectedText        = text.ToString();
                    SelectionStartIndex = 0;
                    SelectionEndIndex   = text.Length;
                    return;
                }
                else if (shift)
                {
                    if (state.IsKeyDown(Keys.Left))
                    {
                        // ei ole selectionia
                        if (SelectionStartIndex == -1)
                        {
                            SelectionStartIndex = caretPosition;
                            if (SelectionEndIndex == -1)
                            {
                                SelectionEndIndex = caretPosition + 1;
                            }
                        }
                        if (caretPosition < SelectionStartIndex)
                        {
                            SelectionStartIndex = caretPosition;
                        }
                        if (caretPosition > SelectionStartIndex)
                        {
                            SelectionEndIndex = caretPosition;
                        }
                    }
                    else if (state.IsKeyDown(Keys.Right))
                    {
                        if (SelectionEndIndex == -1)
                        {
                            SelectionStartIndex = caretPosition - 1;
                            SelectionEndIndex   = caretPosition;
                            return;
                        }
                        if (caretPosition > SelectionEndIndex)
                        {
                            SelectionEndIndex = caretPosition;
                        }
                        if (caretPosition - 1 == SelectionStartIndex)
                        {
                            SelectionStartIndex = caretPosition;

                            if (SelectionStartIndex == SelectionEndIndex)
                            {
                                SelectionEndIndex   = -1;
                                SelectionStartIndex = -1;
                                selectedText        = "";
                            }
                        }
                    }
                    // selection nykyisestä kohdasta alkuun
                    else if (state.IsKeyDown(Keys.Home))
                    {
                        if (Text.Length != 0 && previousCaretPosition != 0)
                        {
                            SelectionStartIndex = 0;
                            SelectionEndIndex   = previousCaretPosition;
                        }
                    }
                    // halutaan loppuun
                    else if (state.IsKeyDown(Keys.End))
                    {
                        if (Text.Length != 0 && previousCaretPosition != Text.Length)
                        {
                            SelectionStartIndex = previousCaretPosition;
                            SelectionEndIndex   = caretPosition;
                        }
                    }
                    if (SelectionStartIndex != -1 && SelectionEndIndex != -1)
                    {
                        selectedText = Text.Substring(SelectionStartIndex, SelectionEndIndex - SelectionStartIndex);
                    }
                }
                HandleCopyPaste(state);
            }

            // halutaan pyyhkiä tekstiä
            if (state.IsKeyDown(Keys.Back))
            {
                if (text.Length != 0)
                {
                    if (IsTextSelectionEnabled)
                    {
                        // jos on valittu jotain
                        if (!SelectedText.Equals(""))
                        {
                            caretPosition = SelectionStartIndex;
                            text.Remove(SelectionStartIndex, selectedText.Length);
                            selectedText        = "";
                            SelectionStartIndex = -1;
                            SelectionEndIndex   = -1;
                            return;
                        }
                    }
                    // jos ollaan tekstin alussa ei tarvi tehä yhtään mitään
                    if (caretPosition == 0)
                    {
                        return;
                    }
                    text.Remove(caretPosition - 1, 1);
                    caretPosition--;
                }
                caretPosition = (int)MathHelper.Clamp(caretPosition, 0, text.Length);
            }
            else
            {
                Keys[]      pressed     = state.GetPressedKeys();
                List <Keys> pressedKeys = pressed.ToList();
                if (lastKeys.Length != pressed.Length)
                {
                    for (int i = pressed.Length - 1; i >= 0; i--)
                    {
                        if (lastKeys.Contains(pressed[i]))
                        {
                            pressedKeys.Remove(pressedKeys[i]);
                        }
                    }
                }
                foreach (Keys key in pressedKeys)
                {
                    string s = TranslateKey(key, shift, capslock);
                    if (s.Length != 0)
                    {
                        if (IsTextSelectionEnabled && IsTextSelected)
                        {
                            text.Remove(SelectionStartIndex, SelectionEndIndex - SelectionStartIndex);
                            text.Insert(SelectionStartIndex, s);
                            caretPosition       = SelectionStartIndex + 1;
                            SelectionEndIndex   = -1;
                            SelectionStartIndex = -1;
                            selectedText        = "";
                        }
                        else
                        {
                            text.Insert(caretPosition, s);
                            caretPosition++;
                        }
                    }
                    break;
                }
            }
        }