首页
社区
课程
招聘
WH_KEYBOARD_LL钩子的问题
发表于: 2010-4-15 22:59 9641

WH_KEYBOARD_LL钩子的问题

2010-4-15 22:59
9641
这个钩子我看msdn说只能做全局钩子,SetWindowsHookEx最后一个参数只能为0。
我想钩某一个线程的(只是想用低级钩子,不用WH_KEYBOARD),可以通过回调函数判断线程或者进程或者窗口吗。。

另外,我想从WH_KEYBOARD_LL得到类似WM_CHAR的字符消息,不想要按键消息。。怎么弄,谁有标准的代码给我参考下好吗。。谢谢

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 249
活跃值: (25)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
2
全局钩子需要用DLL来实现,以前做过一两个,现在也忘了~~
2010-4-16 09:44
0
雪    币: 2105
活跃值: (424)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
3
你的KbdLLProc传进来的参数有线程ID
TranslateMessage
2010-4-16 13:25
0
雪    币: 236
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
给楼主参考。。。

#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

HHOOK g_hLLKeyboardHook = NULL;


#ifdef WH_KEYBOARD_LL
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	CHAR szDebug[256];

	if (nCode == HC_ACTION)
	{
		PKBDLLHOOKSTRUCT pKeyboardHookStruct = (PKBDLLHOOKSTRUCT) lParam;

		if ((wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN))
		{
			BYTE KeyboardState[256];
			ZeroMemory(KeyboardState, sizeof(KeyboardState));
			GetKeyboardState(KeyboardState);

			KeyboardState[VK_SHIFT] = (BYTE) (GetKeyState(VK_LSHIFT) | GetKeyState(VK_RSHIFT));
			KeyboardState[VK_CAPITAL] = (BYTE) GetKeyState(VK_CAPITAL);

			WORD wChar;

			int iNumChar = ToAscii(pKeyboardHookStruct->vkCode, pKeyboardHookStruct->scanCode, KeyboardState, &wChar, 0);

			if (iNumChar >= 1)
			{
				_snprintf(szDebug, 255, "%c", wChar);
				OutputDebugString(szDebug);
			}

			if (iNumChar <= 0)
			{
				CHAR KeyText[20];
				ZeroMemory(KeyText, sizeof(KeyText));

				LONG Flags = 0;
				Flags = pKeyboardHookStruct->scanCode << 16;
				Flags |= pKeyboardHookStruct->flags << 24;
				
				if (GetKeyNameText(Flags, KeyText, 20) > 0)
				{
					_snprintf(szDebug, 255, "%s", KeyText);
					OutputDebugString(szDebug);
				}
			}
		}
	}

	return CallNextHookEx(g_hLLKeyboardHook, nCode, wParam, lParam);
}
#endif

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	g_hLLKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
	
	if (g_hLLKeyboardHook == NULL)
	{
		return -1;
	}

	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) 
	{ 
		TranslateMessage(&msg); 
		DispatchMessage(&msg); 
	} 

	UnhookWindowsHookEx(g_hLLKeyboardHook);

	return msg.wParam;
}
2010-4-16 13:58
0
雪    币: 18
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
你好,呵呵。
请问一下,哪个参数给线程ID了啊?
2010-4-17 00:04
0
雪    币: 18
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
谢谢哥们,这可是相当的好使啊~~~
2010-4-17 00:05
0
游客
登录 | 注册 方可回帖
返回
//