Example #1
0
        public override void OnKeyPress(KeyboardEvent pressEvent)
        {
            if (this["readonly"] != null)
            {
                return;
            }

            if (Type == InputType.Number)
            {
                if (!char.IsNumber(pressEvent.character) && pressEvent.character != '.' && !char.IsControl(pressEvent.character))
                {
                    // Not a number, point or control character. Block it:
                    return;
                }
            }

            if (pressEvent.heldDown)
            {
                if (IsTextInput())
                {
                    // Add to value if pwd/text, unless it's backspace:
                    string value = Value;

                    if (!char.IsControl(pressEvent.character) && pressEvent.character != '\0')
                    {
                        // Drop the character in the string at caretIndex
                        if (value == null)
                        {
                            value = "" + pressEvent.character;
                        }
                        else
                        {
                            value = value.Substring(0, CaretIndex) + pressEvent.character + value.Substring(CaretIndex, value.Length - CaretIndex);
                        }

                        SetValue(value);
                        MoveCaret(CaretIndex + 1);
                        return;
                    }

                    // Grab the keycode:
                    KeyCode key = pressEvent.unityKeyCode;

                    if (key == KeyCode.LeftArrow)
                    {
                        MoveCaret(CaretIndex - 1, true);
                    }
                    else if (key == KeyCode.RightArrow)
                    {
                        MoveCaret(CaretIndex + 1, true);
                    }
                    else if (key == KeyCode.Backspace)
                    {
                        // Delete the character before the caret.

                        // Got a selection?
                        if (Caret != null && Caret.TryDeleteSelection())
                        {
                            return;
                        }

                        if (string.IsNullOrEmpty(value) || CaretIndex == 0)
                        {
                            return;
                        }
                        value = value.Substring(0, CaretIndex - 1) + value.Substring(CaretIndex, value.Length - CaretIndex);
                        int index = CaretIndex;
                        SetValue(value);
                        MoveCaret(index - 1);
                    }
                    else if (key == KeyCode.Delete)
                    {
                        // Delete the character after the caret.

                        // Got a selection?
                        if (Caret != null && Caret.TryDeleteSelection())
                        {
                            return;
                        }

                        if (string.IsNullOrEmpty(value) || CaretIndex == value.Length)
                        {
                            return;
                        }

                        value = value.Substring(0, CaretIndex) + value.Substring(CaretIndex + 1, value.Length - CaretIndex - 1);
                        SetValue(value);
                    }
                    else if (key == KeyCode.Return || key == KeyCode.KeypadEnter)
                    {
                        // Does the form have a submit button? If so, submit now.
                        // Also call a convenience (non-standard) "onenter" method.

                        HtmlFormElement f = form;
                        if (f != null)
                        {
                            // Get the first submit button:
                            HtmlElement submitButton = f.GetSubmitButton();

                            if (submitButton != null)
                            {
                                // Submit it now:
                                f.submit(submitButton);
                            }
                        }

                        return;
                    }
                    else if (key == KeyCode.Home)
                    {
                        // Hop to the start:

                        MoveCaret(0, true);
                    }
                    else if (key == KeyCode.End)
                    {
                        // Hop to the end:

                        int maxCaret = 0;

                        if (value != null)
                        {
                            maxCaret = value.Length;
                        }

                        MoveCaret(maxCaret, true);
                    }
                }
                else if (Type == InputType.Submit)
                {
                    // Submit button.
                    if (!char.IsControl(pressEvent.character) && pressEvent.character != '\0')
                    {
                        return;
                    }

                    // Grab the keycode:
                    KeyCode key = pressEvent.unityKeyCode;

                    if (key == KeyCode.Return || key == KeyCode.KeypadEnter)
                    {
                        // Find the form and then attempt to submit it.
                        HtmlFormElement f = form;

                        if (f != null)
                        {
                            f.submit(this);
                        }
                    }
                }
            }

            base.OnKeyPress(pressEvent);
        }
Example #2
0
        public override void OnKeyPress(KeyboardEvent pressEvent)
        {
            if (readOnly)
            {
                return;
            }

            if (pressEvent.heldDown)
            {
                // Add to value unless it's backspace:
                string value = this.value;

                if (!char.IsControl(pressEvent.character) && pressEvent.character != '\0')
                {
                    // Drop the character in the string at caretIndex
                    if (value == null)
                    {
                        value = "" + pressEvent.character;
                    }
                    else
                    {
                        value = value.Substring(0, CaretIndex) + pressEvent.character + value.Substring(CaretIndex, value.Length - CaretIndex);
                    }

                    SetValue(value);
                    MoveCaret(CaretIndex + 1);

                    return;
                }

                // It's a command character:

                KeyCode key = pressEvent.unityKeyCode;

                if (key == KeyCode.LeftArrow)
                {
                    MoveCaret(CaretIndex - 1, true);
                }
                else if (key == KeyCode.RightArrow)
                {
                    MoveCaret(CaretIndex + 1, true);
                }
                else if (key == KeyCode.UpArrow)
                {
                    MoveCaret(FindNewline(-1), true);
                }
                else if (key == KeyCode.DownArrow)
                {
                    MoveCaret(FindNewline(1), true);
                }
                else if (key == KeyCode.Backspace)
                {
                    // Delete the character before the caret (or the selection, if we have one).

                    // Got a selection?
                    if (Caret != null && Caret.TryDeleteSelection())
                    {
                        return;
                    }

                    if (string.IsNullOrEmpty(value) || CaretIndex == 0)
                    {
                        return;
                    }

                    int index = CaretIndex;
                    value = value.Substring(0, index - 1) + value.Substring(index, value.Length - index);
                    SetValue(value);
                    MoveCaret(index - 1);
                }
                else if (key == KeyCode.Delete)
                {
                    // Delete the character after the caret.

                    // Got a selection?
                    if (Caret != null && Caret.TryDeleteSelection())
                    {
                        return;
                    }

                    if (string.IsNullOrEmpty(value) || CaretIndex == value.Length)
                    {
                        return;
                    }

                    int index = CaretIndex;
                    value = value.Substring(0, index) + value.Substring(index + 1, value.Length - index - 1);
                    SetValue(value);
                }
                else if (key == KeyCode.Home)
                {
                    // Hop to the start:

                    MoveCaret(0, true);
                }
                else if (key == KeyCode.End)
                {
                    // Hop to the end:

                    int maxCaret = 0;

                    if (value != null)
                    {
                        maxCaret = value.Length;
                    }

                    MoveCaret(maxCaret, true);
                }
                else if (key == KeyCode.Return || key == KeyCode.KeypadEnter)
                {
                    // Add a newline
                    if (value == null)
                    {
                        value = "" + pressEvent.character;
                    }
                    else
                    {
                        value = value.Substring(0, CaretIndex) + '\n' + value.Substring(CaretIndex, value.Length - CaretIndex);
                    }
                    SetValue(value);
                    MoveCaret(CaretIndex + 1);
                }
            }

            base.OnKeyPress(pressEvent);
        }