コード例 #1
0
ファイル: input.cs プロジェクト: HippoAR/DemoPowerUI
        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);
        }
コード例 #2
0
 /// <summary>Called when a default key press occurs.</summary>
 /// <param name="clickEvent">The event that represents the key press.</param>
 public virtual void OnKeyPress(KeyboardEvent pressEvent)
 {
 }
コード例 #3
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);
        }
コード例 #4
0
ファイル: Input.cs プロジェクト: labiel1/DreamHouse_Gaminp
		/// <summary>Tells the UI a key was pressed.</summary>
		/// <param name="down">True if the key is now down.</param>
		/// <param name="keyCode">The keycode of the key</param>
		/// <param name="character">The character entered.</param>
		/// <returns>True if the UI consumed the keypress.</returns>
		public static void OnKeyPress(bool down,char character,int keyCode){
			
			KeyboardEvent e=new KeyboardEvent(keyCode,character,down);
			e.SetTrusted();
			e.SetModifiers();
			e.EventType=down?"keydown":"keyup";
			
			EventTarget target=ActiveReceiver;
			
			// Dispatch the event to the focused element:
			if(target.dispatchEvent(e)){
				
				// Run the tag keypress method:
				if(target is HtmlElement){
					(target as HtmlElement).OnKeyPress(e);
				}
				
				// Handle the defaults now:
				
				if(e.heldDown){
					
					// Get the HTML document:
					HtmlDocument htmlDoc=(target is Node) ? ((target as Node).document) as HtmlDocument : UI.document;
					
					// Grab the keycode:
					KeyCode key=e.unityKeyCode;
					
					if(key==KeyCode.Tab && htmlDoc!=null){
						
						// Tab - hop to next input:
						if(e.shiftKey){
							htmlDoc.TabPrevious();
						}else{
							htmlDoc.TabNext();
						}
						
					}else if(e.ctrlKey){
						
						if(key==KeyCode.V){
							
							// Run the onpaste function.
							
							// Get the pasted text:
							string textToPaste=Clipboard.Paste();
							
							// Fire the event now:
							ClipboardEvent ce=new ClipboardEvent("paste",null);
							ce.clipboardData.setData("text/plain",textToPaste);
							ce.SetTrusted();
							target.dispatchEvent(ce);
							
						}else if(key==KeyCode.C && htmlDoc!=null){
							
							// Run the oncopy function.
							
							// Get the selection:
							string selection=htmlDoc.window.getSelection().ToString();
							
							if(selection==null){
								return;
							}
							
							ClipboardEvent ce=new ClipboardEvent("copy",null);
							ce.clipboardData.setData("text/plain",selection);
							ce.SetTrusted();
							
							if(target.dispatchEvent(ce)){
								
								// Copy to the clipboard now:
								Clipboard.Copy(selection);
								
							}
							
						}
						
					}
					
				}
				
			}
			
		}