Click here to Skip to main content
Click here to Skip to main content

Global hotkeys made easy

By , 19 Oct 2003
 

Introduction

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

Using the code

#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!!!\n...press 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.

History

  • 9 May 2003
    • Initial CodeProject version
  • 20 Oct 2003
    • Source code updated

License

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
Member
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:
 
- http://lallousx86.wordpress.com/
- http://0xeb.wordpress.com/
- http://www.hexblog.com/?author=3

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNot exactly..memberpeterchen12 May '03 - 22:14 
Not exactly what I expect from a tutorial.
 
A step-by-step introduction what are "global hotkeys", various possibilities to implement them with their advantages and downsides, and a detailed explanation of the implementation you chose.
 
A sample project with a code snippet showing how to use it is barely enough for "normal" article, less so for a tutorial.
 
Please consider reworking the article, or removing it from the tutorial section

 

"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS

sighist | Agile Programming | doxygen

GeneralRe: Not exactly..susslallous13 May '03 - 21:40 
Hello Peter,
 
In codeproject, I saw lots of articles just like this one that demonstrates how to use a class and no more.
 
I do explain things very well inside the source code itself.
However I see that you are right, maybe I should expand this article.
 
Regards,
Lallous
GeneralRe: Not exactly..memberpeterchen13 May '03 - 22:28 
Hi Lallous,
please consider my previous post "constructive criticism", not as blame Wink | ;)
 
Yes, there are many articles like that - and the 'oldtimers' on this site agree it's just too many. Codeproject was, and is, a high quality source for developers, and that's why many people stay here. I, as most others, welcome the open structure of CP where everybody can post his articles. However, with the recent rapid increase of members, the average "care" an article gets has decayed.
 
I must admit I didn't even DL your source, because the article I saw looked more like a "quick job", and I expect(ed) the code to be the same. It would be a pity if this happens to your article all the time.
 
Good luck, and keep the articles coming Wink | ;)
Peter
 
btw. Marc has written up some points about how to write a good article[^]. I don't agree 100% with it, and would have certain points to add, but it's a good starting point, and I browse it anytime I plan up a new article.
 

 

"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS

sighist | Agile Programming | doxygen

GeneralRe: Not exactly..memberlallous13 May '03 - 22:43 
Hello Peter,
 
>please consider my previous post "constructive criticism", not as blame
Yes sure...This is the second time you give me constructive comments.
 
I'll read Marc's article and try to make mine a much detailed article.
 
Thanks again,
Lallous

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

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