Keyboard Shortcuts
A TextControl on a Windows Forms form responds to a fixed set of keys as soon as it has the focus: the arrow keys and their combinations move the caret, Ctrl+C and Ctrl+V use the clipboard, Ctrl+Enter starts a new page. The tables below list every built-in assignment, for your own reference and for the help of your application. The last section adds shortcuts of your own, such as Ctrl+F for the Find dialog box.
The mouse actions, such as a double-click to select a word, are listed on Selections and Caret Position.
Move the Caret
| Keys | Action |
|---|---|
| Left Arrow, Right Arrow | Moves the caret one character to the left or right |
| Up Arrow, Down Arrow | Moves the caret one line up or down |
| Ctrl+Left Arrow | Moves the caret to the beginning of the current word |
| Ctrl+Right Arrow | Moves the caret to the beginning of the next word |
| Home | Moves the caret to the beginning of the line |
| End | Moves the caret to the end of the line |
| Ctrl+Home | Moves the caret to the start of the text |
| Ctrl+End | Moves the caret to the end of the text |
| Page Up, Page Down | Scrolls up or down by the height of the visible area minus one line, if there is a vertical scroll bar |
| Shift+Page Up, Shift+Page Down | Scrolls the document horizontally |
| Ctrl+Page Up, Ctrl+Page Down | Zooms the document in or out |
Holding down Shift while pressing a key that moves the caret extends the selection to the new caret position. Shift+Ctrl+End, for example, selects everything from the caret to the end of the text.
Edit Text
| Keys | Action |
|---|---|
| Shift+Enter | Inserts a line break without starting a new paragraph |
| Ctrl+Enter | Inserts a page break |
| Ctrl+Shift+Enter | Inserts a column break, so that the text continues in the next column of a multi-column section |
| Ctrl+Shift+Spacebar | Inserts a non-breaking space |
| Delete | Deletes the selected text |
| Ctrl+Backspace, Shift+Backspace | Deletes the previous word |
| Ctrl+C | Copies the selected text to the clipboard |
| Ctrl+X | Copies the selected text to the clipboard and deletes it |
| Ctrl+V | Pastes the content of the clipboard in place of the selection |
| Ctrl+Z | Undoes the last action |
| Ctrl+Y | Restores the last undone action |
Clipboard Operations and Undo and Redo show the methods behind the clipboard and undo keys.
Insert Hyphens and Dashes
| Keys | Inserts | Word may be divided there |
|---|---|---|
| Hyphen (-) | A hyphen or minus sign | Yes |
| Ctrl+Hyphen | An end-of-line hyphen, which is not visible unless the word is divided at that point | Yes |
| Ctrl+Shift+Hyphen | A hyphen for compound words that keeps the parts on one line | No |
| Ctrl+Minus on the numeric keypad | An en dash | Yes |
| Ctrl+Alt+Minus on the numeric keypad | An em dash | Yes |
Automatic hyphenation is described on Spell Checking and Hyphenation.
Keys in Tables
| Keys | Action |
|---|---|
| Tab | Selects the next table cell. In the last cell of a table, it adds a new row at the end of the table. |
| Shift+Tab | Selects the previous table cell |
| Ctrl+Tab | Inserts a tab character in the cell |
| Enter | At the first input position of a document that begins with a table, inserts an empty line in front of the table |
Outside of tables, the Tab key depends on AcceptsTab: with true, it types a tab character; with false, it moves the focus to the next control in the tab order of the form.
Form fields have keys of their own, for example the Spacebar to check a check box or open a drop-down list, and Tab to go to the next field in a protected form. They are listed on Form Fields and Conditional Instructions.
Add Your Own Shortcuts
Every key press raises the KeyDown event that TextControl inherits from the Windows Forms Control class. A handler of this event can call any method of the editor for a key combination and mark the key as handled, so that the editor ignores it. The following code in Form1.cs opens the Find and Replace dialog boxes with Ctrl+F and Ctrl+H, selects all text with Ctrl+A and switches between insert and overwrite mode with the Insert key:
using System;
using System.Windows.Forms;
using TXTextControl;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textControl1.KeyDown += TextControl1_KeyDown;
}
private void TextControl1_KeyDown(object sender, KeyEventArgs e)
{
// Ctrl+F: open the built-in Find dialog box
if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.F)
{
textControl1.Find();
e.Handled = true;
}
// Ctrl+H: open the built-in Replace dialog box
else if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.H)
{
textControl1.Replace();
e.Handled = true;
}
// Ctrl+A: select the whole text
else if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.A)
{
textControl1.SelectAll();
e.Handled = true;
}
// Insert: switch between inserting and overwriting text
else if (e.Modifiers == Keys.None && e.KeyCode == Keys.Insert)
{
textControl1.InsertionMode = textControl1.InsertionMode == InsertionMode.Insert
? InsertionMode.Overwrite
: InsertionMode.Insert;
e.Handled = true;
}
}
}
Keep the namespace that Visual Studio created around the class. Run the application and press Ctrl+F: the Find dialog box opens.
The code works as follows:
e.Control,e.Altande.Shifttell which modifier keys are held down, ande.KeyCodewhich key was pressed. The checks make sure that Ctrl+Shift+F, for example, doesn't trigger the same command.- Find and Replace without parameters open the built-in dialog boxes, and SelectAll selects the whole text.
- InsertionMode is
Insertwhen typed text is inserted andOverwritewhen it replaces the text at the input position. e.Handled = truetells the editor that the key has been processed, so its own handling of the key is skipped.
The TX Words sample of the setup, in %USERPROFILE%\Documents\TX Text Control 34.0.NET for Windows Forms\CSharp\TX Words\Shortcuts, handles Ctrl+O, Ctrl+S and Ctrl+P for opening, saving and printing the same way. The blog article Windows Forms: Removing the Next Word Right to the Input Position on CTRL + DEL adds Ctrl+Delete to delete the next word.
Insert a Line Break With Enter
Some applications, such as an editor for address blocks or table cells, need Enter to start a new line inside the same paragraph, as Shift+Enter does. The KeyPress event, also inherited from Control, receives the Enter key as the carriage return character '\r'. The handler below replaces it with a line break, which is the vertical tab character "\v" in the text of a TextControl:
// In the constructor, after InitializeComponent
textControl1.KeyPress += TextControl1_KeyPress;
private void TextControl1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
// Skip the new paragraph and insert a line break instead
e.Handled = true;
textControl1.Selection.Text = "\v";
}
}
Selection.Text replaces the selected text, or inserts at the input position when nothing is selected.
Next Steps
- Selections and Caret Position: the mouse actions and the selection in code.
- Search and Replace: the Find and Replace dialog boxes and sidebars.
- Events: the other events of the editor.