How to register a Hotkey for your application?
Now what’s a hotkey? An easy definition would be a key that remains hot throughout the lifetime of an application. Whenever you press a hotkey it does it’s work no matter where the focus is or where the input is directed to, etc.
Here is a screenshot and a sample application(Right click and save as exe), using this application you can register or unregister any combination of a given key (Here F12). So here when you press Ctrl + Alt + Shift + WinKey + F12 this dialog toggles it’s visibility state.
So here’s how we go about it.
// Make this a member variable of your class. UINT m_UniqueIdentifier; // Creates a unique identifier for your hotkey so that there is no hotkey id clashes. // Call this probably from OnInitDialog m_UniqueIdentifier = ::GlobalAddAtom( "Somename" ); ::RegisterHotKey( m_hWnd, /*Your window handle*/ m_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) { if( IsWindowVisible() ) { ShowWindow( SW_HIDE ); } else { ShowWindow( SW_SHOWNORMAL ); }// End if }// End HotKeyHandler void CYourDialog::OnDestroy() { //if you register you will have to unregister too. UnregisterHotKey( m_hWnd, m_UniqueIdentifier ); }
A cleaner approach would be create a wrapper class around this hotkey. So that registration and unregistration can take place smoothly.
Even though unregistering may seem like not required but still IMO we should unregister a hotkey. Just following the rules! 🙂
The Old Rugged Cross
Daily Manna
Copyright and Disclaimer
Disclaimer: These postings are provided "AS IS" with no warranties, and confer no rights.
Categories
- .net (24)
- C# (16)
- VB (12)
- WindowsIdentity (1)
- WPF (1)
- ActiveDirectory (1)
- ActiveX (2)
- Announcements (4)
- ATL (5)
- Books (1)
- C Programming (14)
- C++, VC++ (96)
- CodeProject (1)
- COM (20)
- Debugging (31)
- Development Tools (5)
- DevTools (1)
- DOS (2)
- General (14)
- MVP Stuff (1)
- MVP Summit 2008 (5)
- HTML (1)
- Interop (2)
- Interview Questions (4)
- C++ (1)
- MFC (60)
- Microsoft (8)
- Windows 8 (2)
- Windows 8 Apps (1)
- Windows Phone (2)
- Microsoft Office (1)
- My thoughts (3)
- Networking (1)
- Parallel Programming (1)
- Polls (2)
- PowerShell (10)
- Sharepoint (9)
- SQL (1)
- STL (1)
- STL (1)
- STL (1)
- STL (1)
- STL (1)
- Strange bugs (6)
- Uncategorized (2)
- Undocumented WinAPI (6)
- Visual C++ Compiler (5)
- Visual studio (37)
- Visual Studio 2012 (3)
- Visual Studio 2013 (7)
- Win32Controls (1)
- WinDbg (9)
- Windows (24)
- Batch programming (4)
- Explorer (1)
- SysInternals (1)
- Tools (3)
- Windows 10 (1)
- Windows API (191)
- Windows Phone (2)
- WPF (1)
- XML (1)
- XPath (1)