Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
4/14/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:

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_CREATEis a convenient place for the following code.

    Note:
    The call to the Windows Embedded CE function RegisterHotKeyuses MOD_WINas the keyboard shortcut modifier because the hardware keys all send MOD_WINwith their key code.
    Copy 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_HOTKEYmessage as part of the WndProcmethod 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.
    Copy Code
    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 UnregisterHotkeyto release the program key back to the shell, as shown in the following example.

    Copy Code
    	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