Solved

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

Posted on 2006-06-26
5
980 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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