Solved

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

Posted on 2006-06-26
5
963 Views
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.
0
Comment
Question by:hi-Res
5 Comments
 
LVL 15

Accepted Solution

by:
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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/registerhotkey.asp

4. Map the message by placing the code between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP macros
ON_MESSAGE(WM_HOTKEY, OnHotKey)

5. Write code inside the OnHotKey method to accomplish what you want when Ctrl+M is pressed on the key board
Like this
LRESULT CMyDialog::OnHotKey(WPARAM wp, LPARAM lp)
{
      TRACE(_T("CMyDialog::OnHotKey\n"));
      if (wp==m_nIDHotKey && !IsWindowVisible()) {
            ShowWindow(SW_SHOWNORMAL);
      }
      return 0;
}


Full working sample is available at
http://msdn.microsoft.com/msdnmag/issues/1200/c/
0
 
LVL 48

Assisted Solution

by:AlexFM
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.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) 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…

839 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