资源描述:
《java监听键盘鼠标全局事件》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#include"SysHook.h"#includeHINSTANCEhInst=NULL;JavaVM*jvm=NULL;jobjecthookObj_kb=NULL;jobjecthookObj_ms=NULL;jobjectg_kl=NULL;jmethodIDprocessKeyID_kb=NULL;jmethodIDprocessKeyID_ms=NULL;DWORDhookThreadId=0;LONGg_mouseLocX=-1;//x-location
2、ofmousepositionLONGg_mouseLocY=-1;//y-locationofmousepositionextern"C"BOOLAPIENTRYDllMain(HINSTANCE_hInst,DWORDreason,LPVOIDreserved){switch(reason){caseDLL_PROCESS_ATTACH:printf("C++:DllMain-DLL_PROCESS_ATTACH.");hInst=_hInst;break;default:break;}returnTRUE;}L
3、RESULTCALLBACKMouseTracker(intnCode,WPARAMwParam,LPARAMlParam){JNIEnv*env;KBDLLHOOKSTRUCT*p=(KBDLLHOOKSTRUCT*)lParam;if(jvm->AttachCurrentThread((void**)&env,NULL)>=0){if(nCode==HC_ACTION){MOUSEHOOKSTRUCT*pStruct=(MOUSEHOOKSTRUCT*)lParam;if(pStruct->pt.x!=g_mouse
4、LocX
5、
6、pStruct->pt.y!=g_mouseLocY){env->CallVoidMethod(hookObj_ms,processKeyID_ms,(jint)pStruct->pt.x,(jint)pStruct->pt.y,g_kl);g_mouseLocX=pStruct->pt.x;g_mouseLocY=pStruct->pt.y;}}}else{printf("C++:LowLevelKeyboardProc-Errorontheattachcurrentthread.");}returnC
7、allNextHookEx(NULL,nCode,wParam,lParam);}LRESULTCALLBACKLowLevelKeyboardProc(intnCode,WPARAMwParam,LPARAMlParam){JNIEnv*env;KBDLLHOOKSTRUCT*p=(KBDLLHOOKSTRUCT*)lParam;if(jvm->AttachCurrentThread((void**)&env,NULL)>=0){switch(wParam){caseWM_KEYDOWN:caseWM_SYSKEYDO
8、WN:env->CallVoidMethod(hookObj_kb,processKeyID_kb,(jboolean)TRUE,p->vkCode,g_kl);break;caseWM_KEYUP:caseWM_SYSKEYUP:env->CallVoidMethod(hookObj_kb,processKeyID_kb,(jboolean)FALSE,p->vkCode,g_kl);break;default:break;}}else{printf("C++:LowLevelKeyboardProc-Erroront
9、heattachcurrentthread.");}returnCallNextHookEx(NULL,nCode,wParam,lParam);}voidMsgLoop(){MSGmessage;while(GetMessage(&message,NULL,0,0)){TranslateMessage(&message);DispatchMessage(&message);}}JNIEXPORTvoidJNICALLJava_SysHook_registerHook(JNIEnv*env,jobjectobj,jo
10、bjectkl){HHOOKhookHandle_ms=SetWindowsHookEx(WH_MOUSE_LL,MouseTracker,hInst,0);HHOOKhookHandle_kb=SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,hInst,0)