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.Key) {
case Key.Insert: // Toggle insertion mode
if (Keyboard.Modifiers == ModifierKeys.None) {
ToggleInsertionMode();
}
break;
case Key.A: // Ctrl + A: Select all
if (Keyboard.Modifiers == ModifierKeys.Control) {
m_txTextControl.SelectAll();
}
break;
case Key.S: // Ctrl + S: Save
if (Keyboard.Modifiers == ModifierKeys.Control) {
Save(m_strActiveDocumentPath);
}
break;
case Key.O: // Ctrl + O: Open
if (Keyboard.Modifiers == ModifierKeys.Control) {
Open();
}
break;
case Key.F: // Ctrl + F: Search
if (Keyboard.Modifiers == ModifierKeys.Control) {
m_txTextControl.Find();
}
break;
case Key.P: // Ctrl + P: Print
if (Keyboard.Modifiers == ModifierKeys.Control) {
if (m_txTextControl.CanPrint) {
m_txTextControl.Print(m_strActiveDocumentName, true);
}
}
break;
}
}
Private Sub TextControl_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
Select Case e.Key
Case Key.Insert ' Toggle insertion mode
If Keyboard.Modifiers = ModifierKeys.None Then
ToggleInsertionMode()
End If
Case Key.A ' Ctrl + A: Select all
If Keyboard.Modifiers = ModifierKeys.Control Then
Me.m_txTextControl.SelectAll()
End If
Case Key.S ' Ctrl + S: Save
If Keyboard.Modifiers = ModifierKeys.Control Then
Save(m_strActiveDocumentPath)
End If
Case Key.O ' Ctrl + O: Open
If Keyboard.Modifiers = ModifierKeys.Control Then
Open()
End If
Case Key.F ' Ctrl + F: Search
If Keyboard.Modifiers = ModifierKeys.Control Then
Me.m_txTextControl.Find()
End If
Case Key.P ' Ctrl + P: Print
If Keyboard.Modifiers = ModifierKeys.Control Then
If Me.m_txTextControl.CanPrint Then
Me.m_txTextControl.Print(m_strActiveDocumentName, True)
End If
End If
End Select
End Sub