Solved

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

Posted on 2006-06-26
5
939 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now