Click here to Skip to main content
6,596,602 members and growing! (18,514 online)
Email Password   helpLost your password?
General Reading » Hardware & System » General     Intermediate

Global hotkeys made easy

By lallous

A class to facilitates global hotkeys registration and handling
VC6, VC7, VC7.1Win2K, WinXP, Win2003, Dev
Posted:8 May 2003
Updated:19 Oct 2003
Views:73,254
Bookmarked:27 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.44 Rating: 3.44 out of 5
1 vote, 10.0%
1
1 vote, 10.0%
2
1 vote, 10.0%
3
2 votes, 20.0%
4
5 votes, 50.0%
5

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

lallous


Member
Elias (aka lallous) 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.

Elias maintains a blog at http://lallousx86.wordpress.com/ and a website at http://lgwm.org/
Occupation: Web Developer
Location: Lebanon Lebanon

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 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralProblem while compiling in VC++ 2008 PinmemberNitsanBr13:41 30 Aug '08  
GeneralRe: Problem while compiling in VC++ 2008 Pinmember428811:38 14 Aug '09  
GeneralErrors and problems PinmemberVal Samko12:26 18 Oct '03  
GeneralRe: Errors and problems Pinmemberlallous2:48 20 Oct '03  
Generallocal hotkey Pinmembershahrzad2:00 4 Aug '03  
GeneralRe: local hotkey PinsussAnonymous12:01 9 Aug '03  
GeneralHotkeys Vs. KB Hooks Pinmembersumudu7:41 12 Jun '03  
GeneralRe: Hotkeys Vs. KB Hooks Pinmemberlallous4:04 18 Jun '03  
GeneralRe: Hotkeys Vs. KB Hooks Pinmembersumudu7:42 18 Jun '03  
GeneralRe: Hotkeys Vs. KB Hooks PinmemberJGHG3:04 24 Jun '04  
GeneralRe: Hotkeys Vs. KB Hooks Pinmemberlallous3:28 24 Jun '04  
Generalsynchronous Pinmemberlins0119:58 21 May '03  
GeneralRe: synchronous Pinmemberlallous0:56 23 May '03  
GeneralRe: synchronous Pinmemberlins0120:49 27 May '03  
GeneralRe: synchronous Pinmemberlallous23:28 27 May '03  
GeneralRe: synchronous Pinmemberlins0115:47 28 May '03  
GeneralNot exactly.. Pinsupporterpeterchen23:14 12 May '03  
GeneralRe: Not exactly.. Pinsusslallous22:40 13 May '03  
GeneralRe: Not exactly.. Pinsupporterpeterchen23:28 13 May '03  
GeneralRe: Not exactly.. Pinmemberlallous23:43 13 May '03  
GeneralExample doesn't work for me PinmemberMr.X4:04 12 May '03  
GeneralRe: Example doesn't work for me Pinmemberlallous11:12 25 May '03  

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

PermaLink | Privacy | Terms of Use
Last Updated: 19 Oct 2003
Editor: Nishant Sivakumar
Copyright 2003 by lallous
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project