Click here to Skip to main content
6,597,576 members and growing! (22,542 online)
Email Password   helpLost your password?
General Reading » Hardware & System » General     Beginner License: The Microsoft Public License (Ms-PL)

Beginner's Tutorial - Using global hotkeys

By Nishant Sivakumar

Explains how to register, use and unregister hotkeys
VC6, VC7Win2K, WinXP, Dev
Posted:28 Apr 2002
Views:145,670
Bookmarked:45 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
46 votes for this article.
Popularity: 7.22 Rating: 4.34 out of 5

1

2
1 vote, 3.3%
3
5 votes, 16.7%
4
24 votes, 80.0%
5

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)

About the Author

Nishant Sivakumar


Member
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 58 (Total in Forum: 58) (Refresh)FirstPrevNext
GeneralGetting a compilation error PinmemberNitsanBr8:36 31 Aug '08  
GeneralQuery on RegisterHotKey PinmemberMember 407504820:44 17 Jul '08  
GeneralHotKey for IE Pinmemberlal001223:10 17 Jun '08  
Questionhow to trap Hotkey without using Massage Map in win32 app? Pinmemberrajksingh21:28 4 May '08  
GeneralThanks - Pinmemberbruccutler8:19 31 Jan '08  
GeneralMinor correction - PinmemberSabuncu9:12 14 Aug '07  
GeneralThanks PinmemberHakunaMatada20:26 15 May '07  
Generalhow to remap keyboard globally Pinmembertedkidane6:57 11 May '07  
Question? Focus PinmemberSynetech19:25 22 Jul '06  
GeneralHow to make this example to work with Visual C++2005? Pinmemberhires775:18 25 Jun '06  
QuestionHow to Use F1...F12 and other Virtual keys as a Hotkey !! plzzz help PinmemberManni Singh10:28 7 Jun '06  
AnswerRe: How to Use F1...F12 and other Virtual keys as a Hotkey !! plzzz help PinmemberSynetech10:22 4 May '08  
GeneralgOoD PinmemberLivingThoughts19:17 11 Feb '06  
GeneralIs there anyway to handle HOTKEY messages like "Control + Alt + Delete" in one's application? PinmemberSstar16:31 7 Feb '06  
GeneralRe: Is there anyway to handle HOTKEY messages like "Control + Alt + Delete" in one's application? PinmemberLivingThoughts21:16 14 Feb '06  
Generalctrl+alt+del is reserved for operating system use Pinmembercwb310610:35 5 Sep '08  
GeneralRe: ctrl+alt+del is reserved for operating system use Pinmemberguocang8:41 18 Sep '08  
GeneralHow to trap F8 key for specified exe? Pinmemberdkdsnaidu20:41 14 Jul '05  
GeneralThank you Thank you Thank you! Pinmemberbalaclavabob2:16 7 Jul '05  
General? Getting More Info On Existing Hotkeys PinmemberSynetech13:56 20 Jun '05  
GeneralRe: ? Getting More Info On Existing Hotkeys PinmemberSynetech20:09 8 Aug '08  
GeneralRe: ? Getting More Info On Existing Hotkeys PinmemberAngus Comber0:27 23 May '09  
GeneralRe: ? Getting More Info On Existing Hotkeys PinmemberSynetech8:06 23 May '09  
GeneralRe: ? Getting More Info On Existing Hotkeys PinmemberAngus Comber8:15 23 May '09  
GeneralRe: ? Getting More Info On Existing Hotkeys PinmemberSynetech8:18 23 May '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Apr 2002
Editor: Nishant Sivakumar
Copyright 2002 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project