Category Archives: Windows 8

Windows 8: Hide Virtual Keyboard Programmatically

The visibility of the virtual keyboard in Windows 8 Metro Style Apps depends on whether a text input enabled control is focused or not:

In the left image the user tapped into a TextBox control. This tap sets the focus on that TextBox and the virtual keyboard appeared. After having entered some Text the user taps the “OK” button and the virtual keyboard hides again due to the loss of focus (right image).

with-keyboard_thumb1    without-keyboard-copy_thumb1

But sometimes there is no Button control to hit after having entered some text and thus the virtual keyboard does not hide and still might cover important parts of the UI. In order to hide the virtual keyboard no matter where the user taps outside the TextBox one could set the focus to a hidden button programmatically.

See the following example:-

XAML Markup:-

 <Grid>
      <Button x:Name="hiddenButton" Opacity="0" />
      <TextBox Width="300" Margin="50" LostFocus="TextBox_LostFocus" />
 </Grid>

 

C# Code Behind:- 

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
      this.hiddenButton.Focus(Windows.UI.Xaml.FocusState.Pointer);
}

Happy Programming :-)

Skip to toolbar