关于Low Level Keyboard Hook的郁闷,万恶的SDK啊~~
来源:互联网 发布:黄维德的周瑜 知乎 编辑:程序博客网 时间:2024/05/01 06:52
今天要做点事情,需要屏蔽Win键的几个组合键,于是想到用WH_KEYBOARD_LL,随便看了下VS6.0自带的MSDN里LowLevelKeyboardProc的描述:
The LowLevelKeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue. The keyboard input can come from the local keyboard driver or from calls to thekeybd_event function. If the input comes from a call to keybd_event, the input was "injected".
于是写了类似下面的代码:
void f()
{
HHOOK hHook = NULL;
HANDLE hThread = NULL;
__try
{
hHOOK = SetWindowHookEx(hInstance,WH_KEYBOARD_LL,LowLevelKeyboardProc,0);
if(NULL == hHook) __leave;
hThread=(HANDLE) _beginthread(.......);
if ((HANDLE)1 == hThread) __leave;
WaitForSingleObject(hThread);
}
__finally
{
if (NULL != hThread) CloseHandle(hThread);
if (NULL != hHook) UnhookWindowsHookEx(hHook);
}
}
结果发现,安装的HOOK不起作用,研究了很久也不明白怎么回事,正在郁闷中,突然看到Win2K3的SDK Documentation里对LowLevelKeyboardProc的描述是:
The LowLevelKeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue. The keyboard input can come from the local keyboard driver or from calls to the keybd_event function. If the input comes from a call to keybd_event, the input was "injected". However, the WH_KEYBOARD_LL hook is not injected into another process. Instead, the context switches back to the process that installed the hook and it is called in its original context. Then the context switches back to the application that generated the event.
天啊,平白无故的多出来一段,真是万恶的SDK啊~~!这样的话,问题就很清楚了,安装Hook的线程没有消息循环,所以没有机会执行LowLevelKeyboardProc。于是改造:
void f()
{
HANDLE hHOOKThread = NULL;
HANDLE hThread = NULL;
__try
{
DWORD TID;
hHOOKThread = (HANDLE) _beginthreadex(NULL,0,HookThreadProc,NULL,0,&TID);
if(NULL == hHOOKThread) __leave;
hThread=(HANDLE) _beginthread(.......);
if ((HANDLE)1 == hThread) __leave;
WaitForSingleObject(hThread);
PostThreadMessage(TID,WM_QUIT,0,0);
}
__finally
{
if (NULL != hThread) CloseHandle(hThread);
if (NULL !=hHOOKThread) CloseHandle(hHOOKThread);
if (NULL != hHook) UnhookWindowsHookEx(hHook);
}
}
unsigned __stdcall HookThreadProc(void * lpParam)
{
HOOK hHook = SetWindowsHookEx(hInstance,WH_KEYBOARD_LL,LowLevelKeyboardProc,0);
if (NULL == hHook) return;
MSG msg;
while(GetMessage(&msg,NULL,0,0));
UnhookWindowsHookEx(hHook);
}
问题解决。
- 关于Low Level Keyboard Hook的郁闷,万恶的SDK啊~~
- 关于"top-level const"与"low-level const"的疑惑
- 万恶的document啊
- 郁闷的sdk
- 介绍 TestComplete 的 LLP (Low-Level Procedures)
- 万恶的字符串存储啊
- 在ubuntu安装sdk和adt安装不上(万恶的墙啊)
- 万恶的Makefile,万恶的记事本。
- 万恶的BOM,万恶的微软
- Kernel low-level debugging functions linux汇编的调试方法
- 史陶比尔机器人的 LLI (Low Level Interface)
- zabbix 自定义自动发现的key! low level discovery
- Kernel low-level debugging functions linux汇编的调试方法
- 关于hook的
- 关于HOOK的用法
- 万恶的食堂
- 万恶的百度竞价排名
- 万恶的javabean
- Windows CE 5.0模拟器调试串口的环境配置
- C#正则表达式小结
- 通用SQL数据库查询语句/连接查询/多表连接查询 sql小例子
- struts2文件下载
- 哈哈
- 关于Low Level Keyboard Hook的郁闷,万恶的SDK啊~~
- 只需要五分钟,让你明白中国的内需是怎么回事
- 2009下半年软件设计师考试大纲
- JFreeChart教程
- SAP系统配置参数详解
- IT碰碰车第四期:中国式创业因李开复而不同?
- 2009.9.17-名字空间
- 问题解答:Eclipse中怎样判断Editor中光标的位置,并找到光标周围代码,这样的事件在哪里写?
- SharePoint工作流解决方案QuickFlow系列(6)—事件总线(EventBus)