We had a bug raised that when the user presses enter on the sign in screen the login process begins but the soft keyboard (the on-screen keyboard) doesn’t get dismissed so the user gets the impression they can keep interacting with it. Through some Monkey Testing this produced a bug where the application would crash because it would fire off multiple requests to log in as they could keep hitting enter and eventually crashing the application.
The logical solution is to hide the soft keyboard.
But here’s a question, how would you hide the keyboard in Windows 8 XAML?
Thought #1 - remove focus
I’m a web guy so when I want to defocus an element I use
blur, so that’s my first point of call.
But of course there’s no
Blur method on XAML elements. Strike that off the list.
Thought #2 - change focus
But every time I set it the app would crash with an
AccessViolationException (or something to that effect). Great, that’s no help now is it! Moving on…
Thought #3 - FocusManager
No, that’d be too simple! Guess we can strike this one down too
One hack to rule them all
Since you don’t have access to the soft keyboard programmatically and every attempt made to change focus was either throwing exceptions or simply missing anything useful it was time to think outside the box.
It was time for a hack!
A funny thing about input controls in Windows 8 XAML is if they are not enabled, ie - read-only, the soft keyboard wont display for them. Well that makes sense doesn’t it and hey, they were bound to get something right eventually!
This gave me an idea, let’s make the textbox read-only. The only problem is this has to be done as early as possible, even before we validate, to prevent undesired keyboard mashing. This also means that our validation wont be done so there’s a chance that we’ll have a failure in sign in and need to make the fields writeable again.
So all we did was add this to our event handler:
username.IsEnabled = password.IsEnabled = false; username.IsEnabled = password.IsEnabled = true;
Yes, one line after the other like that and you hide your soft keyboard. facepalm
Want to hide your soft keyboard in Windows 8 XAML?