예제 #1
0
        public override bool OnKeyDown(KeyInfo key)
        {
            if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar)
            {
                if (this.IsSelected)
                {
                    this.Invoke?.Invoke(this, EventArgs.Empty);
                    this.HideMenu();
                    return(false);
                }
                else
                {
                    var selectedIndex = this.SubItems.IndexOf(x => x.IsSelected);
                    if (selectedIndex > -1)
                    {
                        this.SubItems[selectedIndex].Invoke?.Invoke(this.SubItems[selectedIndex], EventArgs.Empty);
                        this.HideMenu();
                        return(false);
                    }
                }
            }

            if (this.IsMenuOpen && key.Key == ConsoleKey.LeftArrow)
            {
                if (Parent is MenuStrip menu)
                {
                    var index = menu.Controls.IndexOf(this);
                    if (index > 0)
                    {
                        menu.ControlAt <MenuItem>(index - 1).ShowMenu();
                        this.HideMenu();
                        return(false);
                    }
                }
                else
                {
                    throw new NotImplementedException("Cannot navigate in sub menu items yet");
                }
            }
            else if (this.IsMenuOpen && key.Key == ConsoleKey.RightArrow)
            {
                if (Parent is MenuStrip menu)
                {
                    var index = menu.Controls.IndexOf(this);
                    if (index + 1 < menu.Controls.Count)
                    {
                        menu.ControlAt <MenuItem>(index + 1).ShowMenu();
                        menu.Controls[index + 1].StopNextEvent();
                        this.HideMenu();
                    }
                }
                else
                {
                    throw new NotImplementedException("Cannot navigate in sub menu items yet");
                }
            }
            else if (this.IsMenuOpen && key.Key == ConsoleKey.DownArrow)
            {
                if (Parent is MenuStrip menu)
                {
                    if (this.SubItems.Count > 0)
                    {
                        this.IsSelected = false;
                        var index         = 1;
                        var selectedIndex = this.SubItems.IndexOf(x => x.IsSelected);
                        while (selectedIndex + index < this.SubItems.Count)
                        {
                            if (selectedIndex >= 0)
                            {
                                this.SubItems[selectedIndex].IsSelected = false;
                            }
                            if (this.SubItems[selectedIndex + index] is SeparatorMenuItem || !this.SubItems[selectedIndex + index].IsEnabled)
                            {
                                index++;
                                continue;
                            }
                            this.SubItems[selectedIndex + index].IsSelected = true;
                            this.SubItems[selectedIndex + index].StopNextEvent();
                            break;
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException("Cannot navigate in sub menu items yet");
                }
            }
            else if (this.IsMenuOpen && key.Key == ConsoleKey.UpArrow)
            {
                if (Parent is MenuStrip menu)
                {
                    if (this.SubItems.Count > 0)
                    {
                        var selectedIndex = this.SubItems.IndexOf(x => x.IsSelected);
                        var index         = 1;
                        while (selectedIndex - index >= 0)
                        {
                            this.SubItems[selectedIndex].IsSelected = false;
                            if (this.SubItems[selectedIndex - index] is SeparatorMenuItem || !this.SubItems[selectedIndex - index].IsEnabled)
                            {
                                index++;
                                continue;
                            }
                            this.SubItems[selectedIndex - index].IsSelected = true;
                            break;
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException("Cannot navigate in sub menu items yet");
                }
            }
            else
            {
                // if my menu is open, we should focus on them instead. so we can have same trigger key
                if (this.IsMenuOpen)
                {
                    MenuItem selection = null;
                    foreach (var item in this.SubItems)
                    {
                        var ops        = ParseDrawOperations(item.Text);
                        var hotkeyChar = ops.FirstOrDefault(x => x.ForegroundColor != this.ForegroundColor);
                        if (hotkeyChar == null)
                        {
                            continue;
                        }
                        if (char.ToLower(hotkeyChar.Text[0]) == char.ToLower(key.KeyChar))
                        {
                            this.IsSelected = false;
                            item.IsSelected = true;
                            selection       = item;
                        }
                    }
                    if (selection != null)
                    {
                        SubItems.Where(x => x != selection).ForEach(x => x.IsSelected = false);
                        return(false);
                    }
                    this.HideMenu();
                }
                else
                {
                    var ops        = ParseDrawOperations(this.Text);
                    var hotkeyChar = ops.FirstOrDefault(x => x.ForegroundColor != this.ForegroundColor);
                    if (hotkeyChar == null)
                    {
                        return(true);
                    }

                    if (char.ToLower(hotkeyChar.Text[0]) == char.ToLower(key.KeyChar))
                    {
                        if (this.Parent is MenuStrip strip)
                        {
                            strip.Controls.OfType <MenuItem>().ForEach(x => x.HideMenu());
                        }
                        else if (this.Parent is MenuItem mi)
                        {
                            mi.SubItems.ForEach(x => x.HideMenu());
                        }
                        this.ShowMenu();
                        return(false); // stops other items from receiving keydown events
                    }
                }
            }

            return(true);
        }
예제 #2
0
파일: TextBox.cs 프로젝트: Shinobytes/Dosui
        public override bool OnKeyDown(KeyInfo key)
        {
            var topBorder   = this.BorderThickness.Top > 0 ? 1 : 0;
            var rightBorder = this.BorderThickness.Right > 0 ? 1 : 0;
            var width       = AreaEdit ? this.Size.Width : (this.Text?.Length + 1) ?? 0;
            var height      = AreaEdit ? this.Size.Height : 1;

            if (this.IsArrowKey(key.Key))
            {
                if (key.Key == ConsoleKey.RightArrow)
                {
                    this.CursorX++;
                    if (this.CursorX >= width)
                    {
                        this.CursorX = width - rightBorder;
                        if (this.CursorY < (height - topBorder))
                        {
                            this.CursorX = 0;
                            this.CursorY++;
                        }
                    }
                }
                else if (key.Key == ConsoleKey.LeftArrow)
                {
                    this.CursorX--;
                    if (this.CursorX < 0)
                    {
                        this.CursorX = 0;
                        if (this.CursorY > 0)
                        {
                            this.CursorX = width - rightBorder;
                            this.CursorY--;
                        }
                    }
                }
                else
                {
                    if (!AreaEdit)
                    {
                        return(HandleNavigationKeys(key));// up and down should allow navigation if we are not an area edit
                    }

                    if (key.Key == ConsoleKey.UpArrow)
                    {
                        this.CursorY--;
                        if (this.CursorY < 0)
                        {
                            this.CursorY = 0;
                        }
                    }

                    if (key.Key == ConsoleKey.DownArrow)
                    {
                        this.CursorY++;
                        if (this.CursorY >= (height - topBorder))
                        {
                            this.CursorY = height - topBorder;
                        }
                    }
                }

                // make sure the cursor is always visible when moving it
                this.cursorAnimationState = true;
                return(false);
            }

            if (key.Key == ConsoleKey.Backspace)
            {
                if (this.Text.Length > 0)
                {
                    if (AreaEdit)
                    {
                        // ...
                    }
                    else
                    {
                        if (this.CursorX > 0)
                        {
                            this.CursorX--;
                            this.Text = this.Text.Remove(this.CursorX, 1);
                        }
                    }
                }
            }
            else if (key.Key == ConsoleKey.Delete)
            {
                if (this.CursorX <= width && !string.IsNullOrWhiteSpace(this.Text) && this.CursorX < this.Text.Length)
                {
                    this.Text = this.Text.Remove(this.CursorX, 1);
                }
            }
            else if (key.Key == ConsoleKey.Home)
            {
                this.CursorX = 0;
            }
            else if (key.Key == ConsoleKey.End)
            {
                this.CursorX = width - rightBorder;
            }
            else if (key.Key == ConsoleKey.PageUp)
            {
                this.CursorY = 0; // for now
            }
            else if (key.Key == ConsoleKey.PageDown)
            {
                // not implemented
            }
            else
            {
                this.Text = this.Text.Insert(this.CursorX, key.KeyChar + "");
                this.CursorX++;
            }

            return(false);
        }
예제 #3
0
 public override void OnKeyDown(KeyInfo keyInfo)
 {
     WindowManager.OnKeyDown(keyInfo);
 }
예제 #4
0
 public override bool OnKeyDown(KeyInfo key)
 {
     return(this.Controls.Where(x => !x.EventBlocked()).DoWhile(x => x.OnKeyDown(key)));
 }
예제 #5
0
 public override bool OnKeyDown(KeyInfo key)
 {
     return(false);
 }