Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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