RegisterHotKey in Visual C++ 2005. How to use it and reicive information from WM_HOTKEY?

Posted on 2006-06-26
Last Modified: 2008-01-09
I`m creating a small program in VC++ 2005. This program is a Windows Form Application. Program is almost complete. Currently I`m trying to implement a system wide hotkey in this application. I have searched all over the internet for a clear to a novice user solution.
My intention was to enable and disable a built in Timer using two hotkeys, even when the application is minimized.
From all the solutions I have seen on various websites, I have noticed that hotkeys require the usage of: user32.lib, windows.h, RegisterHotKey() function, onHotKey() function and ON_MESSAGE(WM_HOTKEY, OnHotKey). A call for onHotKey() inside of a message map?

I`m a novice in C++ and Visual C++ 2005.
I`m asking for a sample code, not a link to msdn or google.
Question by:hi-Res
LVL 15

Accepted Solution

lakshman_ce earned 250 total points
ID: 16986106
1. Declare UINT variable as ID for hot key
UINT            m_nIDHotKey;                               // hot key identifier

2. Assign a unique value to your ID. For e.g
m_nIDHotKey = GlobalAddAtom("YourApp");
where YourApp is the name of your application

3. Registry the hot key you want to handle
RegisterHotKey(m_hWnd, m_nIDHotKey, MOD_CONTROL, 'M');
m_hWnd - handle to your app
MOD_CONTROL - Specifie that control key need to be activated
M - Keyboard letter 'M' to be pressed
Look for more options at

4. Map the message by placing the code between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP macros

5. Write code inside the OnHotKey method to accomplish what you want when Ctrl+M is pressed on the key board
Like this
      if (wp==m_nIDHotKey && !IsWindowVisible()) {
      return 0;

Full working sample is available at
LVL 48

Assisted Solution

AlexFM earned 250 total points
ID: 16986545
Is this managed application? In this case you can use code from lakshman_ce's post, with two changes:

RegisterHotKey(m_hWnd, m_nIDHotKey, MOD_CONTROL, 'M');
should be:
RegisterHotKey((HWND)this->Handle.ToPointer(), m_nIDHotKey, MOD_CONTROL, L'M');

Message should be handled in WndProc function.


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question