« November 2005 »
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
You are not logged in. Log in

Programming Tips
Tuesday, 8 November 2005
How to register a Hotkey for your application?
Mood:  sharp
Topic: VC++
Now what's a Hotkey. An easy definition would be a key that remains hot throughout the lifetime of an 
application. Whenever you press the hotkey it does it's work no matter where the focus is where the 
input is directed to, etc. 

So here's how we go about it.

//Make this a member variable of your class.
UINT uniqueIdentifier;

//creates a unique identifier for your hotkey so that there is no hotkey id clashes
uniqueIdentifier = ::GlobalAddAtom("Somename");
::RegisterHotKey(
	m_hWnd/*Your window handle*/,
	uniqueIdentifier/*Unique identifier to uniquely identify this hotkey*/,
	MOD_ALT|MOD_CONTROL /*Modifier keys*/,
	VK_F10/*Virtual key code of any key*/
	);

//Add these message map entries
BEGIN_MESSAGE_MAP(..., ...)
     ON_MESSAGE(WM_HOTKEY, HotKeyHandler)
     ON_WM_DESTROY()
END_MESSAGE_MAP()

//toggle visibility
LRESULT CYourDialog::HotKeyHandler(WPARAM wParam, LPARAM lParam)
{
     static BOOL visible = IsWindowVisible();
     if(visible)
           ShowWindow(SW_HIDE);
     else
           ShowWindow(SW_SHOWNORMAL);
     visible = !visible;
}

void CYourDialog::OnDestroy()
{
     //if you register you will have to unregister too.
     UnregisterHotKey(m_hWnd, uniqueIdentifier);
}

Yeah this is how we go about it.
A cleaner approach would be create a wrapper class around this hotkey. So that registration and 
unregistration can take place smoothly.

Posted by Nibu babu thomas at 5:28 PM
Updated: Thursday, 10 November 2005 4:46 PM

View Latest Entries