Rohini Tribhuwan
Rohini Tribhuwan

Reputation: 11

Hide Virtual Keyboard from Windows App C#

I am new in UWP, I want to hide On screen keyboard which pops up on focus on textbox.I already have numeric pad to accept the input from user. How to avoid keyboard's automatic functionality.

Tried with PreventKeyboardDisplayOnProgrammaticFocus="True" and InputPane.GetForCurrentView().Showing += (s, e) => (s as InputPane).TryHide();

but no use.

Upvotes: 0

Views: 1016

Answers (1)

Anran Zhang
Anran Zhang

Reputation: 7727

You can set PreventKeyboardDisplayOnProgrammaticFocus on TextBox to True, this can solve your problem.

Update

When the user clicks on the TextBox, the FocusState of the space is Pointer, not Programmatic, so the PreventKeyboardDisplayOnProgrammaticFocus property does not work.

This is a Hack method that achieves your purpose through visual spoofing:

<Grid>
    <TextBox x:Name="HideTextBox" Width="1" Height="1" PreventKeyboardDisplayOnProgrammaticFocus="True"/>
    <TextBox x:Name="ShowTextBox" GotFocus="ShowTextBox_GotFocus" IsReadOnly="True" Text="{Binding ElementName=HideTextBox,Path=Text}"/>
</Grid>

code-behind:

private void ShowTextBox_GotFocus(object sender, RoutedEventArgs e)
{
    HideTextBox.Focus(FocusState.Programmatic);
}

As you can see, when ShowTextBox is set to ReadOnly, it does not trigger the virtual keyboard. When it gets the focus, we programmatically shift the focus to the "hidden" HideTextBox. At this time, the virtual keyboard will be intercepted. User-entered content can be obtained by binding.

It's not perfect, I also look forward to a better way to solve this problem.

Best regards.

Upvotes: 1

Related Questions