Share via


How to Modify Program Key Behavior

4/19/2010

You can easily extend the program-key associated behavior of your application. This topic uses an example application called testkeys (testkeys.exe) to demonstrate the following tasks:

  • How a program key is assigned and registered.
  • How to check whether your application is in the foreground and bring it forward if it is not.
  • How to release the program key when the application is closed.

To determine which program key is currently assigned to testkeys and to register that keyboard shortcut when testkeys.exe starts

  • The message handler for WM_CREATE is a convenient place for the following code.

    Note

    The call to the Windows Embedded CE function RegisterHotKey uses MOD_WIN as the keyboard shortcut modifier because the hardware keys all send MOD_WIN with their key code.

    BYTE appkey;
    appkey = SHGetAppKeyAssoc(_T("testkeys.exe"));
    if (appkey != 0)
    {
        if (!RegisterHotKey(hWnd, 400, MOD_WIN, appkey)) 
        {
            // Can't register keyboard shortcut.
            MessageBox(NULL, _T("Can't register hotkey."),
                             _T("Warning"), MB_OK);
            exit(0);  // Replace with specific error handling.
        }
    }
    

To check whether your application is in the foreground and to bring it to the foreground if it is not

  • The following example assumes you are handling the WM_HOTKEY message as part of the WndProc method used to handle Windows messages.

    Note

    You also need code to perform any special action, such as changing views, based on pressing the keyboard shortcut.

    case WM_HOTKEY:
        switch(wParam)
        {
            case 400:
        // This is the keyboard shortcut you registered.
        // Is the application in the foreground?
        if (hWnd != GetForegroundWindow())
        {
        // No, bring it forward and that is all.
        // Set focus to foremost child window.
        // The "| 0x01" is used to bring any owned 
        // windows to the foreground and open them.
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
        }
        else
        {
        // Already on top, so take an action.
        MessageBox(hWnd, _T("Hotkey Pressed"), 
                         _T("Hotkey"), MB_OK);
        }
        break;
    

To release the program key back to the shell

  • When the application closes, it should call UnregisterHotkey to release the program key back to the shell, as shown in the following example.

        case WM_DESTROY:
            appkey = SHGetAppKeyAssoc(_T("testkeys.exe"));
            if (appkey != 0)
            {
                if(!UnregisterHotKey(hWnd, 400)) 
            {
                // Can't unregister hotkey.
                MessageBox(NULL, _T("Can't unregister hotkey"),
                                 _T("Warning"), MB_OK);
                exit(0);  // Replace with specific error handling.
            }
        }
    

See Also

Tasks

How to Program a Press-and-Hold Feature

Concepts

Program Keys

Other Resources

Mobile Device Hardware Overview