Search code examples
c#wpfsendkeys

send key to TextBox only partial working


I have a self made "virtual keyboard". The user can insert text into the TextBox via his keyboard or the "virtual keyboard". If he presses a VirtualKey I send the Key to the TextBox:

TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, _TextBox, key));

If he presses a special Button I send the command via EditingCommands. But this is only partial working. Sending Backspace works, Tab does not work. AcceptTab is set to true. Insert a tab via the keyboard is possible, only simulating this key is not working. Someone got a hint for me?

            case "Back":
                EditingCommands.Backspace.Execute(null, _TextBox);
                break;

            case "Tab":
                EditingCommands.TabForward.Execute(null, _TextBox);
                break;

            case "STab":
                EditingCommands.TabBackward.Execute(null, _TextBox);
                break;
            case "Delete":
                EditingCommands.Delete.Execute(null, _TextBox);
                break;
  • Backspace works
  • TabForward is NOT working
  • TabBackward is NOT working
  • Delete works

Edit:

XAML Code HtVirtualKeyboardViewModel

    <Style TargetType="Keyboard:HtVirtualKeyboardViewModel" x:Key="KeyboardLayoutTrimmed">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Keyboard:HtVirtualKeyboardViewModel">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="1" Name="KeyboardGrid" VerticalAlignment="Top">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Border Grid.Column="0" Padding="10" Background="{Extensions:HtThemeInfo Key=ChildWindow_Content_Background}" BorderBrush="{TemplateBinding BorderBrush}">
                            <ScrollViewer BorderThickness="0" MaxHeight="400" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                                <Grid Margin="-2">
                                    <TextBox Name="tb1" Text="{Binding DisplayedText, RelativeSource={RelativeSource TemplatedParent}}" FontSize="{TemplateBinding FontSize}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" TextWrapping="Wrap" Style="{StaticResource KeyboardTextBox}"/>
                                </Grid>
                            </ScrollViewer>
                        </Border>
                        <ContentControl Grid.Row="1" ContentTemplate="{Binding KeyboardLayout, RelativeSource={RelativeSource TemplatedParent}}" />
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

    <Style TargetType="TextBox" x:Key="KeyboardTextBox">
    <Setter Property="Background" Value="#D3D3D3"/>
    <Setter Property="CaretBrush" Value="{Extensions:HtThemeInfo Key=ChildWindow_Border}"/>
    <Setter Property="SelectionBrush" Value="{Extensions:HtThemeInfo Key=ChildWindow_Border}"/>
    <Setter Property="AcceptsTab" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border 
                  Name="Border"
                  Background="{TemplateBinding Background}">
                    <ScrollViewer Margin="2,4,2,4" x:Name="PART_ContentHost"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Preview preview


Solution

  • The problem is, that the Focus is lost when you click on the Button.

    case "Tab":
        _TextBox.Focus();
        EditingCommands.TabForward.Execute(null, _TextBox);
        break;
    

    Focus the TextBox again, before you send the Command is the solution!

    Working Solution

    liveDemo