Partager via


How to Override the 'Back' Key in your Web Browser Control Application

I just helped a customer with a problem he was having with this web application : when correcting an entry in an edit field by pressing the 'back' key, instead of the last character getting deleted and the cursor moving one space to the left, the application would quit. This was very frustrating since the same scenario works with Internet Explorer Mobile. His application is using the Web Browser control to display the web pages that make up his application. The Web Browser control allows you to use the full power of Internet Explorer Mobile, with the ability to provide your own menus and to capture and respond to events from the DOM. The application is running on Window Mobile Standard 6.0 and 5.0. This is an important point since Windows Mobile Professional devices do not have a 'back' key.

'Back' Key causing App to Quit? 
     'Back' Key causing App to Quit?

So what is going on here? Why does 'back' in the Web Browser control not behave as it does in IE Mobile?  By default, the web browser control does not do any special handling of the back key. The default behavior of the back key is to activate the previously top level window in the Z-order. (His application was not actually quitting, it was just going to the bottom of the Z-order.) To duplicate the IE Mobile back key behavior you need to capture the 'back' key press, and forward it to the window that has the focus. Fortunately, the Windows Mobile SDK includes an API to do just that :  the SHCMBM_OVERRIDEKEY message and the API SHSendBackToFocusWindow(). I modified the SDK sample miniPie using the code below:

LRESULT CMainWindow::OnCreate(UINT /*uMsg*/,  WPARAM /*wParam*/, 
     LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
...

     // For devices that have a back key (i.e. Smartphones),
    // override the back key since we have an edit control 
    CWindow(mbi.hwndMB).SendMessage(SHCMBM_OVERRIDEKEY,
         VK_TBACK, MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY,
         SHMBOF_NODEFAULT | SHMBOF_NOTIFY));
...
    return 1;  // Let the system set the focus
}

LRESULT CMainWindow::OnHotKey(UINT uMsg, WPARAM wParam, 
     LPARAM lParam, BOOL& /*bHandled*/)
{
    if (VK_TBACK == HIWORD(lParam)) // Smartphone-only
    {
        SHSendBackToFocusWindow(uMsg, wParam, lParam);
    }
    return 0;
}

del.icio.us Tags: windowsmobile,web,smartphone,standard,web browser control

Comments

  • Anonymous
    January 23, 2008
    The blog was a bit quiet over the holidays... if you hadn't figure it out yet, Steve's a Christmas maniac

  • Anonymous
    January 23, 2008
    The blog was a bit quiet over the holidays... if you hadn't figure it out yet, Steve's a Christmas