Example #1
0
        private void ResultKeyDownHandler(object sender, KeyEventArgs e)
        {
            if ((e.Key >= Key.D0 && e.Key <= Key.D9))
            {
                var num = e.Key - Key.D0;
                NumMod   += num.ToString();
                e.Handled = true;
                return;
            }
            var maxCount = resultsGrid.Items.Count;

            switch (e.Key)
            {
            case Key.Tab:
            {
                e.Handled = true;

                var lastFocus = Keyboard.FocusedElement as FrameworkElement;

                if (e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift))
                {
                    // focus previous section
                    _deckWindow.ChangeActiveSection(-1);
                }
                else
                {
                    // focus next section
                    _deckWindow.ChangeActiveSection(1);
                }

                if (lastFocus != null)
                {
                    Keyboard.Focus(lastFocus);
                }
                break;
            }

            case Key.Escape:
            {
                NumMod    = "";
                e.Handled = true;
                break;
            }

            case Key.G:
            {
                if (Keyboard.IsKeyDown(Key.LeftShift & Key.RightShift))
                {
                    resultsGrid.SelectedItem = resultsGrid.Items[maxCount - 1];
                    resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                    e.Handled = true;
                }
                break;
            }

            case Key.End:
            {
                resultsGrid.SelectedItem = resultsGrid.Items[maxCount - 1];
                resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                e.Handled = true;
                break;
            }

            case Key.Home:
            {
                resultsGrid.SelectedItem = resultsGrid.Items[0];
                resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                e.Handled = true;
                break;
            }
            }

            if (e.Handled)
            {
                return;
            }

            var row = (DataRowView)resultsGrid.SelectedItem;

            if (row == null)
            {
                return;
            }

            var loopNum = 1;

            if (!String.IsNullOrWhiteSpace(NumMod))
            {
                loopNum = NumMod.ToInt32();
            }
            NumMod = "";

            var gridItemContainer = resultsGrid.ItemContainerGenerator.ContainerFromItem(resultsGrid.SelectedItem);

            if (gridItemContainer == null)
            {
                return;
            }
            var gridItemIdx = resultsGrid.ItemContainerGenerator.IndexFromContainer(gridItemContainer);

            for (var i = 0; i < loopNum; i++)
            {
                switch (e.Key)
                {
                case Key.Insert:
                case Key.Add:
                case Key.A:
                case Key.I:
                case Key.Enter:
                {
                    if (CardAdded != null)
                    {
                        var rowid = row["id"] as string;
                        if (rowid != null)
                        {
                            CardAdded(this, new SearchCardIdEventArgs {
                                    CardId = Guid.Parse(rowid)
                                });
                        }
                    }
                    e.Handled = true;
                    break;
                }

                case Key.Delete:
                case Key.D:
                case Key.Subtract:
                {
                    if (CardRemoved != null)
                    {
                        var rowid = row["id"] as string;
                        if (rowid != null)
                        {
                            CardRemoved(this, new SearchCardIdEventArgs {
                                    CardId = Guid.Parse(rowid)
                                });
                        }
                    }
                    e.Handled = true;
                    break;
                }

                case Key.J:
                case Key.Down:
                {
                    //down
                    var newIdx = gridItemIdx + loopNum;
                    if (newIdx >= maxCount)
                    {
                        newIdx = maxCount - 1;
                    }
                    resultsGrid.SelectedItem = resultsGrid.Items[newIdx];
                    resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                    e.Handled = true;
                    i         = loopNum;
                    break;
                }

                case Key.K:
                case Key.Up:
                {
                    //up
                    var newIdx = gridItemIdx - loopNum;
                    if (newIdx < 0)
                    {
                        newIdx = 0;
                    }
                    resultsGrid.SelectedItem = resultsGrid.Items[newIdx];
                    resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                    e.Handled = true;
                    i         = loopNum;
                    break;
                }
                }
            }
        }
        private void ResultKeyDownHandler(object sender, KeyEventArgs e)
        {
            if ((e.Key >= Key.D0 && e.Key <= Key.D9))
            {
                var num = e.Key - Key.D0;
                NumMod   += num.ToString();
                e.Handled = true;
                return;
            }

            switch (e.Key)
            {
            case Key.Tab:
            {
                var lastFocus = Keyboard.FocusedElement as FrameworkElement;

                var cont = _deckWindow.PlayerCardSections.ItemContainerGenerator.ContainerFromItem(_deckWindow.ActiveSection);
                var idx  = _deckWindow.PlayerCardSections.ItemContainerGenerator.IndexFromContainer(cont);
                if (idx + 1 >= _deckWindow.PlayerCardSections.Items.Count)
                {
                    idx = 0;
                }
                else
                {
                    idx++;
                }
                var nc        = (ContentPresenter)_deckWindow.PlayerCardSections.ItemContainerGenerator.ContainerFromIndex(idx);
                var presenter = VisualTreeHelper.GetChild(nc, 0);

                (presenter as Expander).Focus();
                //lastFocus.Focus();
                //resultsGrid.Focus();
                if (lastFocus != null)
                {
                    Keyboard.Focus(lastFocus);
                }
                e.Handled = true;
                break;
            }

            case Key.Escape:
            {
                NumMod    = "";
                e.Handled = true;
                break;
            }

            case Key.G:
            {
                if (Keyboard.IsKeyDown(Key.LeftShift & Key.RightShift))
                {
                    resultsGrid.SelectedItem = resultsGrid.Items[resultsGrid.Items.Count - 1];
                    resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                    e.Handled = true;
                }
                break;
            }
            }

            if (e.Handled)
            {
                return;
            }

            var row = (DataRowView)resultsGrid.SelectedItem;

            if (row == null)
            {
                return;
            }

            var loopNum = 1;

            if (!String.IsNullOrWhiteSpace(NumMod))
            {
                loopNum = NumMod.ToInt32();
            }
            NumMod = "";
            var gridItemContainer = resultsGrid.ItemContainerGenerator.ContainerFromItem(resultsGrid.SelectedItem);

            if (gridItemContainer == null)
            {
                return;
            }
            var gridItemIdx = resultsGrid.ItemContainerGenerator.IndexFromContainer(gridItemContainer);
            var maxCount    = resultsGrid.Items.Count;

            for (var i = 0; i < loopNum; i++)
            {
                switch (e.Key)
                {
                case Key.Insert:
                case Key.Add:
                case Key.A:
                case Key.I:
                case Key.Enter:
                    if (CardAdded != null)
                    {
                        var rowid = row["id"] as string;
                        if (rowid != null)
                        {
                            CardAdded(this, new SearchCardIdEventArgs {
                                CardId = Guid.Parse(rowid)
                            });
                        }
                    }
                    e.Handled = true;
                    break;

                case Key.Delete:
                case Key.D:
                case Key.Subtract:
                    if (CardRemoved != null)
                    {
                        var rowid = row["id"] as string;
                        if (rowid != null)
                        {
                            CardRemoved(this, new SearchCardIdEventArgs {
                                CardId = Guid.Parse(rowid)
                            });
                        }
                    }
                    e.Handled = true;
                    break;

                case Key.J:
                {
                    //down
                    var newIdx = gridItemIdx + loopNum;
                    newIdx = (newIdx) % (maxCount + 1);
                    resultsGrid.SelectedItem = resultsGrid.Items[newIdx];
                    resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                    e.Handled = true;
                    i         = loopNum;
                    break;
                }

                case Key.K:
                {
                    //up
                    var newIdx = gridItemIdx - loopNum;
                    newIdx = Math.Abs(newIdx);
                    newIdx = (newIdx) % (maxCount + 1);
                    resultsGrid.SelectedItem = resultsGrid.Items[newIdx];
                    resultsGrid.ScrollIntoView(resultsGrid.SelectedItem);
                    e.Handled = true;
                    i         = loopNum;
                    break;
                }
                }
            }
        }