Exemple #1
0
        /// <summary>
        /// Handles the KeyDown event for ButtonBase.
        /// </summary>
        /// <param name="key">
        /// The keyboard key associated with the event.
        /// </param>
        /// <returns>True if the event was handled, false otherwise.</returns>
        /// <remarks>
        /// This method exists for the purpose of unit testing since we can't
        /// set KeyEventArgs.Key to simulate key press events.
        /// </remarks>
        internal virtual bool OnKeyDownInternal(Key key)
        {
            // True if the button will handle the event, false otherwise.
            bool handled = false;

            // Key presses can be ignored when disabled or in ClickMode.Hover
            if (IsEnabled && (ClickMode != ClickMode.Hover))
            {
                // Hitting the SPACE key is equivalent to pressing the mouse
                // button
                if (key == Key.Space)
                {
                    // Ignore the SPACE key if we already have the mouse
                    // captured
                    if (!_isMouseCaptured)
                    {
                        _isSpaceKeyDown = true;
                        IsPressed       = true;
                        CaptureMouseInternal();

                        if (ClickMode == ClickMode.Press)
                        {
                            OnClick();
                        }

                        handled = true;
                    }
                }
                // The ENTER key forces a click
                else if ((key == Key.Enter) && KeyboardNavigation.GetAcceptsReturn(this))
                {
                    _isSpaceKeyDown = false;
                    IsPressed       = false;
                    ReleaseMouseCaptureInternal();

                    OnClick();

                    handled = true;
                }
                // Any other keys pressed are irrelevant
                else if (_isSpaceKeyDown)
                {
                    IsPressed       = false;
                    _isSpaceKeyDown = false;
                    ReleaseMouseCaptureInternal();
                }
            }

            return(handled);
        }
        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
            case Key.Space:
            {
                CycleNext();
                e.Handled = true;
                break;
            }

            case Key.Enter:
            {
                if (KeyboardNavigation.GetAcceptsReturn(this))
                {
                    CycleNext();
                    e.Handled = true;
                }
                break;
            }

            case Key.Left:
            {
                if (Index > 0)
                {
                    --SelectedIndex;
                }
                e.Handled = true;
                break;
            }

            case Key.Right:
            {
                if (Index < LastIndex)
                {
                    ++SelectedIndex;
                }
                e.Handled = true;
                break;
            }
            }
        }
Exemple #3
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (!e.Handled)
            {
                bool handled         = false;
                int  newFocusedIndex = -1;
                switch (e.Key)
                {
                case Key.Space:
                case Key.Enter:
                    if ((Key.Enter != e.Key) || KeyboardNavigation.GetAcceptsReturn(this))
                    {
                        if (ModifierKeys.Alt != (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Alt)))
                        {
                            // KeyEventArgs.OriginalSource (used by WPF) isn't available in Silverlight; use FocusManager.GetFocusedElement instead
                            ListBoxItem listBoxItem = FocusManager.GetFocusedElement() as ListBoxItem;
                            if (null != listBoxItem)
                            {
                                if ((ModifierKeys.Control == (Keyboard.Modifiers & ModifierKeys.Control)) && listBoxItem.IsSelected)
                                {
                                    SelectedItem = null;
                                }
                                else
                                {
                                    SelectedItem = ItemContainerGenerator.ItemFromContainer(listBoxItem);
                                }
                                handled = true;
                            }
                        }
                    }
                    break;

                case Key.Home:
                    newFocusedIndex = 0;
                    break;

                case Key.End:
                    newFocusedIndex = Items.Count - 1;
                    break;

                case Key.PageUp:
                    newFocusedIndex = NavigateByPage(false);
                    break;

                case Key.PageDown:
                    newFocusedIndex = NavigateByPage(true);
                    break;

                case Key.Left:
                    if (IsVerticalOrientation())
                    {
                        ElementScrollViewerScrollInDirection(Key.Left);
                    }
                    else
                    {
                        newFocusedIndex = _focusedIndex - 1;
                    }
                    break;

                case Key.Up:
                    if (IsVerticalOrientation())
                    {
                        newFocusedIndex = _focusedIndex - 1;
                    }
                    else
                    {
                        ElementScrollViewerScrollInDirection(Key.Up);
                    }
                    break;

                case Key.Right:
                    if (IsVerticalOrientation())
                    {
                        ElementScrollViewerScrollInDirection(Key.Right);
                    }
                    else
                    {
                        newFocusedIndex = _focusedIndex + 1;
                    }
                    break;

                case Key.Down:
                    if (IsVerticalOrientation())
                    {
                        newFocusedIndex = _focusedIndex + 1;
                    }
                    else
                    {
                        ElementScrollViewerScrollInDirection(Key.Down);
                    }
                    break;

                // case Key.Divide:  // Feature only used by SelectionMode.Extended
                // case Key.Oem2:  // Key not supported by Silverlight
                //    break;
                // case Key.Oem5:  // Key not supported by Silverlight
                //     break;
                default:
                    Debug.Assert(!handled);
                    break;
                }
                if ((-1 != newFocusedIndex) &&
                    (-1 != _focusedIndex) &&
                    (newFocusedIndex != _focusedIndex) &&
                    (0 <= newFocusedIndex) &&
                    (newFocusedIndex < Items.Count))
                {
                    // A key press will change the focused ListBoxItem
                    ListBoxItem listBoxItem = (ListBoxItem)ItemContainerGenerator.ContainerFromIndex(newFocusedIndex);
                    Debug.Assert(null != listBoxItem);
                    ScrollIntoView(ItemContainerGenerator.ItemFromContainer(listBoxItem));
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        listBoxItem.Focus();
                    }
                    else
                    {
                        SelectedItem = ItemContainerGenerator.ItemFromContainer(listBoxItem);
                    }
                    handled = true;
                }
                if (handled)
                {
                    e.Handled = true;
                }
            }
        }