Using shortcuts is a useful way to access word processing functions by using key combinations. Some shortcuts (like Ctrl+Z for undo, Ctrl+Y for redo,Ctrl+C for copy, Ctrl+X for cut and Ctrl+V for paste) are built-in. This sample shows how to implement a user defined shortcut behavior by using the TextControl.KeyDown event.
The source code for this example is contained in the following directories:
This sample implements the following shortcuts:
To check the implementation add or load some text and apply the defined shortcuts.
As mentioned before, the shortcuts are handled by a TextControl.KeyDown event handler:
private void TextControl_KeyDown(object sender, KeyEventArgs e) {
switch (e.KeyCode) {
case Keys.Insert: // Toggle insertion mode
if (e.Control || e.Alt || e.Shift) { break; }
ToggleInsertionMode();
break;
case Keys.A: // Ctrl + A: Select all
if (!e.Control || e.Alt || e.Shift) { break; }
m_txTextControl.SelectAll();
break;
case Keys.S: // Ctrl + S: Save
if (!e.Control || e.Alt || e.Shift) { break; }
Save(m_strActiveDocumentPath);
break;
case Keys.O: // Ctrl + O: Open
if (!e.Control || e.Alt || e.Shift) { break; }
Open();
break;
case Keys.F: // Ctrl + F: Search
if (!e.Control || e.Alt || e.Shift) { break; }
m_txTextControl.Find();
break;
case Keys.P: // Ctrl + P: Print
if (!e.Control || e.Alt || e.Shift) { break; }
if (m_txTextControl.CanPrint) {
m_txTextControl.Print(m_strActiveDocumentName);
}
break;
}
}