Click here to Skip to main content
11,490,973 members (70,758 online)
Click here to Skip to main content

Beginner's Tutorial - Using global hotkeys

, 28 Apr 2002 Ms-PL 239.1K 4.2K 59
Rate this:
Please Sign up or sign in to vote.
Explains how to register, use and unregister hotkeys

Introduction

Today morning, someone was asking about using hot keys in the VC++ forum. I had never used hotkeys and it intrigued me not a little. I thought I'd cook up a simple application and write a nice little article for CP. These days I have been writing too many .NET articles and I thought it's time I wrote a normal unmanaged program as Mike Dunn or Colin Davies would call it.

First of all remember that hot keys are going to be global to the Operating System. Thus be careful what you choose as your hot key. Also decide whether your application is important enough to have an Operating System level short cut key. The user might accidentally press Ctrl-D and suddenly find your program popping up. In fact if it's an old enough person the sudden popping up of an unexpected window might even give him or her a heart attack. This is obviously a situation to be avoided.

How to set hot keys

Well, setting hot keys is just an API call away for anyone who might be thinking it's a complicated process. We use the API call RegisterHotKey which is prototyped as follows.

BOOL RegisterHotKey(
    HWND hWnd,         // window to receive hot-key notification
    int id,            // identifier of hot key
    UINT fsModifiers,  // key-modifier flags
    UINT vk            // virtual-key code
);

A normal application can use any value between 0x0000 and 0xBFFF but if you are writing a DLL, then you must use GlobalAddAtom to get a unique identifier for your hot key. There are currently four allowed key modifiers which let you trap the Ctrl key, the Alt key, the Shift key and the WinKey. You may trap them individually or as combinations of each other. For example you can have a combination short cut like Ctrl-Shift-WinKey-Y to pop up yahoo messenger, though why you'd want to set up that kind of convoluted short cut would be a very tough question to answer.

RegisterHotKey(hWndA, 100, MOD_ALT | MOD_SHIFT, 'P');
RegisterHotKey(hWndA, 200, MOD_WIN, 'R');

Okay, setting the hot key was simple. So, what does this do? Good question! What we have achieved is that, whenever this hot key is pressed a WM_HOTKEY message is send to the window specified by the HWND parameter. Neat. Also remember that if you are trying to set a hot key that has already been registered like the WinKey-E hot key that brings up Explorer, this function will fail and return FALSE, else it will return TRUE. So please do not fail to check the return value.

Un-registering hotkeys

Well as you would expect, there is an API call called UnregisterHotKey which un-registers our hot key or hot keys. Remember to un-register all hot keys when your program exits. In fact just to be safe un-register a key which might actually already have been un-registered. No harm done at all. This function is prototyped as :-

BOOL UnregisterHotKey(
    HWND hWnd,  // window associated with hot key
    int id      // identifier of hot key
);

The identifier is the same one which we had passed to RegisterHotKey. If you had used GlobalAddAtom to get a unique identifier, you must save it somewhere so that you can use it to un-register the hot key.

UnregisterHotKey(hWndMain,300);

Handling the hot key

If you are writing a straight API program you shouldn't have too much difficulty in handling the WM_HOTKEY message. Just check wParam which will contain the identifier of the hot key. The problem with MFC is that for some strange reason, the class wizard does not seem to include the WM_HOTKEY message. I was using VC++ 6.0 with SP5 when i originally wrote the program. I am not aware of whether this is the same for VC++ 7.0 or even whether there is some kind of obscure workaround for VC++ 6.0. If anyone knows a work around, kindly let me know too. But there is nothing stopping us from adding the entry to the message map. It's perfectly within the rules to do that, I say.

ON_MESSAGE(WM_HOTKEY,OnHotKey)

Okay, that was easy. Now we add our function. Just add this function to your window class, the same window that is going to receive the WM_HOTKEY message.

LRESULT OnHotKey(WPARAM wParam, LPARAM lParam);

Now all you have to do is to write the function body, check wParam and see if it is the identifier of your hot key and do what you want. Typically you might want to use ShellExecute to start your program, which is what I have done in the sample program for this article.

Sample Program

Well. the sample program was quickly put together and it lets you choose a single modifier key (it does not allow multiple modifiers) and you can choose a virtual key between A and Z. You can also browse to an executable and when you click the Start button, the hot key is alive. It's a very simple program and does not really do much. But you can look through the source code, if my explanations weren't coherent enough for you.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Nish Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

Comments and Discussions

 
QuestionBut RegisterHotKey overwites hotkeys of other tools Pin
TheCoderGuy25-Nov-14 21:03
memberTheCoderGuy25-Nov-14 21:03 
AnswerRe: But RegisterHotKey overwites hotkeys of other tools Pin
TheCoderGuy26-Nov-14 19:03
memberTheCoderGuy26-Nov-14 19:03 
QuestionGerman umlauts Pin
rootdial19-Jul-13 1:03
memberrootdial19-Jul-13 1:03 
GeneralMy vote of 5 Pin
Member 96348733-Dec-12 19:28
memberMember 96348733-Dec-12 19:28 
QuestionDownload link Pin
subheesh23-Aug-11 18:27
membersubheesh23-Aug-11 18:27 
AnswerRe: Download link Pin
Nishant Sivakumar24-Aug-11 12:32
mvpNishant Sivakumar24-Aug-11 12:32 
Question.net hotkeys Pin
tsdaemon29-Jun-11 2:03
membertsdaemon29-Jun-11 2:03 
GeneralMy vote of 5 Pin
samq17-Nov-10 3:15
membersamq17-Nov-10 3:15 
Generalbad word filter to protect my kids. Pin
ZUPERKOOL7-Feb-10 7:48
memberZUPERKOOL7-Feb-10 7:48 
GeneralGetting a compilation error Pin
NitsanBr31-Aug-08 8:36
memberNitsanBr31-Aug-08 8:36 
GeneralQuery on RegisterHotKey Pin
Member 407504817-Jul-08 20:44
memberMember 407504817-Jul-08 20:44 
GeneralRe: Query on RegisterHotKey Pin
zapaaris18-Aug-11 10:51
memberzapaaris18-Aug-11 10:51 
GeneralHotKey for IE Pin
lal001217-Jun-08 23:10
memberlal001217-Jun-08 23:10 
Questionhow to trap Hotkey without using Massage Map in win32 app? Pin
rajksingh4-May-08 21:28
memberrajksingh4-May-08 21:28 
GeneralThanks - Pin
bruccutler31-Jan-08 8:19
memberbruccutler31-Jan-08 8:19 
GeneralMinor correction - Pin
Sabuncu14-Aug-07 9:12
memberSabuncu14-Aug-07 9:12 
GeneralThanks Pin
HakunaMatada15-May-07 20:26
memberHakunaMatada15-May-07 20:26 
Questionhow to remap keyboard globally Pin
tedkidane11-May-07 6:57
membertedkidane11-May-07 6:57 
Question? Focus Pin
Synetech22-Jul-06 19:25
memberSynetech22-Jul-06 19:25 
QuestionHow to make this example to work with Visual C++2005? Pin
hires7725-Jun-06 5:18
memberhires7725-Jun-06 5:18 
QuestionHow to Use F1...F12 and other Virtual keys as a Hotkey !! plzzz help Pin
Manni Singh7-Jun-06 10:28
memberManni Singh7-Jun-06 10:28 
AnswerRe: How to Use F1...F12 and other Virtual keys as a Hotkey !! plzzz help Pin
Synetech4-May-08 10:22
memberSynetech4-May-08 10:22 
GeneralgOoD Pin
LivingThoughts11-Feb-06 19:17
memberLivingThoughts11-Feb-06 19:17 
QuestionIs there anyway to handle HOTKEY messages like "Control + Alt + Delete" in one's application? Pin
Sstar7-Feb-06 16:31
memberSstar7-Feb-06 16:31 
AnswerRe: Is there anyway to handle HOTKEY messages like "Control + Alt + Delete" in one's application? Pin
LivingThoughts14-Feb-06 21:16
memberLivingThoughts14-Feb-06 21:16 
Generalctrl+alt+del is reserved for operating system use Pin
cwb31065-Sep-08 10:35
membercwb31065-Sep-08 10:35 
GeneralRe: ctrl+alt+del is reserved for operating system use Pin
guocang18-Sep-08 8:41
memberguocang18-Sep-08 8:41 
QuestionHow to trap F8 key for specified exe? Pin
dkdsnaidu14-Jul-05 20:41
memberdkdsnaidu14-Jul-05 20:41 
GeneralThank you Thank you Thank you! Pin
balaclavabob7-Jul-05 2:16
memberbalaclavabob7-Jul-05 2:16 
General? Getting More Info On Existing Hotkeys Pin
Synetech20-Jun-05 13:56
memberSynetech20-Jun-05 13:56 
GeneralRe: ? Getting More Info On Existing Hotkeys Pin
Synetech8-Aug-08 20:09
memberSynetech8-Aug-08 20:09 
GeneralRe: ? Getting More Info On Existing Hotkeys Pin
Angus Comber23-May-09 0:27
memberAngus Comber23-May-09 0:27 
GeneralRe: ? Getting More Info On Existing Hotkeys Pin
Synetech23-May-09 8:06
memberSynetech23-May-09 8:06 
GeneralRe: ? Getting More Info On Existing Hotkeys Pin
Angus Comber23-May-09 8:15
memberAngus Comber23-May-09 8:15 
GeneralRe: ? Getting More Info On Existing Hotkeys Pin
Synetech23-May-09 8:18
memberSynetech23-May-09 8:18 
GeneralRe: ? Getting More Info On Existing Hotkeys Pin
Angus Comber23-May-09 8:21
memberAngus Comber23-May-09 8:21 
GeneralWin 98 hotkey problem Pin
istynet27-Oct-04 5:18
memberistynet27-Oct-04 5:18 
GeneralRe: Win 98 hotkey problem Pin
bispenho28-Dec-05 8:00
memberbispenho28-Dec-05 8:00 
GeneralThanks Pin
james32513-Sep-04 6:24
memberjames32513-Sep-04 6:24 
GeneralGreat!! Pin
Waleed6-Aug-04 15:29
memberWaleed6-Aug-04 15:29 
QuestionHow do a system wide hot key in C# Pin
pschiff3-May-04 12:27
memberpschiff3-May-04 12:27 
AnswerRe: How do a system wide hot key in C# Pin
Jigar M25-Apr-08 12:36
memberJigar M25-Apr-08 12:36 
GeneralHotkey functionality in Win 9X family Pin
Antti Keskinen28-Oct-03 3:10
memberAntti Keskinen28-Oct-03 3:10 
Generalhot keys & mouse button virtual keys Pin
ducl_1315-Apr-03 21:40
memberducl_1315-Apr-03 21:40 
GeneralRe: hot keys & mouse button virtual keys Pin
Nishant S19-Apr-03 20:55
editorNishant S19-Apr-03 20:55 
Not through the hotkey API. You'll have to write a global mouse hook

Nish


"I'm a bit bored at the moment so I'm thinking about writing a new programming language" - Colin Davies

My book :- Summer Love and Some more Cricket [New Win]
Review by Shog9 Click here for review[NW]

QuestionSkip fsModifiers? Pin
Gopalan9-Feb-03 4:18
memberGopalan9-Feb-03 4:18 
AnswerRe: Skip fsModifiers? Pin
Nishant S9-Feb-03 6:01
editorNishant S9-Feb-03 6:01 
GeneralMultiple Hotkey Pin
eponk30-Dec-02 2:05
membereponk30-Dec-02 2:05 
GeneralRe: Multiple Hotkey Pin
Nishant S9-Feb-03 6:02
editorNishant S9-Feb-03 6:02 
QuestionHow to send keystrokes to another window in OnHotKey()? Pin
xyu25-Aug-02 21:05
memberxyu25-Aug-02 21:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 29 Apr 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid