0
votes

Is it possible to re-program the tab key's functionality when using a TextBox in UWP apps? If you use the UWP version of OneNote, you see that it is possible... Well I can't say for sure, since it is Microsoft themselves that wrote the app, so maybe they just gave themselves the ability to override it?

But my goal is simply to program the key when my TextBox or RichEditBox has focus. I would like to give users the ability to simply press the tab key and indent text or bullet points, instead of having to additionally hold down the control key. But as things stand, by default when you press the tab key the editor loses focus and the next UI element gets focus. Annoying. -_- But thanks.

1
There is KeyDown or PreviewKeyDown Event for you to handle. - kurakura88

1 Answers

0
votes

There's example on msdn with some workaround. However, there's no more CaretIndex property, so you need to use SelectionStart:

private void UIElement_OnPreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
     if (e.OriginalKey == VirtualKey.Tab)
     {
        var txtBox = (TextBox)sender;

         //No CaretIndex property anymore, you need to use SelectionStart
         var caretPos = txtBox.SelectionStart;

         txtBox.Text = txtBox.Text.Insert(caretPos, "    ");
         txtBox.SelectionStart = caretPos + 4;

         //Remember to set Handled to avoid OS handle Tab key
         e.Handled = true;
      }
}

This is presented as a workaround so I recommend that you try to find some open-source "uwp rich text box" and get the idea from there.

Anyway, solution above is pretty fast and easy to implement.