Ejemplo n.º 1
0
        /// <summary>Called when the control needs to handle the keyboard</summary>
        public override bool HandleKeyboard(WindowMessage msg, IntPtr wParam, IntPtr lParam)
        {
            if (!IsEnabled || !IsVisible)
            {
                return(false);
            }

            // Let the scroll bar have a chance to handle it first
            if (scrollbarControl.HandleKeyboard(msg, wParam, lParam))
            {
                return(true);
            }

            switch (msg)
            {
            case WindowMessage.KeyDown:
            {
                switch ((Keys)wParam.ToInt32())
                {
                case Keys.Up:
                case Keys.Down:
                case Keys.Next:
                case Keys.Prior:
                case Keys.Home:
                case Keys.End:
                {
                    // If no items exists, do nothing
                    if (itemList.Count == 0)
                    {
                        return(true);
                    }

                    int oldSelected = selectedIndex;

                    // Adjust selectedIndex
                    switch ((Keys)wParam.ToInt32())
                    {
                    case Keys.Up:
                        --selectedIndex;
                        break;

                    case Keys.Down:
                        ++selectedIndex;
                        break;

                    case Keys.Next:
                        selectedIndex += scrollbarControl.PageSize - 1;
                        break;

                    case Keys.Prior:
                        selectedIndex -= scrollbarControl.PageSize - 1;
                        break;

                    case Keys.Home:
                        selectedIndex = 0;
                        break;

                    case Keys.End:
                        selectedIndex = itemList.Count - 1;
                        break;
                    }

                    // Clamp the item
                    if (selectedIndex < 0)
                    {
                        selectedIndex = 0;
                    }
                    if (selectedIndex >= itemList.Count)
                    {
                        selectedIndex = itemList.Count - 1;
                    }

                    // Did the selection change?
                    if (oldSelected != selectedIndex)
                    {
                        if (ctrlStyle == ListBoxStyle.MultiSelection)
                        {
                            // Clear all selection
                            for (int i = 0; i < itemList.Count; i++)
                            {
                                ListItem lbi = itemList[i];
                                lbi.IsItemSelected = false;
                                itemList[i]        = lbi;
                            }

                            // Is shift being held down?
                            bool shiftDown = WinFormsUtils.IsKeyDown(Keys.ShiftKey);

                            if (shiftDown)
                            {
                                // Select all items from the start selection to current selected index
                                int end = Math.Max(selectedStarted, selectedIndex);
                                for (int i = Math.Min(selectedStarted, selectedIndex); i <= end; ++i)
                                {
                                    ListItem lbi = itemList[i];
                                    lbi.IsItemSelected = true;
                                    itemList[i]        = lbi;
                                }
                            }
                            else
                            {
                                ListItem lbi = itemList[selectedIndex];
                                lbi.IsItemSelected      = true;
                                itemList[selectedIndex] = lbi;

                                // Update selection start
                                selectedStarted = selectedIndex;
                            }
                        }
                        else         // Update selection start
                        {
                            selectedStarted = selectedIndex;
                        }

                        // adjust scrollbar
                        scrollbarControl.ShowItem(selectedIndex);
                        RaiseSelectionEvent(this, true);
                    }
                }
                    return(true);
                }
                break;
            }
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>Handle keyboard input to the edit box</summary>
        public override bool HandleKeyboard(WindowMessage msg, IntPtr wParam, IntPtr lParam)
        {
            if (!IsEnabled || !IsVisible)
            {
                return(false);
            }

            // Default to not handling the message
            bool isHandled = false;

            if (msg == WindowMessage.KeyDown)
            {
                switch ((Keys)wParam.ToInt32())
                {
                case Keys.End:
                case Keys.Home:
                    // Move the caret
                    PlaceCaret(wParam.ToInt32() == (int)Keys.End ? textData.Text.Length : 0);
                    if (!WinFormsUtils.IsKeyDown(Keys.ShiftKey))
                    {
                        // Shift is not down. Update selection start along with caret
                        textData.SelectionStart = caretPosition;
                    }

                    ResetCaretBlink();
                    isHandled = true;
                    break;

                case Keys.Insert:
                    if (WinFormsUtils.IsKeyDown(Keys.ControlKey))
                    {
                        // Control insert -> Copy to clipboard
                        CopyToClipboard();
                    }
                    else if (WinFormsUtils.IsKeyDown(Keys.ShiftKey))
                    {
                        // Shift insert -> Paste from clipboard
                        PasteFromClipboard();
                    }
                    else
                    {
                        // Toggle insert mode
                        isInsertMode = !isInsertMode;
                    }
                    break;

                case Keys.Delete:
                    // Check to see if there is a text selection
                    if (caretPosition != textData.SelectionStart)
                    {
                        DeleteSelectionText();
                        RaiseChangedEvent(this, true);
                    }
                    else
                    {
                        if (caretPosition < textData.Text.Length)
                        {
                            // Deleting one character
                            textData.Text = textData.Text.Remove(caretPosition, 1);
                            RaiseChangedEvent(this, true);
                        }
                    }
                    ResetCaretBlink();
                    isHandled = true;
                    break;

                case Keys.Left:
                    if (WinFormsUtils.IsKeyDown(Keys.ControlKey))
                    {
                        // Control is down. Move the caret to a new item
                        // instead of a character.
                    }
                    else if (caretPosition > 0)
                    {
                        PlaceCaret(caretPosition - 1);     // Move one to the left
                    }
                    if (!WinFormsUtils.IsKeyDown(Keys.ShiftKey))
                    {
                        // Shift is not down. Update selection
                        // start along with the caret.
                        textData.SelectionStart = caretPosition;
                    }
                    ResetCaretBlink();
                    isHandled = true;
                    break;

                case Keys.Right:
                    if (WinFormsUtils.IsKeyDown(Keys.ControlKey))
                    {
                        // Control is down. Move the caret to a new item
                        // instead of a character.
                    }
                    else if (caretPosition < textData.Text.Length)
                    {
                        PlaceCaret(caretPosition + 1);     // Move one to the left
                    }
                    if (!WinFormsUtils.IsKeyDown(Keys.ShiftKey))
                    {
                        // Shift is not down. Update selection
                        // start along with the caret.
                        textData.SelectionStart = caretPosition;
                    }
                    ResetCaretBlink();
                    isHandled = true;
                    break;

                case Keys.Up:
                case Keys.Down:
                    // Trap up and down arrows so that the dialog
                    // does not switch focus to another control.
                    isHandled = true;
                    break;

                default:
                    // Let the application handle escape
                    isHandled = ((Keys)wParam.ToInt32()) == Keys.Escape;
                    break;
                }
            }

            return(isHandled);
        }