TX Words: Shortcuts

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:

  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\CSharp\TX Words\Shortcuts
  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\VB.NET\TX Words\Shortcuts

Using the Sample

This sample implements the following shortcuts:

  • Ctrl+Insert: Toggles the TextControl insertion mode between Insert and Overwrite.
  • Ctrl+A: Selects all text in the TextControl.
  • Ctrl+S: Save the changes of active document.
  • Ctrl+O: Invokes the built-in TextControl load dialog to open a document.
  • Ctrl+F: Invokes the built-in TextControl find dialog.
  • Ctrl+P: Invokes the built-in TextControl print dialog.

To check the implementation add or load some text and apply the defined shortcuts.

The Code Behind

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