Turn off soft keyboard in uwp app for windows

旧街凉风 提交于 2019-12-20 03:36:28

问题


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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/41598350/turn-off-soft-keyboard-in-uwp-app-for-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!