public void Paste() { if (Clipboard == null) { return; } string content = Clipboard.Contents; if (content != null) { StringBuilder buffer = new StringBuilder(); for (int i = 0; i < content.Length; i++) { if (MaxLength > 0 && _text.Length + buffer.Length + 1 > MaxLength) { break; } char c = content[i]; if (!_style.Font.ContainsCharacter(c)) { continue; } if (TextFieldFilter != null && !TextFieldFilter.AcceptChar(this, c)) { continue; } buffer.Append(c); } content = buffer.ToString(); if (!_hasSelection) { _text = _text.Substring(0, _cursor) + content + _text.Substring(_cursor, _text.Length - _cursor); UpdateDisplayText(); _cursor += content.Length; OnTextChanged(); } else { int minIndex = Math.Min(_cursor, _selectionStart); int maxIndex = Math.Max(_cursor, _selectionStart); _text = (minIndex > 0 ? _text.Substring(0, minIndex) : "") + (maxIndex < _text.Length ? _text.Substring(maxIndex, _text.Length - maxIndex) : ""); _cursor = minIndex; _text = _text.Substring(0, _cursor) + content + _text.Substring(_cursor, _text.Length - _cursor); OnTextChanged(); UpdateDisplayText(); _cursor = minIndex + content.Length; ClearSelection(); } } }
protected virtual void OnKeyTyped(KeyCharEventArgs e) { if (IsDisabled) { return; } BitmapFont font = _style.Font; Stage stage = Stage; if (stage != null && stage.GetKeyboardFocus() == this) { if (e.Character == CharBackspace) { if (_cursor > 0 || _hasSelection) { if (!_hasSelection) { _text = _text.Substring(0, _cursor - 1) + _text.Substring(_cursor); UpdateDisplayText(); _cursor--; _renderOffset = 0; OnTextChanged(); } else { Delete(); } } } else if (e.Character == CharDelete) { if (_cursor < _text.Length || _hasSelection) { if (!_hasSelection) { _text = _text.Substring(0, _cursor) + _text.Substring(_cursor + 1); UpdateDisplayText(); OnTextChanged(); } else { Delete(); } } } else if ((e.Character == CharTab || e.Character == CharEnterAndroid) && FocusTraversal) { KeyboardState keyboard = Keyboard.GetState(); Next(keyboard.IsKeyDown(Keys.LeftShift) || keyboard.IsKeyDown(Keys.RightShift)); } else if (font.ContainsCharacter(e.Character)) { if (e.Character != CharEnterDesktop && e.Character != CharEnterAndroid) { if (TextFieldFilter != null && !TextFieldFilter.AcceptChar(this, e.Character)) { e.Handled = true; return; } } if (MaxLength > 0 && _text.Length + 1 > MaxLength) { e.Handled = true; return; } if (!_hasSelection) { _text = _text.Substring(0, _cursor) + e.Character + _text.Substring(_cursor, _text.Length - _cursor); UpdateDisplayText(); _cursor++; OnTextChanged(); } else { int minIndex = Math.Min(_cursor, _selectionStart); int maxIndex = Math.Max(_cursor, _selectionStart); _text = (minIndex > 0 ? _text.Substring(0, minIndex) : "") + (maxIndex < _text.Length ? _text.Substring(maxIndex, _text.Length - maxIndex) : ""); _cursor = minIndex; _text = _text.Substring(0, _cursor) + e.Character + _text.Substring(_cursor, _text.Length - _cursor); OnTextChanged(); UpdateDisplayText(); _cursor++; ClearSelection(); } } e.Handled = true; } }