Ejemplo n.º 1
0
        public override void OnKeyPress(UIEvent pressEvent)
        {
            // We want to fire the nitro event first:
            base.OnKeyPress(pressEvent);

            if (Value != null && CursorIndex > Value.Length)
            {
                MoveCursor(0);
            }

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

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

                    SetValue(value);
                    MoveCursor(CursorIndex + 1);

                    return;
                }

                // It's a command character:

                KeyCode key = ((KeyCode)pressEvent.keyCode);

                if (key == KeyCode.LeftArrow)
                {
                    MoveCursor(CursorIndex - 1, true);
                }
                else if (key == KeyCode.RightArrow)
                {
                    MoveCursor(CursorIndex + 1, true);
                }
                else if (key == KeyCode.Backspace)
                {
                    // Delete the character before the cursor.
                    if (string.IsNullOrEmpty(value) || CursorIndex == 0)
                    {
                        return;
                    }
                    value = value.Substring(0, CursorIndex - 1) + value.Substring(CursorIndex, value.Length - CursorIndex);
                    int index = CursorIndex;
                    SetValue(value);
                    MoveCursor(index - 1);
                }
                else if (key == KeyCode.Delete)
                {
                    // Delete the character after the cursor.
                    if (string.IsNullOrEmpty(value) || CursorIndex == value.Length)
                    {
                        return;
                    }
                    value = value.Substring(0, CursorIndex) + value.Substring(CursorIndex + 1, value.Length - CursorIndex - 1);
                    SetValue(value);
                }
                else if (key == KeyCode.Home)
                {
                    // Hop to the start:

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

                    int maxCursor = 0;

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

                    MoveCursor(maxCursor, true);
                }
                else if (pressEvent.ctrlKey)
                {
                    if (key == KeyCode.V)
                    {
                        // Run the onpaste function.
                        if (Element.RunBlocked("onpaste", pressEvent))
                        {
                            // It blocked it.
                            return;
                        }

                        string textToPaste = Clipboard.Paste();

                        if (!string.IsNullOrEmpty(textToPaste))
                        {
                            // Drop the character in the string at cursorIndex
                            if (value == null)
                            {
                                value = "" + textToPaste;
                            }
                            else
                            {
                                value = value.Substring(0, CursorIndex) + textToPaste + value.Substring(CursorIndex, value.Length - CursorIndex);
                            }

                            SetValue(value);
                            MoveCursor(CursorIndex + textToPaste.Length);
                        }
                    }
                    else if (key == KeyCode.C)
                    {
                        // Run the oncopy function.
                        if (Element.RunBlocked("oncopy", pressEvent))
                        {
                            // It blocked it.
                            return;
                        }

                        Clipboard.Copy(value);
                    }
                }
                else if (key == KeyCode.Return || key == KeyCode.KeypadEnter)
                {
                    // Add a newline
                    if (value == null)
                    {
                        value = "" + pressEvent.character;
                    }
                    else
                    {
                        value = value.Substring(0, CursorIndex) + '\n' + value.Substring(CursorIndex, value.Length - CursorIndex);
                    }
                    SetValue(value);
                    MoveCursor(CursorIndex + 1);
                }
            }
        }
Ejemplo n.º 2
0
        public override void OnKeyPress(UIEvent pressEvent)
        {
            // We want to fire the nitro event first:
            base.OnKeyPress(pressEvent);

            // How long is the current value?
            int length = 0;

            if (Value != null)
            {
                length = Value.Length;
            }

            // Is the cursor too far over?
            if (CursorIndex > length)
            {
                MoveCursor(0);
            }

            if (pressEvent.cancelBubble)
            {
                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 cursorIndex
                        if (value == null)
                        {
                            value = "" + pressEvent.character;
                        }
                        else
                        {
                            value = value.Substring(0, CursorIndex) + pressEvent.character + value.Substring(CursorIndex, value.Length - CursorIndex);
                        }

                        SetValue(value);
                        MoveCursor(CursorIndex + 1);
                        return;
                    }

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

                    if (key == KeyCode.LeftArrow)
                    {
                        MoveCursor(CursorIndex - 1, true);
                    }
                    else if (key == KeyCode.RightArrow)
                    {
                        MoveCursor(CursorIndex + 1, true);
                    }
                    else if (key == KeyCode.Backspace)
                    {
                        // Delete the character before the cursor.
                        if (string.IsNullOrEmpty(value) || CursorIndex == 0)
                        {
                            return;
                        }
                        value = value.Substring(0, CursorIndex - 1) + value.Substring(CursorIndex, value.Length - CursorIndex);
                        int index = CursorIndex;
                        SetValue(value);
                        MoveCursor(index - 1);
                    }
                    else if (key == KeyCode.Delete)
                    {
                        // Delete the character after the cursor.
                        if (string.IsNullOrEmpty(value) || CursorIndex == value.Length)
                        {
                            return;
                        }

                        value = value.Substring(0, CursorIndex) + value.Substring(CursorIndex + 1, value.Length - CursorIndex - 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.

                        FormTag form = Element.form;
                        if (form != null && form.HasSubmitButton)
                        {
                            form.submit();
                        }

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

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

                        int maxCursor = 0;

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

                        MoveCursor(maxCursor, true);
                    }
                    else if (pressEvent.ctrlKey)
                    {
                        if (key == KeyCode.V)
                        {
                            // Run the onpaste function.
                            if (Element.RunBlocked("onpaste", pressEvent))
                            {
                                // It blocked it.
                                return;
                            }

                            // Paste the text, stripping any newlines:
                            string textToPaste = Clipboard.Paste().Replace("\r", "").Replace("\n", "");

                            if (!string.IsNullOrEmpty(textToPaste))
                            {
                                // Drop the character in the string at cursorIndex
                                if (value == null)
                                {
                                    value = "" + textToPaste;
                                }
                                else
                                {
                                    value = value.Substring(0, CursorIndex) + textToPaste + value.Substring(CursorIndex, value.Length - CursorIndex);
                                }

                                SetValue(value);
                                MoveCursor(CursorIndex + textToPaste.Length);
                            }
                        }
                        else if (key == KeyCode.C)
                        {
                            // Run the oncopy function.
                            if (Element.RunBlocked("oncopy", pressEvent))
                            {
                                // It blocked it.
                                return;
                            }

                            Clipboard.Copy(value);
                        }
                    }
                }
            }
        }