/// <summary> /// 处理输入键按下的事件,即输入字符的事件 /// </summary> /// <param name="sender">事件发送者</param> /// <param name="e">事件参数</param> private void TextCtrl_KeyPress(object sender, KeyPressEventArgs e) { if (lastKeyHandled) { return; } if (isSelecting) { if (!CoreMethods.Remove(ref selectionStartPos, ref selectionEndPos)) { if (DebugMsgSend != null) { DebugMsgSend("::Remove failed!"); } } ScrollTo(selectionStartPos); ClearSelection(); } CPosition tmp = cursorRelativePos + viewAbsolutePos; if (e.KeyChar == 13) { e.KeyChar = (char)10; } if (!CoreMethods.InsertC(ref tmp, e.KeyChar)) { if (DebugMsgSend != null) { DebugMsgSend("::InsertChar failed!"); } } UpdateText(); UpdateScrollBars(); if (e.KeyChar == '\t') { cursorRelativePos.x += 4; UpdateCaretPos(); } else { CaretStep(true); } }
/// <summary> /// 剪切选区 /// </summary> public void CutSelection() { if (!isSelecting) { SystemSounds.Beep.Play(); return; } StringBuilder selection = new StringBuilder(selectionLength + 1); CoreMethods.GetSelection(ref selectionStartPos, ref selectionEndPos, selection); Clipboard.SetText(selection.ToString(), TextDataFormat.Text); if (!CoreMethods.Remove(ref selectionStartPos, ref selectionEndPos)) { if (DebugMsgSend != null) { DebugMsgSend("::Remove failed!"); } } ScrollTo(selectionStartPos); ClearSelection(); UpdateText(); }
/// <summary> /// 粘贴选区 /// </summary> public void Paste() { if (isSelecting) { if (!CoreMethods.Remove(ref selectionStartPos, ref selectionEndPos)) { if (DebugMsgSend != null) { DebugMsgSend("::Remove failed!"); } } } if (!Clipboard.ContainsText()) { SystemSounds.Beep.Play(); return; } CPosition tmp = cursorRelativePos + viewAbsolutePos, endPos = new CPosition(); CoreMethods.InsertS(ref tmp, Clipboard.GetText(), ref endPos); UpdateText(); ScrollTo(endPos); }
/// <summary> /// 处理控制键按下的事件,包括功能键、删除键、编辑键的功能实现 /// </summary> /// <param name="sender">事件触发者</param> /// <param name="e">事件参数</param> private void TextCtrl_KeyDown(object sender, KeyEventArgs e) { lastKeyHandled = true; if (e.KeyCode == Keys.Up) { cursorRelativePos.y--; if (!ValidateCaretPos()) { SystemSounds.Beep.Play(); } UpdateCaretPos(); ArrowKeyEndProc(); } else if (e.KeyCode == Keys.Down) { cursorRelativePos.y++; if (!ValidateCaretPos()) { SystemSounds.Beep.Play(); } UpdateCaretPos(); ArrowKeyEndProc(); } else if (e.KeyCode == Keys.Left) { CaretStep(false); } else if (e.KeyCode == Keys.Right) { CaretStep(true); } else if (e.KeyCode == Keys.Home) { cursorRelativePos.x = viewAbsolutePos.x = 0; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.End) { viewAbsolutePos.x = 0; cursorRelativePos.x = lineMaxLen; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.PageUp) { cursorRelativePos.y -= txtFieldSize.Height; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.PageDown) { cursorRelativePos.y += txtFieldSize.Height; ValidateCaretPos(); UpdateCaretPos(); Invalidate(); } else if (e.KeyCode == Keys.C && e.Control) { CopySelection(); } else if (e.KeyCode == Keys.V && e.Control) { Paste(); } else if (e.KeyCode == Keys.F && e.Control) { RequestFind(this); } else if (e.KeyCode == Keys.S && e.Control) { SaveFile(); } else if (e.KeyCode == Keys.A && e.Control) { selectionStartPos.x = selectionStartPos.y = 0; selectionEndPos.y = texts.Length - 1; selectionEndPos.x = sizes[texts.Length - 1]; isSelecting = true; UpdateSelection(); } else if (e.KeyCode == Keys.D && e.Control) { CoreMethods.RemoveLine(cursorRelativePos.y + viewAbsolutePos.y); UpdateText(); ValidateCaretPos(); UpdateCaretPos(); } else if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) { if (isSelecting) { if (!CoreMethods.Remove(ref selectionStartPos, ref selectionEndPos)) { if (DebugMsgSend != null) { DebugMsgSend("::Remove failed!"); } } ScrollTo(selectionStartPos); ClearSelection(); UpdateText(); } else { if (e.KeyCode == Keys.Delete && !CaretStep(true, true)) { return; } CPosition tmp = cursorRelativePos + viewAbsolutePos; if (!CoreMethods.Backspace(ref tmp)) { if (DebugMsgSend != null) { DebugMsgSend("::Backspace failed!"); } } CaretStep(false, true); tmp = cursorRelativePos + viewAbsolutePos; UpdateText(); UpdateScrollBars(); cursorRelativePos = tmp - viewAbsolutePos; ValidateCaretPos(); UpdateCaretPos(); } } else { lastKeyHandled = false; } ctrlState = e.Control; }