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();
}

换一批

延伸阅读

[显示光电] 全系统鼠标光标支撑功能将在新 iPad 中推出

全系统鼠标光标支撑功能将在新 iPad 中推出

苹果通常会在每年 6 月举行一年一度的 WWDC 全球开发者大会,推出新一代 iOS 14 系统。今年由于新冠肺炎疫情的形势严峻,WWDC 2020 是否能如期举行现在还无法确定,但系统研发不会停下,苹果已经开始了新系统的内部测试...

关键字: 14 iOS smart 代码 机型 keyboard 全系统鼠标光标支撑

[代码人生] 操作系统开发 – 婴儿教程 5

This code is meant to show how the hardware?interrupt?generated when you press a key can be handled by replacing the se...

关键字: keyboard vector

[Linuxer的小圈子] 外国一个比较全的操作系统开发网站

外国一个比较全的操作系统开发网站 http://www.osdever.net/documents.php?cat=0&sort=1 Documents The Universal Serial Bus?Printed Book...

关键字: microsoft keyboard

[工程师杂谈] CE 6 下键盘钩子(HOOK)

// 已经写过一个 CE5.0 下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570 // 前一段时间在坛子看到有人在问 CE6.0 下钩子的实现,有的说不行,有的说行,...

关键字: keyboard hook

[玩儿转嵌入式开发] CE 6 下键盘钩子(HOOK)的实现

//?已经写过一个?CE5.0?下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570 //?前一段时间在坛子看到有人在问?CE6.0?下钩子的实现,有的说不行,有的说行,...

关键字: keyboard hook

工程师杂谈

779 篇文章

关注

发布文章

技术子站

更多

论坛活动

  • 加入Vicor,让机器人供电系统设计更有“料”
    加入Vicor,让机器人供电系统设计更有“料”
  • ADI视频课堂精品上新-观看有礼
  • 【20周年庆】21ic下载站VIP五折起,技术资料免积分下载!
  • 贸泽电子设计“新”头好
更多

资料下载

  • ST-Link原理图和固件
  • Linux下C语言编程入门教程
  • Multisim2001电路设计及仿真教材
  • Linux内核源码分析--zImage出生实录(Linux-3.0ARMv7)
  • 新概念模拟电路全集
  • 张飞电子全套视频下载地址及提取码
  • AD封装库-国标版
  • stm32温湿度-超声波-LCD结合项目
  • DSP教程---ADC和DAC
  • 分布式BFD系统中驱动模块的设计与实现
关闭
XML 地图 | Sitemap 地图