ICSharpCode.TextEditor.TextArea.SimulateKeyPress C# (CSharp) Method

SimulateKeyPress() public method

public SimulateKeyPress ( char ch ) : void
ch char
return void
		public void SimulateKeyPress(char ch)
		{
			if (SelectionManager.HasSomethingSelected) {
				if (SelectionManager.SelectionIsReadonly)
					return;
			} else if (IsReadOnly(Caret.Offset)) {
				return;
			}
			
			if (ch < ' ') {
				return;
			}
			
			if (!hiddenMouseCursor && TextEditorProperties.HideMouseCursor) {
				if (this.ClientRectangle.Contains(PointToClient(Cursor.Position))) {
					mouseCursorHidePosition = Cursor.Position;
					hiddenMouseCursor = true;
					Cursor.Hide();
				}
			}
			CloseToolTip();
			
			BeginUpdate();
			Document.UndoStack.StartUndoGroup();
			try {
				// INSERT char
				if (!HandleKeyPress(ch)) {
					switch (Caret.CaretMode) {
						case CaretMode.InsertMode:
							InsertChar(ch);
							break;
						case CaretMode.OverwriteMode:
							ReplaceChar(ch);
							break;
						default:
							Debug.Assert(false, "Unknown caret mode " + Caret.CaretMode);
							break;
					}
				}
				
				int currentLineNr = Caret.Line;
				Document.FormattingStrategy.FormatLine(this, currentLineNr, Document.PositionToOffset(Caret.Position), ch);
				
				EndUpdate();
			} finally {
				Document.UndoStack.EndUndoGroup();
			}
		}
		

Usage Example

		public bool InsertAction(TextArea textArea, char ch)
		{
			if ((dataType == DataType.XmlElement))
            {
				textArea.InsertString(text);
			}
            else if (dataType == DataType.XmlAttributeValue)
            {
                if( XmlParser.IsInsideAttributeValue(textArea.Document.TextContent,textArea.Caret.Offset))
                {
                    int first, last;
                    XmlParser.GetCurrentAttributeValueSpan(textArea.Document.TextContent, textArea.Caret.Offset, out first, out last);
                    if (last > first && last > 0)
                    {
                        textArea.SelectionManager.SetSelection(textArea.Document.OffsetToPosition(first)
                                                               , textArea.Document.OffsetToPosition(last)
                                                               );
                        textArea.SelectionManager.RemoveSelectedText();
                    }
                }
                textArea.InsertString(text);
                Caret caret = textArea.Caret;
                // Move caret outside of the attribute quotes.
                caret.Position = textArea.Document.OffsetToPosition(caret.Offset + 1);
            }
			else if (dataType == DataType.NamespaceUri) {
				textArea.InsertString(String.Concat("\"", text, "\""));
			} else {
				// Insert an attribute.
				Caret caret = textArea.Caret;
				textArea.InsertString(String.Concat(text, "=\""));	
				
				// Move caret into the middle of the attribute quotes.
				caret.Position = textArea.Document.OffsetToPosition(caret.Offset - 1);
                textArea.SimulateKeyPress('\"');
			}
			return false;
		}