Example #1
0
        public override bool OnKeyboardEvent(KeyboardEvent keyboardevent)
        {
            if (!Enabled || !HasFocus)
            {
                return(false);
            }

            if (keyboardevent.type == KeyboardEventType.InputKey)
            {
                var inputKeyEvent = (InputKeyEvent)keyboardevent;
                if (inputKeyEvent.Ch == '\r' && enterkeycallback != null)
                {
                    enterkeycallback(this);
                }

                if (inputKeyEvent.Ctrl)
                {
                    if (inputKeyEvent.Ch == 'c' || inputKeyEvent.Ch == 'C')
                    {
                        CopyToClipboard(GetHighlightedRegion());
                    }
                    else if (inputKeyEvent.Ch == 'V' || inputKeyEvent.Ch == 'v')
                    {
                        var ch = CopyFromClipboard();
                        if (!string.IsNullOrEmpty(ch))
                        {
                            if (highlight_start >= 0)
                            {
                                var highlightStart = highlight_start;
                                DeleteHighlightedRegion();
                                textline.AddStringAt(ch, highlightStart);
                            }
                            else
                            {
                                textline.AddStringAt(ch, cursor);
                            }
                        }
                    }
                    else if (inputKeyEvent.Ch == 'X' || inputKeyEvent.Ch == 'x')
                    {
                        CopyToClipboard(GetHighlightedRegion());
                        DeleteHighlightedRegion();
                    }
                }
                else
                {
                    DeleteHighlightedRegion();
                    if (inputKeyEvent.Ch == '\r' || inputKeyEvent.Ch == '\n')
                    {
                        OnControlMsg(this, ControlMsg.ENTERHIT, 0.0f, 0.0f);
                    }
                    else if (inputKeyEvent.Ch == '\b')
                    {
                        if (cursor > 0)
                        {
                            textline.DeleteAt(cursor - 1);
                        }

                        this.onbackspace?.Invoke(this);

                        if (cursor > 0)
                        {
                            --cursor;
                        }
                    }
                    else if (MAX_CHARS <= 0 || MAX_CHARS > textline.GetSize())
                    {
                        var ch = inputKeyEvent.Ch;
                        if (CAPS)
                        {
                            ch = char.ToUpper(ch);
                        }

                        textline.AddCharAt(ch, cursor);
                        this.onnewtext?.Invoke(this);

                        ++cursor;
                    }
                }
                return(true);
            }
            if (keyboardevent.type == KeyboardEventType.CommandKey)
            {
                var commandKeyEvent = (CommandKeyEvent)keyboardevent;
                if (commandKeyEvent.Key == KeyboardCommandKey.Left)
                {
                    if (cursor > 0)
                    {
                        if (commandKeyEvent.Shift)
                        {
                            if (highlight_start < 0 || highlight_end == highlight_start)
                            {
                                highlight_end   = cursor;
                                highlight_start = --cursor;
                            }
                            else if (cursor == highlight_end)
                            {
                                highlight_end = --cursor;
                            }
                            else
                            {
                                highlight_start = --cursor;
                            }
                        }
                        else
                        {
                            --cursor;
                        }
                    }
                    if (!commandKeyEvent.Shift)
                    {
                        CancelHighlight();
                    }
                }
                else if (commandKeyEvent.Key == KeyboardCommandKey.Right)
                {
                    if (cursor < Text.Length)
                    {
                        if (commandKeyEvent.Shift)
                        {
                            if (highlight_start < 0 || highlight_end == highlight_start)
                            {
                                highlight_start = cursor;
                                highlight_end   = ++cursor;
                            }
                            else if (cursor == highlight_start)
                            {
                                highlight_start = ++cursor;
                            }
                            else
                            {
                                highlight_end = ++cursor;
                            }
                        }
                        else
                        {
                            ++cursor;
                        }
                    }
                    if (!commandKeyEvent.Shift)
                    {
                        CancelHighlight();
                    }
                }
                else if (commandKeyEvent.Key == KeyboardCommandKey.Delete)
                {
                    if (highlight_start >= 0)
                    {
                        DeleteHighlightedRegion();
                    }
                    else if (cursor < Text.Length)
                    {
                        textline.DeleteAt(cursor);
                    }
                }
                else if (commandKeyEvent.Key == KeyboardCommandKey.Home)
                {
                    if (!commandKeyEvent.Shift)
                    {
                        CancelHighlight();
                    }
                    else if (Text.Length > 0 && cursor > 0)
                    {
                        if (highlight_start < 0)
                        {
                            highlight_end = cursor;
                        }
                        else if (cursor > highlight_start)
                        {
                            highlight_end = highlight_start;
                        }

                        highlight_start = 0;
                    }
                    cursor = 0;
                }
                else if (commandKeyEvent.Key == KeyboardCommandKey.End)
                {
                    if (!commandKeyEvent.Shift)
                    {
                        CancelHighlight();
                    }
                    else if (Text.Length > 0 && cursor < Text.Length)
                    {
                        if (highlight_start < 0)
                        {
                            highlight_start = cursor;
                        }
                        else if (cursor < highlight_end)
                        {
                            highlight_start = highlight_end;
                        }

                        highlight_end = Text.Length;
                    }
                    cursor = Text.Length;
                }
            }
            return(base.OnKeyboardEvent(keyboardevent));
        }