Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5382147
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类: C/C++

2008-09-05 16:32:34

Introduction

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

Using the code

Collapse
#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

阅读(711) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~