|
发表于 2010-1-13 16:00:25
|
显示全部楼层
-
- 在coredll.dll中有SetWindowsHookEX相关函数,这里用LoadLibrary和GetProcAddress可以调用。
- 以下是测试代码:
- //Install the KB hook by passing the
- //handle of the application to be hooked
- //and the address of the KB procedure
- //which will handle all the KB events
- if(!ActivateKBHook(hInstance, LLKeyboardHookCallbackFunction))
- {
- MessageBox(GetActiveWindow(),
- TEXT("Couldn't intall hook...Terminating"),
- TEXT("Warning"), NULL);
- exit(1);
- }
- //LLKeyboardHookCallbackFunction is the funtion whose
- //address we passed to the system while installing the hook.
- //so all the KB events will bring the control to this procedure.
- //Here we want that when the user presse left or
- //right key it should be interpreted as an UP key
- //so now you can allow the user to configure the
- //key boards the way he/she wants it
- LRESULT CALLBACK LLKeyboardHookCallbackFunction(
- int nCode, WPARAM wParam, LPARAM lParam)
- {
- if(((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == VK_LEFT) ||
- ((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == VK_RIGHT))
- {
- //Generate the keyboard press event of the mapped key
- keybd_event(VK_UP, 0, 0, 0);
- //release the mapped key
- keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0);
- }
- //let default processing take place
- return CallNextHookEx(g_hInstalledLLKBDhook, nCode,
- wParam, lParam);
- }
- //we are done with the hook. now uninstall it.
- DeactivateKBHook();
复制代码 |
|