Search code examples
uwpwindows-10-mobile

Turn off soft keyboard in uwp app for windows


I have an uwp app running on a handheld device running windows 10. The handheld device has a barcode scanner and all input for the app is made using this. So I want to prevent the keyboard coming up when a user moves the focus to any of the textbox controls.

To a large extent, the focus is handled programmatically - and I have prevented the keyboard coming up in those instances with PreventKeyboardDisplayOnProgrammaticFocus=True. But the user does need to move the focus himself sometimes and I cannot find any way of preventing the keyboard coming up when he does this.

I have found articles regarding the programmatic focus mentioned above, and hiding the keyboard when the user presses enter in a textbox - and setting the readonly value to true for the control. But these are not applicable in this case. I want to be able to prevent it coming up ever in this app. Can anyone advise?


Solution

  • I'm not sure if there is a direct way to prevent keyboard from showing up. You can surely hide the keyboard once it shows, by subscribing to InputPane's events:

    InputPane.GetForCurrentView().Showing += (s, e) => (s as InputPane).TryHide();
    

    But this doesn't look nice. Therefore I've tried a tricky way to achieve what you want - disable the TextBox for hit testing and use dummy control under it to set programmatic focus. As I've tested it should work. The sample xaml:

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Border Tapped="Border_Tapped" Background="Transparent">
            <TextBox x:Name="myTextBox" Width="200" Height="100" Header="Enter:" PreventKeyboardDisplayOnProgrammaticFocus="True" IsHitTestVisible="False"/>
        </Border>
        <Button Margin="20" Content="Dummy to test focus"/>
    </StackPanel>
    

    And the code behind:

    private void Border_Tapped(object sender, TappedRoutedEventArgs e)
    {
        myTextBox.Focus(FocusState.Programmatic);
    }