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:
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
New! WindowsPhoneGeek Component Marketplace
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
