WindowsPhoneGeek

WPAppInfo

Login | Join (Why?)

rss rss rss
logo

How to hide the soft keyboard in a Windows Phone app

published on: 1/13/2012 | Views: N/A | Tags: UI

by WindowsPhoneGeek

In this post I am going to show you how to how to hide the soft keyboard in a Windows Phone app when the user press enter.

Step1. In XAML, define two text block controls:

<StackPanel Orientation="Vertical">
    <TextBlock Text="Hides soft keyboard on enter:" />
    <TextBox x:Name="textBox" />
    <TextBlock Text="Normal text box:" />
    <TextBox />
</StackPanel>    

For the first text block control, we will implement hiding of the soft keyboard. The second text block control is only needed to demonstrate that the soft keyboard is not automatically hidden when the user presses the enter key.

Step2. Subscribe to the KeyUp event of the first text block control:

public MainPage()
{
    InitializeComponent();

    this.textBox.KeyUp += new KeyEventHandler(textBox_KeyUp);
}

Step3. In the KeyUp handler, add the following code to hide the soft keyboard when the enter key is pressed:

void textBox_KeyUp(object sender, KeyEventArgs e)
{
    // if the enter key is pressed
    if (e.Key == Key.Enter)
    {
        // focus the page in order to remove focus from the text box
        // and hide the soft keyboard
        this.Focus();
    }
}

Why does this work:

The soft keyboard is shown when a text input control, like a text box for example, receives focus. The keyboard is automatically hidden when the input control loses focus. In the handler of the KeyUp event, we make the text box lose focus by focusing the page, which results in the soft keyboard being hidden.

Step4. Run the application:

image image

That`s it. Here is the full source code:

Hope that the post was helpful.

You can also follow us on Twitter @winphonegeek

Comments

Nice trick

posted by: PHenry on 1/13/2012 8:35:51 PM

Very cool! Thanks for the heads up! I didn't think it would be THAT easy.

posted by: pan on 4/13/2012 1:32:25 PM

verygood

Add comment to 'How to hide the soft keyboard in a Windows Phone app'

Comment

New! WindowsPhoneGeek Component Marketplace

Our Top Articles & Free books

Our Top Tips & Samples