Archis's Blog

August 12, 2008

How to really handle the "back" key on a Smartphone

Filed under: Uncategorized — Tags: , , , , , , — archisgore @ 2:06 pm

There’s a lot of documentation and blogs on using the SHSendBackToFocusWindow API on Smartphones for intercepting back keys and sending them to EDIT controls on your window. As anyone who has actually taken the trouble to code on a smartphone will soon realise, this isn’t always enough to give you the complete effect you need.

Specifically, here’s how the EDIT controls are supposed to respond to the back key:
1. If the EDIT control is empty or has no focus, and the user presses the back key, it should behave as the default behaviour – which is to take you back to the previous window that had focus.
2. If the EDIT has focus, and is non-empty, the back-key should behave like a backspace.

Most docs that I found recommend the following usage of the back key handling in your WM_HOTKEY message (perhaps because that’s the example given on MSDN):

WM_HOTKEY:
{
if(HIWORD(lParam) == VK_TBACK)
SHSendBackToFocusWindow(uMessage, wParam, lParam);
}


However, the behaviour this creates is to trap all back key presses and send them back to the focussed window and hence once your window has focus, the back key is effectively disabled unless it’s used as a backspace by an EDIT control. I’m sure you’ve found this quite irritating – it drove me nuts trying to figure this one out – and there aren’t many code-samples explaining this.

Heck, I didn’t even know about the SHNavigateBack API which allows you to cause the default back-key behaviour. Once you get your hands on this, things are pretty simple. In your WM_HOTKEY handler, all you do is get the currently focussed window:

HWND hWndFocussed = GetFocus()

Then check whether it’s an edit control by getting it’s class (probably with one of the Class Info APIs). And finally, get the window’s text (using GetWindowText) and strcmp it with a blank string (a pair of double-quotes). The pesudocode should look something like (I don’t include actual code to protect both you and me):

WM_HOTKEY:
{
HWND hWndFocussed = GetFocus();
if(HIWORD(lParam) == VK_TBACK &&
IsEditControl(hWndFocussed) &&
IsNonEmptyText(hWndFocussed)
)
{
SHSendBackToFocusWindow(uMessage, wParam, lParam);
}
else if ( //your dialog has a default cancel action
{
//Do cancel behaviour here
}
else
{
SHNavigateBack();
}
}

With any luck, this should give your apps a “native” feel making them behave like any other Smartphone apps.

1 Comment »

  1. I found this sample code which worked for me…

    http://www.ceveni.com/2009/01/how-to-handle-back-key-on-windows.html

    Comment by Debbie Jones — January 17, 2009 @ 12:08 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 122 other followers