67194:67194 > 工程师杂谈
[导读]// 已经写过一个 CE5.0 下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570 // 前一段时间在坛子看到有人在问 CE6.

// 已经写过一个 CE5.0 下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570
// 前一段时间在坛子看到有人在问 CE6.0 下钩子的实现,有的说不行,有的说行,最后也没有结果
// 今天试了下,是可以的,包括鼠标的钩子。不过速度上好像是有些问题,待有时间再看看吧。 
// 看代码吧。

// KeyBoardHook.h 

#ifdef KEYBOARDHOOK_EXPORTS
#define KEYBOARDHOOK_API __declspec(dllexport)
#else
#define KEYBOARDHOOK_API __declspec(dllimport)
#endif

// This class is exported from the KeyBoardHook.dll
class KEYBOARDHOOK_API CKeyBoardHook
{
public:
  CKeyBoardHook(void);
  // TODO: add your methods here.
};

extern "C" KEYBOARDHOOK_API void InstallKeyBoardHook(void);
extern "C" KEYBOARDHOOK_API void UnKeyBoardHook(void);
extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam);


// KeyBoardHook.cpp 
// KeyBoardHook.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "KeyBoardHook.h"
#include 

// 告诉编译器将变量放入它自己的数据共享节中
#pragma data_seg("KeyHookData")

HINSTANCE ghInst = NULL;
HHOOK ghKeyHook = NULL;

#pragma data_seg()

// 告诉编译器设置共享节的访问方式为:读,写,共享
#pragma comment(linker, "/SECTION:KeyHookData,RWS")

BOOL APIENTRY DllMain(HANDLE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)
{
  switch (ul_reason_for_call)
  {
  case DLL_PROCESS_ATTACH:
    ghInst = (HINSTANCE)hModule;
    break;
  case DLL_THREAD_ATTACH:
  case DLL_THREAD_DETACH:
  case DLL_PROCESS_DETACH:
    break;
  }
  return TRUE;
}

// This is the constructor of a class that has been exported.
// see KeyBoardHook.h for the class definition
CKeyBoardHook::CKeyBoardHook()
{ 
  return; 
}

extern "C" KEYBOARDHOOK_API void InstallKeyBoardHook(void)
{
  RETAILMSG(1,(L"[Keyboard HOOK]Install HOOK: 0x%xrn",ghInst));
  if(ghInst)
  {
    ghKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, ghInst, 0);
    if(NULL == ghKeyHook)
    {
      RETAILMSG(1,(L"[Keyboard HOOK]HOOK create failed: %drn",GetLastError()));
    }
  }
}

extern "C" KEYBOARDHOOK_API void UnKeyBoardHook(void)
{
  RETAILMSG(1,(L"[Keyboard HOOK]Uninstall HOOK:0x%xrn",ghKeyHook));
  if(ghKeyHook)
  {
    UnhookWindowsHookEx(ghKeyHook);
    ghKeyHook = NULL;
  }

  ghInst = NULL;
}

// CE6 的键盘 HOOK 的响应速度慢, 原因不明!
// CallNextHookEx(hKeyHook, nCode, wParam, lParam); //继续传递消息
// SIP 上仅 Ctl/Shift/CAP/Esc/Backspace 可以处理到
// key value 17/ 16  / 20/ 27/ 8
extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
#if _DEBUG
  RETAILMSG(1,(L"[Keyboard HOOK]Proc,Code is: %d,Down/Up is: %srn",nCode,256 == wParam ? L"Down" : L"Up"));
#endif

  if(HC_ACTION != nCode)
  {
    return CallNextHookEx(ghKeyHook, nCode, wParam, lParam);
  }

  PKBDLLHOOKSTRUCT pKBHookStruct = (PKBDLLHOOKSTRUCT)lParam;
  RETAILMSG(1,(L"[Keyboard HOOK]vkCode value is: %drn",pKBHookStruct->vkCode));
  RETAILMSG(1,(L"[Keyboard HOOK]scanCode value is: %drn",pKBHookStruct->scanCode));
  RETAILMSG(1,(L"[Keyboard HOOK]dwExtraInfo value is: %drn",pKBHookStruct->dwExtraInfo));

  if(WM_KEYDOWN == wParam)
  {
    // 响应按键声,并限制需要向上 Post 的 vkCode
    switch(pKBHookStruct->vkCode)
    {
    case VK_UP:
    case VK_DOWN:
    case VK_LEFT:
    case VK_RIGHT:
      break;
    default:
      break;
    }
  }
  else if (WM_KEYUP == wParam)
  {
    // 限制需要向上Post的vkCode
    switch (pKBHookStruct->vkCode)
    {
    case VK_UP:
    case VK_DOWN:
    case VK_LEFT:
    case VK_RIGHT:
      break;
    default:
      break;
    }
  }

  // 若注释此句, 则被 HOOK 到的按键不再发到 CE 系统 
  return CallNextHookEx(ghKeyHook, nCode, wParam, lParam);  // 继续传递消息
}


// 调用示例

// Keyboard hook test
typedef void (*pInstallKeyboardHook)(void);
typedef void (*pUnKeyboardHook)(void);
HINSTANCE ghKeyboardModule = NULL;
pInstallKeyboardHook  InKeyBoardHook = NULL;
pUnKeyboardHook  UnKeyBoardHook = NULL;


ghKeyboardModule = LoadLibrary(L"\AppStore\SystemInfo\KeyBoardHook.dll");
if(ghKeyboardModule)
{
  InKeyBoardHook = (pInstallKeyboardHook)GetProcAddress(ghKeyboardModule, L"InstallKeyBoardHook");
  UnKeyBoardHook = (pUnKeyboardHook)GetProcAddress(ghKeyboardModule, L"UnKeyBoardHook");
}
if(NULL != InKeyBoardHook)
{
  InKeyBoardHook();
}

工程师杂谈

779 篇文章

关注

发布文章

技术子站

更多

论坛活动

  • TI圣诞狂欢月,答题礼不停
    TI圣诞狂欢月,答题礼不停
  • 上传资源瓜分5000元现金奖励!
  • 学习TI线上培训,赢500元京东卡!
  • 新年历,ADI精品视频课堂开始啦
更多

资料下载

  • (网盘)干货-Altium Designer20 高效实用4层PCB视频课程+配套练习文件
  • (网盘)注册电气工程师考试资料及视频资源
  • (网盘)老顽童STM32开发板开发App及资料
  • (网盘)智芯STM32开发板资料
  • (网盘)刘凯老师STM32视频教程及配套资源
  • (网盘)电路精讲班视频
  • (网盘)quartus基本语法等
  • 非接触式电子体温计(额温枪)PCB+程序源码
  • AD常用3D封装库(Step)【常用贴片篇】
  • (网盘)电路强化班视频
更多

技术专题

  • 单片机编程App的使用
    单片机编程App的使用
  • 二极管的应用及二极管正负极的判断
    二极管的应用及二极管正负极的判断
  • 电路仿真App
    电路仿真App
  • 电磁炉维修的方法
    电磁炉维修的方法
  • 功放维修的方法
    功放维修的方法
  • 电焊技术的应用
    电焊技术的应用
  • 电路设计App的应用
    电路设计App的应用
XML 地图 | Sitemap 地图