Click here to Skip to main content
13,297,799 members (47,220 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 8 May 2003

Global hotkeys made easy

, 19 Oct 2003
Rate this:
Please Sign up or sign in to vote.
A class to facilitates global hotkeys registration and handling
<!-- Add the rest of your HTML here -->


This article will show you how to use the CHotkeyHandler class in order to manage and create global hotkeys.

Using the code

<PRE lang=c++>#include <conio.h> #include <stdio.h> #include "hotkeyhandler.h" void handA(void *) { printf("this is A\n"); } void handQ(void *) { printf("this is Q\n"); } void hand1(void *param) { WinExec((char *)param, SW_SHOW); } int main(void) { int err, id; CHotkeyHandler hk; hk.InsertHandler(MOD_CONTROL | MOD_ALT, 'Q', handQ, id); hk.InsertHandler(MOD_CONTROL | MOD_ALT, 'A', handA, id); hk.InsertHandler(MOD_CONTROL | MOD_ALT, '1', hand1, id); err = hk.Start("calc.exe"); if (err != CHotkeyHandler::hkheOk) { printf("Error %d on Start()\n", err); return err; } printf("hotkeys started!!!\ any key to stop them...\n"); getch(); err = hk.Stop(); return 0; }

First we declare a CHotkeyHandler instance. We then start inserting the hotkeys defined by their Control key, Virtual Key, Callback function. The InsertHandler() will return us an identifier for the registered hotkey. You can use this id with the RemoveHandler(). After we have inserted our handlers you can enable the hotkeys by calling Start(). You should always check for error codes returned by the CHotkeyHandler methods. The Start() will take an optional parameter that will be passed to the registered callback when it gets invoked. We then disable all hotkeys via Stop()

For more information about error codes and other methods description please refer to the CHotkeyHandler.cpp file. That's all.


  • 9 May 2003
    • Initial CodeProject version
  • 20 Oct 2003
    • Source code updated<!------------------------------- That's it! --------------------------->


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Elias Bachaalany
Web Developer
United States United States
Elias (aka lallousx86, @0xeb) has always been interested in the making of things and their inner workings.

His computer interests include system programming, reverse engineering, writing libraries, tutorials and articles.

In his free time, and apart from researching, his favorite reading topics include: dreams, metaphysics, philosophy, psychology and any other human/mystical science.

Former employee of Hex-Rays (the creators of IDA Pro), was responsible about many debugger plugins, IDAPython project ownership and what not.

Elias currently works at Microsoft as a software security engineer.

More articles and blog posts can be found here:


You may also be interested in...


Comments and Discussions

GeneralProblem while compiling in VC++ 2008 Pin
NitsanBr30-Aug-08 13:41
memberNitsanBr30-Aug-08 13:41 
GeneralRe: Problem while compiling in VC++ 2008 Pin
428814-Aug-09 11:38
member428814-Aug-09 11:38 
GeneralErrors and problems Pin
Val Samko18-Oct-03 12:26
memberVal Samko18-Oct-03 12:26 
GeneralRe: Errors and problems Pin
lallous20-Oct-03 2:48
memberlallous20-Oct-03 2:48 
Generallocal hotkey Pin
shahrzad4-Aug-03 2:00
membershahrzad4-Aug-03 2:00 
GeneralRe: local hotkey Pin
Anonymous9-Aug-03 12:01
sussAnonymous9-Aug-03 12:01 
GeneralHotkeys Vs. KB Hooks Pin
sumudu12-Jun-03 7:41
membersumudu12-Jun-03 7:41 
I've been using global hotkeys (with MFC) for a while.

Recently, I found out that what I was doing with the hotkeys can be done using a keyboard hook as well. I don't know much about hooks. So, I was wonder if anybody could help me by listing the pros and cons of the two methods as opposed to each other

Much appreciated.
Big Grin | :-D
GeneralRe: Hotkeys Vs. KB Hooks Pin
lallous18-Jun-03 4:04
memberlallous18-Jun-03 4:04 
GeneralRe: Hotkeys Vs. KB Hooks Pin
sumudu18-Jun-03 7:42
membersumudu18-Jun-03 7:42 
GeneralRe: Hotkeys Vs. KB Hooks Pin
JGHG24-Jun-04 3:04
memberJGHG24-Jun-04 3:04 
GeneralRe: Hotkeys Vs. KB Hooks Pin
lallous24-Jun-04 3:28
memberlallous24-Jun-04 3:28 
Generalsynchronous Pin
lins0121-May-03 19:58
memberlins0121-May-03 19:58 
GeneralRe: synchronous Pin
lallous23-May-03 0:56
memberlallous23-May-03 0:56 
GeneralRe: synchronous Pin
lins0127-May-03 20:49
memberlins0127-May-03 20:49 
GeneralRe: synchronous Pin
lallous27-May-03 23:28
memberlallous27-May-03 23:28 
GeneralRe: synchronous Pin
lins0128-May-03 15:47
memberlins0128-May-03 15:47 
GeneralNot exactly.. Pin
peterchen12-May-03 23:14
memberpeterchen12-May-03 23:14 
GeneralRe: Not exactly.. Pin
lallous13-May-03 22:40
susslallous13-May-03 22:40 
GeneralRe: Not exactly.. Pin
peterchen13-May-03 23:28
memberpeterchen13-May-03 23:28 
GeneralRe: Not exactly.. Pin
lallous13-May-03 23:43
memberlallous13-May-03 23:43 
GeneralExample doesn't work for me Pin
Mr.X12-May-03 4:04
memberMr.X12-May-03 4:04 
GeneralRe: Example doesn't work for me Pin
lallous25-May-03 11:12
memberlallous25-May-03 11:12 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 20 Oct 2003
Article Copyright 2003 by Elias Bachaalany
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid