Exemplo n.º 1
0
        private void HandleKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && !(e.Shift || e.Alt))
            {
                HandleControlKeyDown(sender, e);
                return;
            }

            e.Handled = true;
            if (Keys.Escape == e.KeyCode)
            {
                Select(_promptPosition, EndOfLinePosition);
                UpdateCountsAndDeleteSelection();
                FlushInputBuffer();
            }
            else if (Keys.Enter == e.KeyCode)
            {
                NotifyCommandEntered();
            }
            else if (Keys.Tab == e.KeyCode)
            {
                if (String.IsNullOrEmpty(_tabExpansionInput))
                {
                    Select(_promptPosition, SelectionStart);
                    _tabExpansionInput = SelectedText;
                }

                string cmd = String.Empty;
                if (!e.Shift)
                {
                    cmd = AutoCompleteWalker.NextUp(_tabExpansionInput);
                }
                else
                {
                    cmd = AutoCompleteWalker.NextDown(_tabExpansionInput);
                }

                if (String.IsNullOrEmpty(cmd))
                {
                    Select(EndOfLinePosition, EndOfLinePosition);
                    return;
                }

                Select(_promptPosition, EndOfLinePosition);
                InsertText(cmd);
            }
            else if (Keys.PageUp == e.KeyCode)
            {
                string cmd = HistoryStackWalker.Oldest();
                if (String.IsNullOrEmpty(cmd))
                {
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.PageDown == e.KeyCode)
            {
                string cmd = HistoryStackWalker.Newest();
                if (String.IsNullOrEmpty(cmd))
                {
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.Up == e.KeyCode)
            {
                string cmd = HistoryStackWalker.NextUp();
                if (String.IsNullOrEmpty(cmd))
                {
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.Down == e.KeyCode)
            {
                string cmd = HistoryStackWalker.NextDown();
                if (String.IsNullOrEmpty(cmd))
                {
                    MoveCaretEOL(false);
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.Back == e.KeyCode)
            {
                if (SelectionStart >= _promptPosition)
                {
                    if (SelectionStart != _promptPosition && SelectionLength == 0)
                    {
                        int index = SelectionStart - 1;
                        Select(index, 1);
                    }
                    UpdateCountsAndDeleteSelection();
                }
            }
            else if (Keys.Delete == e.KeyCode)
            {
                if (EndOfLinePosition > SelectionStart)
                {
                    if (SelectionLength == 0)
                    {
                        int index = SelectionStart;
                        Select(index, 1);
                    }
                    UpdateCountsAndDeleteSelection();
                }
            }
            else if (Keys.Home == e.KeyCode)
            {
                MoveCaretHome(e.Shift);
            }
            else if (Keys.End == e.KeyCode)
            {
                MoveCaretEOL(e.Shift);
            }
            else
            {
                e.Handled = false;
            }
        }